jsp页面使用javabean连接数据库报错!!是javabean有问题吗????拜托了,帮帮忙。
javabean如下:packagereg;importjava.sql.*;publicclassMyDbBean{privateStatementstmt=null;...
javabean如下:
package reg;
import java.sql.*;
public class MyDbBean
{
private Statement stmt = null;
ResultSet rs = null;
private Connection conn = null;
private String dsn;
//构造函数
public MyDbBean() { }
//根据dsn参数,加载驱动程序,建立连接
public void getConn(String dbname, String uid, String pwd) throws
Exception
{
try
{
dsn =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=" +dbname;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
conn = DriverManager.getConnection(dsn, uid, pwd);
}
catch (Exception ex)
{
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}
//执行查询类的SQL语句,有返回集
public ResultSet executeQuery1(String sql)
{
rs = null;
try
{
stmt =conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}
//执行更新类的SQL语句,无返回集
public void executeUpdate2(String sql)
{
stmt = null;
rs=null;
try
{
stmt =
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}
//关闭对象
public void closeStmt()
{
try{ stmt.close(); }
catch(SQLException ex)
{
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}
public void closeConn()
{
try{ conn.close(); }
catch(SQLException ex)
{
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}
}
在jsp页面连接数据库后报错如下:
org.apache.jasper.JasperException: An exception occurred processing JSP page /test2.jsp at line 18
15: <%
16: test2.getConn("zhaopin","sa","1");
17: sql="select * from users";
18: ResultSet rs=test2.executeQuery1(sql);
19:
20: while(rs.next()) {%>
21: 您的第一个字段内容为:<%=rs.getString(1)%> 展开
package reg;
import java.sql.*;
public class MyDbBean
{
private Statement stmt = null;
ResultSet rs = null;
private Connection conn = null;
private String dsn;
//构造函数
public MyDbBean() { }
//根据dsn参数,加载驱动程序,建立连接
public void getConn(String dbname, String uid, String pwd) throws
Exception
{
try
{
dsn =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=" +dbname;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
conn = DriverManager.getConnection(dsn, uid, pwd);
}
catch (Exception ex)
{
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}
//执行查询类的SQL语句,有返回集
public ResultSet executeQuery1(String sql)
{
rs = null;
try
{
stmt =conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery:"+ex.getMessage());
}
return rs;
}
//执行更新类的SQL语句,无返回集
public void executeUpdate2(String sql)
{
stmt = null;
rs=null;
try
{
stmt =
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
stmt.executeQuery(sql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}
//关闭对象
public void closeStmt()
{
try{ stmt.close(); }
catch(SQLException ex)
{
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}
public void closeConn()
{
try{ conn.close(); }
catch(SQLException ex)
{
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}
}
在jsp页面连接数据库后报错如下:
org.apache.jasper.JasperException: An exception occurred processing JSP page /test2.jsp at line 18
15: <%
16: test2.getConn("zhaopin","sa","1");
17: sql="select * from users";
18: ResultSet rs=test2.executeQuery1(sql);
19:
20: while(rs.next()) {%>
21: 您的第一个字段内容为:<%=rs.getString(1)%> 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询