最近联系JSP连接SQL 2000数据库.可总是有错误.不知道怎么解决.现将代码贴上.请各位指点一二.
新手,请尽量详细.在此有礼了.谢谢!
代码如下:
首先建一个class来存放连接数据库用的相关代码:
package test;
import java.sql.*;
public class DB {
private Connection conn;
private Statement stmt;
private ResultSet rs;
private void preparedDriver(){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
}
public Connection getConn(){
this.preparedDriver();
try {
conn = DriverManager.getConnection("jdbc:odbc:test","","");
} catch (SQLException ex) {
ex.printStackTrace();
}
return conn;
}
}
然后建一JSP来调用以上代码:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*"%>
<jsp:useBean id="sel" scope="page" class="test.DB"></jsp:useBean>
<html>
<head>
</head>
<body>
<%
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
conn=sel.getConn();
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from Ango");
while(rs.next()){
out.println(rs.getInt(1));
out.println(rs.getString(2));
out.println(rs.getString(3));
out.println(rs.getString(4));
out.println(rs.getString(5));
out.println(rs.getString(6));
out.println(rs.getFloat(7));
out.println(rs.getString(8));
out.println(rs.getInt(9));
out.println(rs.getInt(10));
}
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>