JSP使用javabean查询数据库
jsp代码<%@pagelanguage="java"contentType="text/html;charset=gb2312"%><%@pageimport="jav...
jsp代码
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>Test Bean</title>
</head>
<body>
<jsp:useBean id="jdbc" class="jdbc.JDBCBean" scope="page" />
<%
String sql="select * from student ";
ResultSet rs=jdbc.executeQuery(sql);
while(rs.next())
{
out.println(rs.getString("id")+"<br/>");
}
%>
catch(Exception e)
{}
</body>
</html>
javabean代码
package jdbc;
import java.sql.*;
public class JDBCBean
{
String dbDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=shopData";
String username="sa";
String password="";
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
//实例化,装载JDBC驱动程序
public JDBCBean()
{
try
{
Class.forName(dbDriver);
}
catch(ClassNotFoundException e){}
}
//返回查询后的数据集
public ResultSet executeQuery(String sql)
{
try
{
con=DriverManager.getConnection(url,username,password);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
}
catch(SQLException e){}
return rs;
}
//关闭数据库
public void closeDB()
{
try{
rs.close();
con.close();
}
catch(SQLException e) {}
}
}
错误代码
org.apache.jasper.JasperException: An exception occurred processing JSP page /aaa.jsp at line 12
9: <%
10: String sql="select * from student ";
11: ResultSet rs=jdbc.executeQuery(sql);
12: while(rs.next())
13: {
14: out.println(rs.getString("id")+"<br/>");
15: } 展开
<%@ page language="java" contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<head>
<title>Test Bean</title>
</head>
<body>
<jsp:useBean id="jdbc" class="jdbc.JDBCBean" scope="page" />
<%
String sql="select * from student ";
ResultSet rs=jdbc.executeQuery(sql);
while(rs.next())
{
out.println(rs.getString("id")+"<br/>");
}
%>
catch(Exception e)
{}
</body>
</html>
javabean代码
package jdbc;
import java.sql.*;
public class JDBCBean
{
String dbDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=shopData";
String username="sa";
String password="";
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
//实例化,装载JDBC驱动程序
public JDBCBean()
{
try
{
Class.forName(dbDriver);
}
catch(ClassNotFoundException e){}
}
//返回查询后的数据集
public ResultSet executeQuery(String sql)
{
try
{
con=DriverManager.getConnection(url,username,password);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
}
catch(SQLException e){}
return rs;
}
//关闭数据库
public void closeDB()
{
try{
rs.close();
con.close();
}
catch(SQLException e) {}
}
}
错误代码
org.apache.jasper.JasperException: An exception occurred processing JSP page /aaa.jsp at line 12
9: <%
10: String sql="select * from student ";
11: ResultSet rs=jdbc.executeQuery(sql);
12: while(rs.next())
13: {
14: out.println(rs.getString("id")+"<br/>");
15: } 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询