使用javabeans连接数据库500报错
我在使用javabeans连接数据库时,老是出现500报错.请各位高手指点,小弟万分感激.
我的bean的内容如下;
/**
* 创建日期 2007-4-17
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package dd;
import java.sql.*;
/**
* @author Administrator
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class DBContext{
String CLASSFORNAME= "com.microsoft.jdbc.sqlserver.SQLServerDrive ";
String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test ";
String user= "sa ";
String password= "123 ";
Connection con=null;
ResultSet rs=null;
public DBContext(){
try{
Class.forName(CLASSFORNAME);
}
catch(Exception e){
System.err.println( "DBContext(): "+e.getMessage());
}
}
public ResultSet executeQuery(String sql){
rs=null;
try
{
con=DriverManager.getConnection(url,user,password);
Statement stmt=con.createStatement();
rs=stmt.executeQuery(sql);
}catch(SQLException e){
System.err.println( "DBContext.executeQuery: "+e.getMessage());
}
return rs;
}
}
jsp页面的内容如下:
<%@ page contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* "%>
<html>
<body>
<%@ page import= "dd.DBContext "%>
<jsp:useBean id= "S " class= "dd.DBContext " scope= "session " />
<%
String sql= "select * from test ";
ResultSet tt=S.executeQuery(sql);
while(tt.next()) {
out.println( "您的第一个字段内容为: "+tt.getString( "name "));
out.println( "您的第二个字段内容为: "+tt.getString( "password "));
}%>
<%out.print( "数据库操作成功,恭喜你 ");%>
</body>
</html>
报错内容如下:
HTTP Status 500 -
--------------------------------------------
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:372)
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:802)
root cause
java.lang.NullPointerException
org.apache.jsp.index_jsp._jspService(index_jsp.java:62)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
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:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
--------------------------------------------
Apache Tomcat/5.0.28
[解决办法]
很可能是没有取到连接
Conn为空了
[解决办法]
con=DriverManager.getConnection(url,user,password);
这条语句应该放在Class.forName(CLASSFORNAME);的下面.
[解决办法]
java.lang.NullPointerException
空指针错误
看看DBContext打印的信息是什么
[解决办法]
确定驱动程序拷贝到 web-inf\lib下了?
String CLASSFORNAME= "com.microsoft.jdbc.sqlserver.SQLServerDrive ";
少个r
String CLASSFORNAME= "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html