使用JDBC访问数据库

在Oracle安装目录\10.2\jdbc\lib 下,找到classes12.jar,它就是用于连接Oracle数据库的JDBC驱动程序。将其拷贝到Tomcat的安装目录"Tomcat 6.0\lib"目录下。重启Tomcat,这个驱动程序即可生效。

编写JSP程序

<%@ page language="java" contentType="text/html; charset=GB2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<%@ page import="java.sql.*" %>
<body>
<table border=1 align="center">
<tr>
	<td>ID</td>
	<td>姓名</td>
	<td>密码</td>
</tr>
<%
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@192.168.1.18:1521:orcl","oftenlin","123456");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from userinfo");
while(rs.next())
{
 %>
 <tr>
 <td><%=rs.getInt(1)%></td>
 <td><%=rs.getString(2)%></td>
 <td><%=rs.getString(3)%></td>
 </tr>
 <%}
 rs.close();
 con.close();
%>
</table>
</body>
</html>

程序中用一个url指向数据库的连接

posted @ 2012-12-20 17:31  oftenlin  阅读(217)  评论(0编辑  收藏  举报