求救:JSP调用bean访问sql server的问题!
yhb72 2004-09-02 10:47:45 在应用目录mytest中放有测试文件:testbean.jsp,在mytest目录下的WEB-INF的classes中有文件夹SqlConn,其下有编译好的Conn.class。环境变量配置绝对没有问题,非连接后台数据库的bean调用测试都成功了,但测试bean访问数据库报错,实在搞不懂问题在哪里,望高手指教!
环境配置:windows2003+sql server2000(sp3)+tomcat5.0
源码:
Conn.java:
package SqlConn;
import java.sql.*;
import java.lang.*;
import java.util.*;
public class Conn
{
String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String sConnStr ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ims;user=sa;password=123456";
Connection conn=null;
ResultSet rs=null;
public Conn()
{
try {
Class.forName(sDBDriver);
}
catch(ClassNotFoundException e)
{
System.out.println("无法建立数据库连接!:"+e.getMessage());
}
}
public void executeUpdate(String sql) throws Exception
{
sql= new String(sql.getBytes("GBK"),"ISO8859_1");
try
{
conn= DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.out.println("更新数据操作失败!"+ex.getMessage());
}
}
public ResultSet executeQuery(String sql) throws Exception
{
rs = null;
try
{
sql= new String(sql.getBytes("GBK"),"ISO8859_1");
conn= DriverManager.getConnection(sConnStr);
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
conn.close();
stmt.close();
}
catch(SQLException ex)
{
System.out.println("执行查询出错!"+ex.getMessage());
}
return rs;
}
}
------------------------------------------------------------------------
testbean.jsp:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.sql.*" %>
<jsp:useBean id="Conn" scope="page" class="SqlConn.Conn"/>
<%
String uname= request.getParameter("username");
String pwd= request.getParameter("passwd");
if (uname.length()!=0)
{
if (pwd.length()==0)
{
out.println ("<script language=javascript>alert('密码不能为空');javascript:history.back();</script>");
}
else
{
ResultSet rt=Conn.executeQuery("select username from testuser where username='"+uname+"' and pass='"+pwd+"'" );
if (!rt.next())
{
out.println("用户名密码错误");
}
else
{
out.println("登录成功!");
}
}
}
%>
<html>
<head></head>
<body>
<FORM action=testbean.jsp method=POST>
<table>
<tr>
<td>用户名:</td><td><input type="text" name="username" size="16"></td>
</tr>
<tr>
<td>密 码:</td><td><input type="text" name="passwd" size="16"></td>
</tr>
<tr>
<td></td><td><input type="submit" name="btn1" value="登录"></td>
</tr>
</table>
</form>
</body>
</html>
---------------------------------------------------------------------
报错:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:346)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
root cause
java.lang.NullPointerException
org.apache.jsp.testbean_jsp._jspService(testbean_jsp.java:58)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:298)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.25 logs.