是这样子的,我在网上找了一个用java语言写的数据库连接池,我想在web项目中使用它,该怎么配置?或者说该怎么使用??
一直想不明白,不知道该如何下手!
在main函数里,数据库连接池是这样使用的,如下所示: public static void main(String args[]) {
ConnectionPool connpool = null;
try {
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url =
"jdbc:microsoft:sqlserver://localhost:2433;DatabaseName=pim;";
String user = "sync4j";
String pwd = "sync4j";
connpool = new ConnectionPool(driver, url, user, pwd);
connpool.setInitialConnections(1);
connpool.setIncrementalConnections(1);
connpool.setMaxConnections(1);
connpool.createPool();
Connection conn = connpool.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from sync4j_id");
while (rs.next()) {
System.out.println(rs.getString(1));
}
connpool.returnConnection(conn);
connpool.closeConnectionPool();
}
catch (Exception e) {
e.printStackTrace();
}
}可是在web程序中,我该将它写到哪里,才可以使各个jsp或servlet都可以直接调用getConnection()获得数据库连接呢,我的初始化的代码又应该写到哪里呢??
望各位大哥指教
谢谢!!!!!!!
一直想不明白,不知道该如何下手!
在main函数里,数据库连接池是这样使用的,如下所示: public static void main(String args[]) {
ConnectionPool connpool = null;
try {
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url =
"jdbc:microsoft:sqlserver://localhost:2433;DatabaseName=pim;";
String user = "sync4j";
String pwd = "sync4j";
connpool = new ConnectionPool(driver, url, user, pwd);
connpool.setInitialConnections(1);
connpool.setIncrementalConnections(1);
connpool.setMaxConnections(1);
connpool.createPool();
Connection conn = connpool.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from sync4j_id");
while (rs.next()) {
System.out.println(rs.getString(1));
}
connpool.returnConnection(conn);
connpool.closeConnectionPool();
}
catch (Exception e) {
e.printStackTrace();
}
}可是在web程序中,我该将它写到哪里,才可以使各个jsp或servlet都可以直接调用getConnection()获得数据库连接呢,我的初始化的代码又应该写到哪里呢??
望各位大哥指教
谢谢!!!!!!!
我是想使用自己用代码写的数据库连接池类,不知道该怎么办??
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url =
"jdbc:microsoft:sqlserver://localhost:2433;DatabaseName=pim;";
String user = "sync4j";
String pwd = "sync4j";
ConnectionPool connpool = null;
Connection conn = null;
//获取一个连接
public static Connection getConnection(){
connpool = new ConnectionPool(driver, url, user, pwd);
connpool.setInitialConnections(1);
connpool.setIncrementalConnections(1);
connpool.setMaxConnections(1);
connpool.createPool();
conn = connpool.getConnection();
return conn;
}//将资源返回给连接池
public static void returnPoll(){
this.connpool.returnConnection(this.conn);
this.connpool.closeConnectionPool();
}
}使用的时候你只需要DBConnectionPoll.getConnection()就可以获得一个连接,然后利用这个连接进行数据库操作。
部分代码如下:
//import 相关的包
public class test{
public ArrayList getInfo(){
Connection conn = DBConnectionPoll.getConnection();//获得连接
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from sync4j_id");
while (rs.next()) {
System.out.println(rs.getString(1));
}
DBConnectionPoll.returnPoll();//返回给连接池
}
}
package yeahi.cxt.common;
import java.sql.*;public class DBConnection {
public static Connection getConnection() {
Connection conn = null;
String username = PropertyFile.getValue("username");
String password = PropertyFile.getValue("password");
String url = PropertyFile.getValue("url");
String driver = PropertyFile.getValue("driver"); try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException ex) {
ex.printStackTrace();
System.out.println("数据库连接出现异常");
} catch (ClassNotFoundException ex) {
System.out.println("加载数据库驱动异常");
}
return conn;
}
}
在程序中使用上面这个类返回的数据库连接
package yeahi.cxt.execute;import yeahi.cxt.common.DBConnection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.sql.PreparedStatement;
import java.sql.Connection;
import yeahi.cxt.beans.DomainBean;/**
* <p>Title: </p>
*
* <p>Description: cnnic接口</p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: 苏州网路神电子技术商务有限公司</p>
*
* @author 网路神技术部
* @version 1.0
*/
public class DomainManager {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
private PreparedStatement pstmt = null;
public DomainManager() {
} public ArrayList getDomain() {
ArrayList al = new ArrayList();
conn = DBConnection.getConnection();
try {
String sql =
"select * from domain where statu=0 and user_id=88 order by level desc,id asc";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
DomainBean db = new DomainBean();
db.setId(rs.getString("id"));
db.setDomain_name(rs.getString("domain_name"));
db.setOutdate(rs.getString("outdate"));
db.setLevel(rs.getString("level"));
db.setStatu(rs.getString("statu"));
db.setRO(rs.getString("RO"));
db.setRO(rs.getString("new_outdate"));
al.add(db);
}
} catch (SQLException ex) {
ex.printStackTrace();
System.out.println("数据库查询操作异常。");
} finally {
try {
rs.close();
stmt.close();
conn.close();
} catch (SQLException ex1) {
System.out.println("数据库关闭操作异常");
}
}
return al;
}
}
...
ConnBean.java
PoolBean.java
testPool.jsptestPool.jsp文件代码如下:<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="java.sql.*,java.util.*"%>
<jsp:useBean id="pool" scope="application" class="zwf.PoolBean"/>
<%
Connection con=null;
try{
if(pool.getDriver()==null){
pool.setDriver("sun.jdbc.odbc.JdbcOdbcDriver");
pool.setURL("jdbc:odbc:jrun");
pool.setSize(5);
pool.initializePool();
}
con=pool.getConnection();
java.sql.Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from user where userid='zwf'");%>
<html>
<head>
<title>testPool</title>
</head>
<body>
<%=rs.getString("dbusername")%> <br/>
</body>
</html>
<%
rs.close();
pool.releaseConnection(con);
}
catch(Exception e){
out.println(e.getMessage());
}
%>
ConnBean.java/*
* Created on 2004-12-6
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*//**
* @author zwf
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/package zwf;
import java.io.Serializable;
import java.sql.*;public class ConnBean {
private Connection conn = null;private boolean inuse = false;public ConnBean() {}public ConnBean(Connection con) {
if (con != null)
conn = con;
}public Connection getConnection() {
return conn;
}public void setConnection(Connection con) {
conn = con;
}public void setInuse(boolean inuse) {
this.inuse = inuse;
}public boolean getInuse() {
return inuse;
}public void close() {
try {
conn.close();
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}PoolBean.java/*
* Created on 2004-12-6
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*//**
* @author zwf
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/package zwf;
import java.io.Serializable;
import java.sql.*;
import java.util.*;
public class PoolBean implements java.io.Serializable {
private String driver = null;private String url = null;private int size = 0;private String username = "";private String password = "";private ConnBean connBean = null;private Vector pool = null;public PoolBean() {}public void setDriver(String d) {
if (d != null)
driver = d;
}public String getDriver() {
return driver;
}public void setURL(String u) {
if (u != null)
url = u;
}public String getURL() {
return url;
}public void setSize(int s) {
if (s > 1)
size = s;
}public int getSize() {
return size;
}public void setUserName(String un) {
if (un != null)
username = un;
}public String getUserName() {
return username;
}public void setPassword(String pw) {
if (pw != null)
password = pw;
}public String getPassword() {
return password;
}public void setConnBean(ConnBean cb) {
if (cb != null)
connBean = cb;
}public ConnBean getConnBean() {
Connection con = connBean.getConnection();
ConnBean cb = new ConnBean(con);
cb.setInuse(true);
return cb;
}private Connection createConnection() throws Exception {
Connection con = null;
con = DriverManager.getConnection(url, username, password);
return con;
}public synchronized void initializePool() throws Exception {
if (driver == null)
throw new Exception("没有提供驱动程序名称!");
if (url == null)
throw new Exception("没有提供URL");
if (size < 1)
throw new Exception("连接池大小小于1");
try {
Class.forName(driver);
for (int i = 0; i < size; i++) {
Connection con = createConnection();
if (con != null) {
ConnBean connBean = new ConnBean(con);
addConnection(connBean);
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
throw new Exception(e.getMessage());
}
}private void addConnection(ConnBean connbean) {
if (pool == null)
pool = new Vector(size);
pool.addElement(connbean);
}public synchronized void releaseConnection(Connection con) {
for (int i = 0; i < pool.size(); i++) {
ConnBean connBean = (ConnBean) pool.elementAt(i);
if (connBean.getConnection() == con) {
System.out.println("释放第" + i + "个连接");
connBean.setInuse(false);
break;
}
}
}public synchronized Connection getConnection() throws Exception {
ConnBean connBean = null;
for (int i = 0; i < pool.size(); i++) {
connBean = (ConnBean) pool.elementAt(i);
if (connBean.getInuse() == false) {
connBean.setInuse(true);
Connection con = connBean.getConnection();
return con;
}
}
try {
Connection con = createConnection();
connBean = new ConnBean(con);
connBean.setInuse(true);
pool.addElement(connBean);
} catch (Exception e) {
System.out.println(e.getMessage());
throw new Exception(e.getMessage());
}
return connBean.getConnection();
}public synchronized void emptyPool() {
for (int i = 0; i < pool.size(); i++) {
System.err.println("关闭第" + i + "个JDBC连接");
ConnBean connBean = (ConnBean) pool.elementAt(i);
if (connBean.getInuse() == false)
connBean.close();
else {
try {
java.lang.Thread.sleep(20000);
connBean.close();
} catch (InterruptedException ie) {
System.err.println(ie.getMessage());
}
}
}
}
}
你给的代码正是我想要的东东
多谢!!!