如何在java项目中使用proxool连接池? 在网上找的教程都是针对web项目的,我现在需要在一个java项目中使用proxool连接池,求教。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1. If you adapting an existing application then the most transparent way to switch to Proxool is to simply change the URL you use to connect to the database. For example: 1.Connection connection = null; 2.try { 3. Class.forName("org.hsqldb.jdbcDriver"); 4. try { 5. connection = DriverManager.getConnection("jdbc:hsqldb:test"); 6. } catch (SQLException e) { 7. LOG.error("Problem getting connection", e); 8. } 9. 10. if (connection != null) {11. LOG.info("Got connection :)");12. } else {13. LOG.error("Didn't get connection, which probably means that no Driver accepted the URL");14. }15. 16.} catch (ClassNotFoundException e) {17. LOG.error("Couldn't find driver", e);18.} finally {19. try {20. // Check to see we actually got a connection before we21. // attempt to close it.22. if (connection != null) {23. connection.close();24. }25. } catch (SQLException e) {26. LOG.error("Problem closing connection", e);27. }28.}To start using Proxool, simply edit lines 3 and 5 (ed in blue): 1.Connection connection = null; 2.try { 3. Class.forName("org.logicalcobwebs.proxool.ProxoolDriver"); 4. try { 5. connection = DriverManager.getConnection("proxool.example:org.hsqldb.jdbcDriver:jdbc:hsqldb:test"); 6. } catch (SQLException e) { 7. LOG.error("Problem getting connection", e); 8. } 9. 10. if (connection != null) {11. LOG.info("Got connection :)");12. } else {13. LOG.error("Didn't get connection, which probably means that no Driver accepted the URL");14. }15. 16.} catch (ClassNotFoundException e) {17. LOG.error("Couldn't find driver", e);18.} finally {19. try {20. // Check to see we actually got a connection before we21. // attempt to close it.22. if (connection != null) {23. connection.close();24. }25. } catch (SQLException e) {26. LOG.error("Problem closing connection", e);27. }28.} 英文不好的话 再帮你翻译下我的新书刚上架,欢迎订购:《搜索引擎零距离—基于Ruby+Java搜索引擎原理与实现》 清华出版社。http://www.huachu.com.cn/itbook/itbookinfo.asp?lbbh=10105450 PS:在网上找的教程都是针对web项目的,我现在需要在一个java项目中使用proxool连接池,求教。web的SSH框架都有数据库连接配置java项目中使用连接池我昨天回答过相同问题帖出来吧 需要fscontext.jar和providerutil.jar,没有去Google下载package com.isoftstone.common;import java.sql.Connection;import java.sql.DriverManager;import java.util.Hashtable;import javax.naming.Context;import javax.naming.InitialContext;import oracle.jdbc.pool.OracleConnectionPoolDataSource;public class CreatePool { public static Connection getCon(){ Connection con = null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "tiger"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } public static void main(String[] args) { Connection conn =getCon(); if(null!=conn) { System.out.print("2--------"); } createPool(); Connection connection = getPool("jdbc/pool"); if(null!=connection){ System.out.print("2--------"); } } public static void createPool(){ Hashtable ht = new Hashtable(); ht.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); try { OracleConnectionPoolDataSource ods = new OracleConnectionPoolDataSource(); Context context = new InitialContext(ht); ods.setPassword("tiger"); ods.setURL("jdbc:oracle:thin:@127.0.0.1:1521:orcl"); ods.setUser("scott"); //ods.setLoginTimeout(arg0) ods.setDriverType("oracle.jdbc.driver.OracleDriver"); context.unbind("jdbc/pool"); context.bind("jdbc/pool", ods); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getPool(String name){ Connection con = null; Hashtable ht = new Hashtable(); ht.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); try { Context context = new InitialContext(ht); OracleConnectionPoolDataSource ods = (OracleConnectionPoolDataSource)context.lookup(name); con = ods.getConnection(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return con; } } 我这个是mysql,而且你的代码里没出现proxool啊 接口,类和对象的关系 hibernary 一个新手问题 关于SSH的问题求解 用OGNL遇到的一个异常?(求详解!) EL问题,请教高手 spring+hibernate1对1关联中ClassCastException 服务器配置 <form method="post" name="form" action="/settime.jsp" >为什么会是下载? 如何改变resin启动的默认页面?(在线等) 在线等待,急,请问怎样在soloris操作系统系运行Applet文件? 普通的类中得到Session的方法 tomcat启动问题
1.Connection connection = null;
2.try {
3. Class.forName("org.hsqldb.jdbcDriver");
4. try {
5. connection = DriverManager.getConnection("jdbc:hsqldb:test");
6. } catch (SQLException e) {
7. LOG.error("Problem getting connection", e);
8. }
9.
10. if (connection != null) {
11. LOG.info("Got connection :)");
12. } else {
13. LOG.error("Didn't get connection, which probably means that no Driver accepted the URL");
14. }
15.
16.} catch (ClassNotFoundException e) {
17. LOG.error("Couldn't find driver", e);
18.} finally {
19. try {
20. // Check to see we actually got a connection before we
21. // attempt to close it.
22. if (connection != null) {
23. connection.close();
24. }
25. } catch (SQLException e) {
26. LOG.error("Problem closing connection", e);
27. }
28.}To start using Proxool, simply edit lines 3 and 5 (ed in blue):
1.Connection connection = null;
2.try {
3. Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
4. try {
5. connection = DriverManager.getConnection("proxool.example:org.hsqldb.jdbcDriver:jdbc:hsqldb:test");
6. } catch (SQLException e) {
7. LOG.error("Problem getting connection", e);
8. }
9.
10. if (connection != null) {
11. LOG.info("Got connection :)");
12. } else {
13. LOG.error("Didn't get connection, which probably means that no Driver accepted the URL");
14. }
15.
16.} catch (ClassNotFoundException e) {
17. LOG.error("Couldn't find driver", e);
18.} finally {
19. try {
20. // Check to see we actually got a connection before we
21. // attempt to close it.
22. if (connection != null) {
23. connection.close();
24. }
25. } catch (SQLException e) {
26. LOG.error("Problem closing connection", e);
27. }
28.}
我的新书刚上架,欢迎订购:
《搜索引擎零距离—基于Ruby+Java搜索引擎原理与实现》 清华出版社。
http://www.huachu.com.cn/itbook/itbookinfo.asp?lbbh=10105450
import java.sql.DriverManager;
import java.util.Hashtable;import javax.naming.Context;
import javax.naming.InitialContext;import oracle.jdbc.pool.OracleConnectionPoolDataSource;public class CreatePool {
public static Connection getCon(){
Connection con = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "tiger");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
} public static void main(String[] args) {
Connection conn =getCon();
if(null!=conn)
{
System.out.print("2--------");
}
createPool();
Connection connection = getPool("jdbc/pool");
if(null!=connection){
System.out.print("2--------");
}
}
public static void createPool(){
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
try {
OracleConnectionPoolDataSource ods = new OracleConnectionPoolDataSource();
Context context = new InitialContext(ht);
ods.setPassword("tiger");
ods.setURL("jdbc:oracle:thin:@127.0.0.1:1521:orcl");
ods.setUser("scott");
//ods.setLoginTimeout(arg0)
ods.setDriverType("oracle.jdbc.driver.OracleDriver");
context.unbind("jdbc/pool");
context.bind("jdbc/pool", ods);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getPool(String name){
Connection con = null;
Hashtable ht = new Hashtable();
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.fscontext.RefFSContextFactory");
try {
Context context = new InitialContext(ht);
OracleConnectionPoolDataSource ods = (OracleConnectionPoolDataSource)context.lookup(name);
con = ods.getConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
}