1、用数据库软件(如:mysql)创建数据库,在数据库注册时用到的数据库名则为刚刚创建的数据
库,若为数据源名,则需odbc配置。
2、将驱动程序放在d:tomcat 5.5commonlib或相应的开发工具的lib下,
对d:tomcat 5.5confweb.xml修改,打开web.xml,在</web-app>的前面添加以下内容:
<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.datasource</res-type>
<res-auth>container</res-auth>
</resource-ref>
通过文件夹导航到d:tomcat 5.5confcatalinalocalhost下,找到你的web应用对应的.xml文
件,如root.xml,并在此文件的下添入代码:
<resourcelink name="jdbc/mysql" global="jdbc/mysql"
type="javax.sql.datasourcer"/>
到这里,配置工作就基本完成了。
3、数据库连接(注:以下为转载)
一、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>
闽公网安备 35060202000074号