请问如何连数据库?

LoveLL810625 2003-10-20 12:24:14
那位大虾可以将具体的连数据库的步骤告诉我,我目前手头没有相关的书
谢谢了!!!!
...全文
88 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
TonyTonyQ 2003-10-20
  • 打赏
  • 举报
回复
这是我看到的一篇关于jsp连接数据库的文章,但是其中连接和使用数据库的方法你可以看看啊。

一、jsp连接Oracle8/8i/9i数据库(用thin模式)
testoracle.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl";
//orcl为你的数据库的SID
String user="scott";
String password="tiger";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
二、jsp连接Sql Server7.0/2000数据库
testsqlserver.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//pubs为你的数据库的
String user="sa";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
三、jsp连接DB2数据库
testdb2.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.ibm.db2.jdbc.app.DB2Driver ").newInstance();
String url="jdbc:db2://localhost:5000/sample";
//sample为你的数据库名
String user="admin";
String password="";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
四、jsp连接Informix数据库
testinformix.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.informix.jdbc.IfxDriver").newInstance();
String url =
"jdbc:informix-sqli://123.45.67.89:1533/testDB:INFORMIXSERVER=myserver;
user=testuser;password=testpassword";
//testDB为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
五、jsp连接Sybase数据库
testmysql.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("com.sybase.jdbc.SybDriver").newInstance();
String url =" jdbc:sybase:Tds:localhost:5007/tsdata";
//tsdata为你的数据库名
Properties sysProps = System.getProperties();
SysProps.put("user","userid");
SysProps.put("password","user_password");
Connection conn= DriverManager.getConnection(url, SysProps);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
六、jsp连接MySQL数据库
testmysql.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//testDB为你的数据库名
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
七、jsp连接PostgreSQL数据库
testmysql.jsp如下:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.postgresql.Driver").newInstance();
String url ="jdbc:postgresql://localhost/soft"
//soft为你的数据库名
String user="myuser";
String password="mypassword";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
您的第一个字段内容为:<%=rs.getString(1)%>
您的第二个字段内容为:<%=rs.getString(2)%>
<%}%>
<%out.print("数据库操作成功,恭喜你");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
mtou 2003-10-20
  • 打赏
  • 举报
回复
//oracle
Connection connection=null;
try
{
String driverName="oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);

String serverName="127.0.0.1";
String portNumber="1521";
String sid="mydatabase";
String url="jdbc:oracle:thin:@"+serverName+":"+portNumber+":"+sid;
String username="username";
String password="password";
connection=DriverManager.getConnection(url,username,password);
}
catch(ClasNotFoundException e){
//Could not find the database driver
}catch(SQLException e){
//Could not connect to the database
}


//SQL Server
String drv = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433";
String uid = "sa";
String pwd = "123456";

Connection cn=null;
try
{
Class.forName(drv).newInstance();
cn=DriverManager.getConnection(url,uid,pwd);
}
catch (Exception e)
{
e.printStackTrace();
}
LoveLL810625 2003-10-20
  • 打赏
  • 举报
回复
请看这个程序:

import java.sql.*;
public class JdbcDemo
{
public static void main(String[] args)
{
try{
Statement stmt;
PreparedStatement pstmt;
ResultSet rs;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:JDBCDemo";
Connection con=DriverManager.getConnection(url);
System.out.println("URL: "+url);
System.out.println("Connection: "+con);
stmt=con.createStatement();
System.out.println("DROP TABLE DemoTable,if it exists.");
try{
stmt.executeUpdate("DROP TABLE DemoTable");
}catch(Exception e){
System.out.print(e);
System.out.println("No existing table to delete");
}

stmt.executeUpdate("CREATE TABLE DemoTable ("+"test_id int,test_val char(15) not null)");
System.out.println("table DemoTable created!");
stmt.executeUpdate("INSERT INTO DemoTable (" + "test_id,test_val) VALUES(6,'Six')");
stmt.executeUpdate("INSERT INTO DemoTable (" + "test_id,test_val) VALUES(2,'Two')");
stmt.executeUpdate("INSERT INTO DemoTable (" + "test_id,test_val) VALUES(3,'Three')");
stmt.executeUpdate("INSERT INTO DemoTable (" + "test_id,test_val) VALUES(4,'Four')");
stmt.executeUpdate("INSERT INTO DemoTable (" + "test_id,test_val) VALUES(5,'Five')");

stmt=con.createStatement();
rs=stmt.executeQuery("SELECT * from DemoTable ORDER BY test_id");
System.out.println("Display all results:");
while(rs.next()){
int theInt=rs.getInt("test_id");
String str=rs.getString("test_val");
System.out.println("\ttest_id= "+ theInt+ "\tstr= "+str);
}
pstmt=con.prepareStatement("UPDATE DemoTable SET test_val = ? WHERE test_id =?");
pstmt.setString(1,"Hello!");
pstmt.setInt(2,2);
pstmt.executeUpdate();
System.out.println("Update row number 2: OK.");
stmt=con.createStatement();
rs=stmt.executeQuery("SELECT * from DemoTable ORDER BY test_id");
System.out.println("Display row 2:");
if(rs.next()&&rs.next()){
int theInt=rs.getInt("test_id");
String str=rs.getString("test_val");
System.out.println("\ttest_id=" + theInt + "\tstr=" + str);
}
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
}

我在WINDOWS的控制面板中的管理工具的数据源中添加了sql数据源,名字叫JDBCDemo;然后在SQL数据库中,建立了一个数据库,在其中建立了一个叫DemoTable表,表的字段和程序中的DemoTable的字段相同,分别是:test_id,test_val,输入一些数据。然后运行以上的程序,可是似乎程序并没有和数据库真正连接起来,请问这是为什么?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧