主题:请教jsp+javabean的问题
使用mydb.java制成的javabean
以下是mydb.java的源程序。
--mydb.java----------------------------
package mydb;
import java.sql.*;
public class mydb {
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:sunshine";
Connection conn = null;
ResultSet rs = null;
public mydb() {
try {
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e) {
System.err.println("mydb(): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null; //
try {
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
}
//-------------------------------------------------------
然后在Jsp页面中调用该JavaBean,源码如下
<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="RegisterBean" scope="page" class="mydb.mydb" />
<%
String sql="select * from user";
ResultSet rs = RegisterBean.executeQuery(sql);
if(rs.next()) {
rs.close();
RegisterBean.closeStmt();
}
%>
上面是网上看到的例子可是我运行事RegisterBean.closeStmt();不能实现 是怎么回事??
以下是mydb.java的源程序。
--mydb.java----------------------------
package mydb;
import java.sql.*;
public class mydb {
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:sunshine";
Connection conn = null;
ResultSet rs = null;
public mydb() {
try {
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e) {
System.err.println("mydb(): " + e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs = null; //
try {
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
return rs;
}
}
//-------------------------------------------------------
然后在Jsp页面中调用该JavaBean,源码如下
<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="RegisterBean" scope="page" class="mydb.mydb" />
<%
String sql="select * from user";
ResultSet rs = RegisterBean.executeQuery(sql);
if(rs.next()) {
rs.close();
RegisterBean.closeStmt();
}
%>
上面是网上看到的例子可是我运行事RegisterBean.closeStmt();不能实现 是怎么回事??