這次寫的是關於JSP中調用JavaBean實現數據庫連接和操作!
首先是JavaBean的原程序
//——————————————————————
package mywebconn;
import java.sql.*;
public class JDBCrs {
//
static final String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
static final String sConnStr="jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=D:/myWeb01/WebModule1/test.mdb";
Connection conn=null;
ResultSet rs=null;
public JDBCrs() {
try {
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e) {
System.err.println(e.getMessage());
}
}
// the public
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;
}
public void executeUpdate(String sql) {
try {
conn = DriverManager.getConnection(sConnStr);
Statement stmt = conn.createStatement();
stmt.executeUpdate(sql);
conn.close();
}
catch(SQLException ex) {
System.err.println("aq.executeUpdate "+ex.getMessage());
}
}
public void close() {
try {
if (!conn.isClosed()) {
conn.close();
}
} catch (SQLException ex) {
System.err.println("conn.close "+ex.getMessage());
}
}
}
//——————————————————————