请问c3p0连接池怎么读Properties配置文件?

liuzhengkang 2009-02-13 11:04:52
请问c3p0连接池怎么读properties配置文件?
我怎么试都不行,总是:driverClass--->null
properties文件如下:

driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbcUrl=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=stock2
user=sa
password=ba123

不知道为什么就是读不了,向高手求救,希望给点代码提示一下,谢谢了!,我在官网那里搞了一上午都没弄出什么来。
现在只能在new 一个ComboPooledDataSource后,一个个设置,这样很不方便,每次改的时候都要从新编译!!

ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
ds.setJdbcUrl( "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=stock2" );
ds.setUser("sa");
ds.setPassword("ba123");
...全文
2846 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq591137303 2011-02-22
  • 打赏
  • 举报
回复
写一个properties文件,在Spring配置文件中读取.properties文件的内容
lr5200 2010-12-23
  • 打赏
  • 举报
回复
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(Unknown Source)
at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:223)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
at

看你错误提示,应该是没有加入oracle的驱动jar包吧!!
qindequan 2009-02-16
  • 打赏
  • 举报
回复
不告诉你
fulianglove 2009-02-16
  • 打赏
  • 举报
回复
Properties p = new Properties();
。。。//加载Properties文件

ds.setDriverClass(p.getProperty("driver.class"));
ds.setJdbcUrl(p.getProperty("jdbc.url"));
ds.setUser(p.getProperty("user"));
ds.setPassword(p.getProperty("password"));
这种形式不可以么?
这样以后又变更个修改Properties文件就可以了
liuzhengkang 2009-02-16
  • 打赏
  • 举报
回复
有没有高手知道啊,帮帮忙啊!
liuzhengkang 2009-02-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 fulianglove 的回复:]
Properties p = new Properties();
。。。//加载Properties文件

ds.setDriverClass(p.getProperty("driver.class"));
    ds.setJdbcUrl(p.getProperty("jdbc.url"));
    ds.setUser(p.getProperty("user"));                                 
    ds.setPassword(p.getProperty("password"));
这种形式不可以么?
这样以后又变更个修改Properties文件就可以了
[/Quote]
呵呵!非常感谢fulianglove。我怎么就想不到呢,不会转弯啊
liuzhengkang 2009-02-14
  • 打赏
  • 举报
回复
我这样的话,
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
ds.setJdbcUrl( "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=stock2" );
ds.setUser("sa");
ds.setPassword("ba123");

每次都用重新编译不方便啊,我不知道c3p0要怎么把properties加载上去才行,
上面的这样设置了ds.setProperties(p);都不行!!
Dantin 2009-02-14
  • 打赏
  • 举报
回复
不会,帮忙顶起来
liuzhengkang 2009-02-13
  • 打赏
  • 举报
回复
但还是提示:
2009-2-13 11:59:52 com.mchange.v2.log.MLog <clinit>
信息: MLog clients using java 1.4+ standard logging.
2009-2-13 11:59:53 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
2009-2-13 11:59:53 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 2sa1hd7zugo04o1c4fygx|ef5502, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> null, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 2sa1hd7zugo04o1c4fygx|ef5502, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> null, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver, user=******, password=******, jdbcUrl=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=stock2}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
2009-2-13 12:00:22 com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask run
警告: com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask@8e32e7 -- Acquisition Attempt Failed!!! Clearing pending acquires. While trying to acquire a needed new resource, we failed to succeed more than the maximum number of allowed acquisition attempts (30). Last acquisition attempt exception:
java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(Unknown Source)
at com.mchange.v2.c3p0.DriverManagerDataSource.driver(DriverManagerDataSource.java:223)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:134)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:182)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:171)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:137)
at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1014)
at com.mchange.v2.resourcepool.BasicResourcePool.access$800(BasicResourcePool.java:32)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:1810)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:547)

properties是加载成功的,因为提示信息里有:
properties -> {driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver, user=******, password=******, jdbcUrl=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=stock2},
我只是不知道为什么,那个driverClass -> null,和jdbcUrl -> null,?怎么还是null啊
liuzhengkang 2009-02-13
  • 打赏
  • 举报
回复
Properties文件我是这样加载上去的啊

Properties p = new Properties();
p.load(this.getClass().getResourceAsStream(c3p0Properties));
ds = new ComboPooledDataSource();
ds.setProperties(p);


mysky191_chi 2009-02-13
  • 打赏
  • 举报
回复
ResourceBundle rb = ResourceBundle.getBundle("c3po",Locale.ENGLISH);//此文件在classes下
rg.getString("");
fulianglove 2009-02-13
  • 打赏
  • 举报
回复
看javaAPI
java.util.Properties
Properties prop = new Properties();
prop.load(InputStream inStream);
prop.getProperty(String key);
fulianglove 2009-02-13
  • 打赏
  • 举报
回复
setProperties可能要求你的键名必须要是它要求的吧,所以有的找不到
不过不知道他具体要求的是什么。。。。。。

=====================================================================
你还使用
ds.setDriverClass( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
ds.setJdbcUrl( "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=stock2" );
ds.setUser("sa");
ds.setPassword("ba123");
这种方式,只不过你面的字符串是propertie.getProperty("XXXX");也可以,没啥区别,反正只需要改properties文件
=================================================================

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧