第一步:請把資料庫驅動mm.mysql-2.0.4-bin.jar放到web-inf/lib/目錄下(如果是虛拟主機請放到您的虛拟主機根目錄下web-inf/lib/)
第二步:把testlinkmysql.jsp連接配接資料庫檔案放到您的具有可執行jsp的目錄下,通過web執行.如果看到恭喜資料庫連接配接成功字樣那麼恭喜您資料連接配接成功!
在c:/tomcat5/webapps/myapp/webapp目錄下用記事本或editplus(一個編輯工具)編寫一個檔案儲存為testlinkmysql.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/example?user=
root&password=1234&useunicode=true&characterencoding=8859_1" ;//資料庫連接配接字元串
// about為你的資料表名
connection conn= drivermanager .getconnection (url);
statement stmt=conn.createstatement
(resultset.type_scroll_sensitive,
resultset.concur_updatable);
string sql ="select * from about ";
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裡未必是好的做法,當大家學到一定程度的時候,可以考慮用mvc的模式開發。在練習這些代碼的時候,注意:你一定将jdbc的驅動程式放到伺服器的類路徑裡,然後要在資料庫裡建一個表test,有兩個字段比如為test1,test2,可以用下面sql 建
create table test(test1 varchar(20),test2 varchar(20)
然後向這個表寫入一條測試紀錄。那麼現在開始我們的jsp和資料庫之旅吧。我們重點講
一、jsp連接配接oracle8/8i/9i資料庫(用thin模式)
testoracle.jsp如下:
<%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";
二、jsp連接配接sql server7.0/2000資料庫
testsql server.jsp如下:
<%class.forname("com.microsoft.jdbc. sqlserver. sql serverdriver ").newinstance();
string url="jdbc:microsoft: sqlserver://localhost:1433;databasename=pubs";
//pubs為你的資料庫的
string user="sa";
string password="";
statement stmt=conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable);
三、jsp連接配接db2資料庫
testdb2.jsp如下:
<%class.forname("com.ibm.db2.jdbc.app.db2driver").newinstance();
string url="jdbc:db2://localhost:5000/sample";
//sample為你的資料庫名
string user="admin";
四、jsp連接配接informix資料庫
testinformix.jsp如下:
<%class.forname("com.informix.jdbc.ifxdriver").newinstance();
string url =
"jdbc:informix-sql i://123.45.67.89:1533/testdb:informixserver=myserver;
user=testuser;password=testpassword";
//testdb為你的資料庫名
五、jsp連接配接sybase資料庫
testmysql.jsp如下:
<%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);
六、jsp連接配接mysql資料庫
<%class.forname("org.gjt.mm.mysql.driver").newinstance();
string url ="jdbc:mysql://localhost/example?user=root&password=1234&useunicode=true&characterencoding=8859_1"
//example為你的資料庫名
七、jsp連接配接postgresql 資料庫
<%class.forname("org.postgresql .driver").newinstance();
string url ="jdbc:postgresql ://localhost/soft"
//soft為你的資料庫名
string user="myuser";
string password="mypassword";
</html>