主题:诚心请教JSP连接SQL 2000的相关问题.谢谢.
最近想练习用JSP连接SQL 2000数据库.可总是抱错.有点快崩溃了的感觉.....
先将代码贴上.请大家说说哪里不对了?.新手,请尽量详细一点.
谢谢.
代码如下:
首先建一个 class写入如下代码:
package ok;
import java.sql.*;
public class sql {
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="ok.sql"></jsp:useBean>
<html>
<head>
</head>
<body>
<%
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
conn=sel.getConn();
stmt=conn.getStatement();
rs=stmt.executeQuery("select * from cat");
while(rs.next()){
out.println(rs.getString(1));
out.println(rs.getString(2));
}
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
请大家指点一二.
先将代码贴上.请大家说说哪里不对了?.新手,请尽量详细一点.
谢谢.
代码如下:
首先建一个 class写入如下代码:
package ok;
import java.sql.*;
public class sql {
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="ok.sql"></jsp:useBean>
<html>
<head>
</head>
<body>
<%
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
conn=sel.getConn();
stmt=conn.getStatement();
rs=stmt.executeQuery("select * from cat");
while(rs.next()){
out.println(rs.getString(1));
out.println(rs.getString(2));
}
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
请大家指点一二.