Bean 教本
/************************************************
Java 连接数据库 Java Bean
@author:Seaman Jiang
Last Modified:23,Apr,2004
*************************************************/
package jwsystem;
import java.sql.*;
import java.util.*;
import java.io.*;
import java.sql.*;
/*************************************************
dbManager 类进行数据库连接的管理
*************************************************/
public class dbManager {
/**
* 定义数据库连接的常量
*/
private String myURL = "jdbc:oracle:oci8:@";
private String dbuser = "wwwwork";
private String dbpassword = "123";
protected Connection conn = null; //Connection
protected Statement stmt = null; //Statement
protected ResultSet rest = null; //resultset
/**
* 连接数据库
*/
public void makeConnection() throws Exception {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection(myURL,dbuser,dbpassword);
}
/**
* 返回记录集
*/
public ResultSet RS_Query(String sql) throws Exception{
rest = null;
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rest = stmt.executeQuery(sql);
return rest;
}
/**
* 执行更新,返回操作状态;
*/
public int RS_Execute(String sql) throws Exception{
int success_update=0;
rest = null;
Statement stmt = conn.createStatement();
success_update=stmt.executeUpdate(sql);
return success_update;
}
/**
* 清除连接
*/
public void takeDown() throws Exception {
conn.close();
}
}
connect.jsp
<jsp:useBean id='objDB' scope='request' class='jwsystem.dbManager'/>
<%
objDB.makeConnection();
%>
为什么老出现Native Library C:/OraHome/bin/ocijdbc8.dll already loaded in another classloader 只有第一次能运行成功~~~
应该改成如下:
public class dbManager {
/**
* 定义数据库连接的常量
*/
private String myURL = "jdbc:oracle:oci8:@";
private String dbuser = "wwwwork";
private String dbpassword = "123";
protected Connection conn = null; //Connection
protected Statement stmt = null; //Statement
protected ResultSet rest = null; //resultset
static
{
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
}
/**
* 连接数据库
*/
public void makeConnection() throws Exception {
conn = DriverManager.getConnection(myURL,dbuser,dbpassword);
//注意这一句和DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());或者Class.forName(strDBDriver).newInstance();要分开写,不能写在一起
}
参考:http://search.csdn.net/Expert/topic/1704/1704531.xml?temp=.3021204
另外注意:Oracle中的表名是区分大小写的。