天天看点

jsp连接各大数据库的方法

第一步:请把数据库驱动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>