JSP怎样用JDBC连接SQL SERVER?(100分)

  • 主题发起人 主题发起人 sercall
  • 开始时间 开始时间
S

sercall

Unregistered / Unconfirmed
GUEST, unregistred user!
1.JDK1.3. 用JSWDK做服务器.
2.已在"系统DSN"中注册了名为"jspEx"的SQL Server的数据源.
3.在系统环境Classpath中加了"jswdk-1.0.1/webpages/web-inv/jsp/beans.
(opendb.java就放在jswdk-1.0.1/webpages/web-inv/jsp/beans/opendb/opendb.java)
但未能连接数据库.我的错误在那? 或应该用那种JDBC驱动程序来连接?
谢谢!! 详细如下:

(1):写了一个JavaBeans程序.并已将opendb.java编印成功为opendb.class.
package opendb;
import java.sql.*;
public class opendb
{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:jspEx";
Connection conn = null;
ResultSet rs = null;
public opendb( )
{
try{ Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{ System.err.println("opendb( ):'" + e.getMessage( ));}
}
public ResultSet executeQuery(String sql)
{
rs = null;
try {
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement( );
rs = stmt.executeQuery (sql);
}
catch(SQLException ex){
System.err.println("aq.executeQuery:" + ex.getMessage( ));
}
return rs;
}
}

(2):再写了一个测试JSP连接数据库的程序.zlm1.jsp
<%@ page language="java" import="java.sql.*"%>
<jsp:useBean id="jspEx" scope="page" class="opendb.opendb"/>
<%
String sql="select * from userId";
ResultSet rs=jspEx.executeQuery(sql);
out.print("<pre>"+rs.getString("Name")+"</pre> ");
rs.close();
%>
(3):在IE发布时出现"HTTP 500 - 内部服务器错误"
jswdk服务器的错误如下:
....
com.sun.jsp.runtime.JspServlet: init
Scratch dir for the JSP engine is: work/%3A8080%2F
IMPORTANT:do
not modify the generated servlets
Unhandled error! You might want to consider having an error page to report such
errors more gracefully
java.sql.SQLException: [Microsoft][ODBC 驱动程序 管理器] 无效的光标状态
at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6031)
....

HANDLER THREAD PROBLEM: java.io.IOException: Socket Closed
java.io.IOException: Socket Closed
at java.net.PlainSocketImpl.getInputStream(PlainSocketImpl.java:432)
....
Thank!!
 
请查看一下是否ODBC连接SQL Server正常。
 
接受答案了.
 
后退
顶部