求救: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.

...全文
264 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
hoggio_1980 2004-09-03
  • 打赏
  • 举报
回复
除了lguo_714(凌)和nchln(打倒传奇)说的,我还想补充以下if (uname.length()!=0),你的lenth后面的括号好像是中文的
yhb72 2004-09-03
  • 打赏
  • 举报
回复
在调试JSP程序过程中,常常被:
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)
这种错误困扰,不知如何下手排错!查代码也查不出问题啊?
yhb72 2004-09-03
  • 打赏
  • 举报
回复
这个是测试JSP:
<%@ page language="java" import="java.lang.*,java.util.*" %>
<%@ page import="java.sql.*" %>
<html>
<head></head>
<body>

<jsp:useBean id="myBean" scope="page" class="db.DBConnect" />
<%
String strSql="select * from test";
ResultSet rs=myBean.executeQuery(strSql);
while(rs.next())
{
out.println(rs.getString(0));
}
rs.close();
myBean.closeConnection();
%>
</body>
</html>
kevin_yeah 2004-09-03
  • 打赏
  • 举报
回复
也遇到过类似问题,帮你UP一下!
yhb72 2004-09-03
  • 打赏
  • 举报
回复
DBConnect.java:

package db;

import java.net.*;
import java.sql.*;
import java.lang.*;
import java.io.*;
import java.util.*;

public class DBConnect
{
static Connection conn=null;
Statement stmt=null;
ResultSet rs=null;

public DBConnect(){}

public static String getConnection()
{
try{

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn= DriverManager.getConnection("jdbc:oracle:thin:@172.168.1.22:1521:hms","user01","user01");
return "数据库连接成功!";
}
catch(java.lang.ClassNotFoundException e){
return "驱动加载失败靠"+"ClassNotFoundException:"+e.getMessage();
}
catch(java.sql.SQLException e){
return "连接数据库错误了么"+"SQLExcception:"+e.getMessage();
}
}


public void closeConnection()
{
try{
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
catch(java.sql.SQLException e)
{
System.err.println("SQLBridge():"+e.getMessage());
}
rs=null;
stmt=null;
conn=null;
}

public ResultSet executeQuery(String sql)
{
try
{
if(conn==null)
getConnection();
if(conn!=null)
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
}

catch(SQLException ex)
{
System.err.println("SQLBridge.executeQuery:"+ex.getMessage());
}
return rs;
}


public boolean executeUpdate(String sql)
{
boolean bupdate=false;
try
{

if(conn==null)
getConnection();
if(conn!=null)
{
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
int rowCount=stmt.executeUpdate(sql);
if(rowCount!=0)
bupdate=true;
}
}
catch(SQLException ex)
{
System.err.println("SQLBridge.executeUpdate:"+ex.getMessage());
}
return bupdate;
}


}
yhb72 2004-09-03
  • 打赏
  • 举报
回复
我后台又重新做了一个链接oracle的bean测试,发现多半情况都报:
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
db.DBConnect.executeQuery(DBConnect.java:61)
org.apache.jsp.orabean_jsp._jspService(orabean_jsp.java:64)
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.

这种错误,但是这种错误查起来比较困难,还要请教一下高手调试这种错误的经验。
报错中的:db.DBConnect.executeQuery(DBConnect.java:61)
org.apache.jsp.orabean_jsp._jspService(orabean_jsp.java:64)
我分别查过了,分别是bean中的rs=stmt.executeQuery(sql)和JSP页面中的ResultSet rs=myBean.executeQuery(strSql),同样的位置,但是还是看不出错在哪里?郁闷中!
源码如下:
yhb72 2004-09-03
  • 打赏
  • 举报
回复
漏掉了:lguo_714(凌)和nchln(打倒传奇)说的没错,做返回记录集的查询后不能关闭链接。

nchln 2004-09-03
  • 打赏
  • 举报
回复
conn.close();
stmt.close();
这两行去掉。如果关闭连接的话,rs就会返回空。
gqlao 2004-09-03
  • 打赏
  • 举报
回复
我猜是因为有可能这个uname接收过来的值为null的情况下执行uname.length()会报错?是不是这样写比较严密一点:
if ((uname != null) && (uname.length() != 0))
{
......
}
yhb72 2004-09-03
  • 打赏
  • 举报
回复
谢谢大家!ikevin(菜无心)说的没错,第58行的代码是:if (uname.length()!=0),但是我怎么也看不出有什么语法错误,难道JSP里面判断长度不是用length函数?我也是刚学JSP。不知道大家知不知道哪里有JSP函数手册可以下载的?我把JSP文件中对用户名和口令长度的判断去掉后重新运行就OK了!所以还是JSP页面的语法问题。

binligen 2004-09-03
  • 打赏
  • 举报
回复
sorry,应该是:
String sConnStr ="jdbc:microsoft:sqlserver://localhost:1433,sa,123456";
binligen 2004-09-03
  • 打赏
  • 举报
回复
String sConnStr ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=ims;user=sa;password=123456";

换成:
String sConnStr ="jdbc:microsoft:sqlserver://localhost:1433,ims;user=sa,123456";
试试
lguo_714 2004-09-03
  • 打赏
  • 举报
回复
去掉rs.close();看看行不行!
lguo_714 2004-09-03
  • 打赏
  • 举报
回复
顺便引入<%@ page import="db.DBConnect"%>
lguo_714 2004-09-03
  • 打赏
  • 举报
回复
你试一下在JSP页面中用try{}catch(Exception e){ }扑捉一下异常!
一点晴 2004-09-02
  • 打赏
  • 举报
回复


在TOMCAT下的work目录下,找到testbean_jsp.java这个文件,看看第58行!!!
yhb72 2004-09-02
  • 打赏
  • 举报
回复
这三个都拷过去了的,单独在JSP里面写数据库连接,做查询都没问题的。但写在BEAN里面,JSP调用就报上面那个错了!

to jimsons(路漫漫其修远兮):你那里有没有在你机器上调试成功的bean和JSP测试程序,发给我一下吧,邮箱:yhb72@sohu.com,我在我机器上试试。
jimsons 2004-09-02
  • 打赏
  • 举报
回复
我的环境也是windows2003+sql server2000(sp3)+tomcat5.0
我一开始没装sp3时也会发生错误,装了之后就正常了,有时连接不上,重启一下SQL服务器就可以了
你有没有将Microsoft SQL Server 2000 Driver for JDBC那三个文件拷到tomcat\common\lib下
yhb72 2004-09-02
  • 打赏
  • 举报
回复
在2003下SQL SERVER必须打SP3才能连接,我做了另一段程序从sql server中做增加、删除、查询都是正常的,的确是用1433端口。

重启SQL SERVER服务器还是一样的。

郁闷中。。。。我还写了另外一段测试代码,连oracle数据库的,报错和这个一样。我也翻过不少类似的帖子,好像还没有解决方案。
jimsons 2004-09-02
  • 打赏
  • 举报
回复
重启一下SQL Server服务器,
加载更多回复(7)

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧