试试这个,我自己用的。
package hall;import java.util.HashMap;
import java.util.Vector;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;/**
* <p>Title: </p>
* <p>Description: test</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author nighthawk
*/public class ConnectionPool { //连接池的管理器,首先初始化,仅仅有一个对象,管理连接池
private static HashMap connectionPoolManager = new HashMap();
//没有用过的连接池,用vector实现同步
private static Vector noUseConnectionPool= new Vector();
//没有用过的连接池
private static HashMap nowUseConnectionPool = new HashMap(); private static String dbDriver = "oracle.jdbc.driver.OracleDriver";
private static String dbUrl = "jdbc:oracle:thin:@localhost:1521:db";
private static String userName = "yourname";
private static String userPassword = "yourpassword"; //默认为100个连接池
private static int MAX_POOL = 100; //singleTon 设计模式
private ConnectionPool(String driver, String url, String name,
String password, int max) throws
ClassNotFoundException {
Class.forName(driver);
dbUrl = url;
userName = name;
userPassword = password;
MAX_POOL = max;
} public static ConnectionPool getConnManagerInstance(String poolName) throws
ClassNotFoundException {
ConnectionPool tempPool = (ConnectionPool) connectionPoolManager.get(
poolName);
if (tempPool == null) {
tempPool = new ConnectionPool(dbDriver, dbUrl, userName, userPassword,
MAX_POOL);
connectionPoolManager.put(poolName, tempPool);
return tempPool;
}
else {
return tempPool;
}
} //通过连接池获得真正的链接
public static Connection getConnection() throws java.sql.SQLException {
Connection conn = null;
synchronized (noUseConnectionPool) {
if (noUseConnectionPool.size() > 0) {
conn = (Connection) noUseConnectionPool.firstElement();
noUseConnectionPool.remove(conn);
return conn;
}
}
//如果数据库连接池没有链接了,自己创建一个
if (conn == null) {
conn = createConnection(dbDriver, dbUrl, userName, userPassword);
}
else if (conn.isClosed()) {
nowUseConnectionPool.remove(conn);
conn = createConnection(dbDriver, dbUrl, userName, userPassword);
}
conn.setAutoCommit(false);
nowUseConnectionPool.put(conn, conn);
return conn;
} //如果连接池没有链接了,就需要产生一个链接
private static Connection createConnection(String driver, String url,
String user, String password) throws
java.sql.SQLException {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} public static void releaseConnection(Connection conn, boolean isCommit) throws
java.sql.SQLException {
if (isCommit) {
conn.commit();
}
else {
conn.rollback();
}
nowUseConnectionPool.remove(conn);
if (noUseConnectionPool.size() + nowUseConnectionPool.size() < MAX_POOL) {
synchronized (noUseConnectionPool) {
noUseConnectionPool.add(conn);
}
}
else {
conn.close();
}
}
}
package hall;import java.util.HashMap;
import java.util.Vector;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;/**
* <p>Title: </p>
* <p>Description: test</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author nighthawk
*/public class ConnectionPool { //连接池的管理器,首先初始化,仅仅有一个对象,管理连接池
private static HashMap connectionPoolManager = new HashMap();
//没有用过的连接池,用vector实现同步
private static Vector noUseConnectionPool= new Vector();
//没有用过的连接池
private static HashMap nowUseConnectionPool = new HashMap(); private static String dbDriver = "oracle.jdbc.driver.OracleDriver";
private static String dbUrl = "jdbc:oracle:thin:@localhost:1521:db";
private static String userName = "yourname";
private static String userPassword = "yourpassword"; //默认为100个连接池
private static int MAX_POOL = 100; //singleTon 设计模式
private ConnectionPool(String driver, String url, String name,
String password, int max) throws
ClassNotFoundException {
Class.forName(driver);
dbUrl = url;
userName = name;
userPassword = password;
MAX_POOL = max;
} public static ConnectionPool getConnManagerInstance(String poolName) throws
ClassNotFoundException {
ConnectionPool tempPool = (ConnectionPool) connectionPoolManager.get(
poolName);
if (tempPool == null) {
tempPool = new ConnectionPool(dbDriver, dbUrl, userName, userPassword,
MAX_POOL);
connectionPoolManager.put(poolName, tempPool);
return tempPool;
}
else {
return tempPool;
}
} //通过连接池获得真正的链接
public static Connection getConnection() throws java.sql.SQLException {
Connection conn = null;
synchronized (noUseConnectionPool) {
if (noUseConnectionPool.size() > 0) {
conn = (Connection) noUseConnectionPool.firstElement();
noUseConnectionPool.remove(conn);
return conn;
}
}
//如果数据库连接池没有链接了,自己创建一个
if (conn == null) {
conn = createConnection(dbDriver, dbUrl, userName, userPassword);
}
else if (conn.isClosed()) {
nowUseConnectionPool.remove(conn);
conn = createConnection(dbDriver, dbUrl, userName, userPassword);
}
conn.setAutoCommit(false);
nowUseConnectionPool.put(conn, conn);
return conn;
} //如果连接池没有链接了,就需要产生一个链接
private static Connection createConnection(String driver, String url,
String user, String password) throws
java.sql.SQLException {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} public static void releaseConnection(Connection conn, boolean isCommit) throws
java.sql.SQLException {
if (isCommit) {
conn.commit();
}
else {
conn.rollback();
}
nowUseConnectionPool.remove(conn);
if (noUseConnectionPool.size() + nowUseConnectionPool.size() < MAX_POOL) {
synchronized (noUseConnectionPool) {
noUseConnectionPool.add(conn);
}
}
else {
conn.close();
}
}
}
Connection conn = ConnectionPool.getConnection();
然后通过
Statement stmt = conn.createStatement();
再
ResultSet rs = stmt.executeQuery("select * from yourtable");
得到rs后
再while(rs.next){
在循环里面把rs里面的内容取出来,他就是数据库里的内容
rs.getString("yourcolumn");}
最后关闭rs;
rs.close();
stmt.close();
conn.close();
注意这些都要扑捉异常。具体的你自己去实现,印象会更深。
poolman性能怎么样,好用吗?有用过poolman的说说
private static String dbDriver = "oracle.jdbc.driver.OracleDriver";
private static String dbUrl = "jdbc:oracle:thin:@localhost:1521:db";
private static String userName = "yourname";
private static String userPassword = "yourpassword";
都放到一个文本文件中可能更好。
还想请教你:如果我想把整个它做一个war文件你会怎么做?前提是可移植性强
好象oracle已经提供了数据库连接池可以查看oracle的jdbc,里面应该有的