当前位置: 技术问答>java相关
调用连接池?
来源: 互联网 发布时间:2017-03-26
本文导语: 我用servlet建立一个连接池,怎样在其他的jsp页面使用连接池的连接,请说的具体一点 | servlet连接池的例子 *************************************** import javax.servlet.* ; import javax.servlet.http.* ; import ...
我用servlet建立一个连接池,怎样在其他的jsp页面使用连接池的连接,请说的具体一点
|
servlet连接池的例子
***************************************
import javax.servlet.* ;
import javax.servlet.http.* ;
import java.io.* ;
import java.sql.* ;
import java.util.Vector;
import oracle.jdbc.driver.*;
import java.util.Enumeration;
import java.util.Properties;
import com.unitech.connectionpool.* ;
public class dbTest extends HttpServlet {
//Initialize global variables
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
// 数据库连接:Connetcion conn = null ;
Connection conn = null ;
//数据库查询内容执行:Statement stment = null ;
Statement stment = null ;
// 数据库连接池的初始化
DBConnectionManager connMgr = DBConnectionManager.getInstance();
//初始化数据库连接池,并且获取数据库连接
private void initDatabaseDriver () {
conn = connMgr.getConnection("oracle");
if (conn == null) {
System.out.println("数据库连接失败。");
return;
}
try {
stment = conn.createStatement();
}
catch (SQLException e) {
e.printStackTrace() ;
}
}
//释放数据库连接
private void freeConnectionPool() {
connMgr.freeConnection("oracle", conn) ;
}
//获取记录集,并返回给VERTOR V
public Vector getForumList() {
String[] s = {"","","","",""} ;//与选取的列数相等。
Vector v = new Vector() ;
this.initDatabaseDriver();
try{
String queryStr = null ;
queryStr = "SELECT BBS_ID,BBS_NAME,DESCRIPTION,MANAGER_ID, CREATE_DATE FROM BBS WHERE IS_SYSTEM='0' ORDER BY CREATE_DATE DESC" ;
ResultSet rSet = stment.executeQuery(queryStr) ;
while (rSet.next()) {
s[0] = Integer.toString(rSet.getInt("BBS_ID")) ;
s[1] = rSet.getString("BBS_NAME") ;
s[2] = rSet.getString("DESCRIPTION") ;
s[3] = rSet.getString("MANAGER_ID") ;
Timestamp createdate = rSet.getTimestamp("CREATE_DATE") ;
String tmp = createdate.toString() ;
s[4] = tmp.substring(0,(tmp.length()-2)) ;
v.addElement(s.clone());
}
rSet.close();
stment.close();
this.freeConnectionPool();
}
catch(Exception e) {
try {
stment.close();
this.freeConnectionPool();
}
catch(SQLException ee) {
ee.printStackTrace();
}
e.printStackTrace() ;
}
return v ;
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = new PrintWriter (response.getOutputStream());
response.setContentType("text/html");
out.println("");
out.println("The servlet has received a GET. This is the reply.");
out.println("");
out.println("");
// 将记录集循环输出到页面。
Vector v = new Vector() ;
v = this.getForumList() ;
for (int i=0; i"
+ ""+s[0]+""
+ ""+s[1]+""
+ ""+s[2]+""
+ ""+s[3]+""
+ ""+s[4]+"");
}
out.println("");
out.close();
}
}
***************************************
import javax.servlet.* ;
import javax.servlet.http.* ;
import java.io.* ;
import java.sql.* ;
import java.util.Vector;
import oracle.jdbc.driver.*;
import java.util.Enumeration;
import java.util.Properties;
import com.unitech.connectionpool.* ;
public class dbTest extends HttpServlet {
//Initialize global variables
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
// 数据库连接:Connetcion conn = null ;
Connection conn = null ;
//数据库查询内容执行:Statement stment = null ;
Statement stment = null ;
// 数据库连接池的初始化
DBConnectionManager connMgr = DBConnectionManager.getInstance();
//初始化数据库连接池,并且获取数据库连接
private void initDatabaseDriver () {
conn = connMgr.getConnection("oracle");
if (conn == null) {
System.out.println("数据库连接失败。");
return;
}
try {
stment = conn.createStatement();
}
catch (SQLException e) {
e.printStackTrace() ;
}
}
//释放数据库连接
private void freeConnectionPool() {
connMgr.freeConnection("oracle", conn) ;
}
//获取记录集,并返回给VERTOR V
public Vector getForumList() {
String[] s = {"","","","",""} ;//与选取的列数相等。
Vector v = new Vector() ;
this.initDatabaseDriver();
try{
String queryStr = null ;
queryStr = "SELECT BBS_ID,BBS_NAME,DESCRIPTION,MANAGER_ID, CREATE_DATE FROM BBS WHERE IS_SYSTEM='0' ORDER BY CREATE_DATE DESC" ;
ResultSet rSet = stment.executeQuery(queryStr) ;
while (rSet.next()) {
s[0] = Integer.toString(rSet.getInt("BBS_ID")) ;
s[1] = rSet.getString("BBS_NAME") ;
s[2] = rSet.getString("DESCRIPTION") ;
s[3] = rSet.getString("MANAGER_ID") ;
Timestamp createdate = rSet.getTimestamp("CREATE_DATE") ;
String tmp = createdate.toString() ;
s[4] = tmp.substring(0,(tmp.length()-2)) ;
v.addElement(s.clone());
}
rSet.close();
stment.close();
this.freeConnectionPool();
}
catch(Exception e) {
try {
stment.close();
this.freeConnectionPool();
}
catch(SQLException ee) {
ee.printStackTrace();
}
e.printStackTrace() ;
}
return v ;
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = new PrintWriter (response.getOutputStream());
response.setContentType("text/html");
out.println("");
out.println("The servlet has received a GET. This is the reply.");
out.println("");
out.println("");
// 将记录集循环输出到页面。
Vector v = new Vector() ;
v = this.getForumList() ;
for (int i=0; i"
+ ""+s[0]+""
+ ""+s[1]+""
+ ""+s[2]+""
+ ""+s[3]+""
+ ""+s[4]+"");
}
out.println("");
out.close();
}
}
|
jsp调用连接池的例子
http://www.51jsp.net/InfoView/Info.asp?ID=1035
http://www.51jsp.net/InfoView/Info.asp?ID=1035