現在有好多初學jsp的網友經常會問數據庫怎么連接啊,怎么老出錯?。克晕壹械脑谶@寫篇文章供大家參考,其實這種把數據庫邏輯全部放在jsp里未必是好的做法,但是有利于初學者學習,所以我就這樣做了,當大家學到一定程度的時候,可以考慮用mvc的模式開發。在練習這些代碼的時候,你一定將jdbc的驅動程序放到服務器的類路徑里,然后要在數據庫里建一個表test,有兩個字段比如為test1,test2,可以用下面sql建
create table test(test1 varchar(20),test2 varchar(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>
商業源碼熱門下載www.html.org.cn
新聞熱點
疑難解答