使用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