c3p0使用

chinaluopiao 2011-09-02 03:03:17
我想在java se的环境里面使c3p0连接池!! 当时从官方下载下来的里面的xml配置文件是
<server>

<mbean code="com.mchange.v2.c3p0.mbean.C3P0PooledDataSource"
name="jboss:service=C3P0PooledDataSource">
<attribute name="JndiName">java:PooledDS</attribute>
<attribute name="JdbcUrl">jdbc:oracle:thin:@localhost:1521:orcl</attribute>
<attribute name="DriverClass">oracle.jdbc.driver.OracleDriver</attribute>
<attribute name="User">scott</attribute>
<attribute name="Password">123456</attribute>
<depends>jboss:service=Naming</depends>
</mbean>

</server>

请问第一个参数和最后一个参数怎么配置!! java se的环境是不是不要配置最后一个参数!!

里面的源码的例子是:

String jndiName = args[0];
DataSource unpooled = DataSources.unpooledDataSource(
"jdbc:oracle:thin:@localhost:1521:orcl", "scott", "123456");
DataSource pooled = DataSources.pooledDataSource(unpooled);

InitialContext ctx = new InitialContext();
ctx.rebind(jndiName, pooled);


我要是不配置就报Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
这个错!!

请问下这个jndiName是个什么东西!!



import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;

/**
* This example shows how to programmatically get and directly use an unpooled
* DataSource
*/
public final class UseJndiDataSource {

public static void main(String[] args) {
try {

String jndiName = args[0];

InitialContext ctx = new InitialContext();

// acquire the DataSource... this is the only c3p0 specific code
// here
DataSource ds = (DataSource) ctx.lookup(jndiName);

// get hold of a Connection an do stuff, in the usual way
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = ds.getConnection();
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT * FROM foo");
while (rs.next())
System.out.println(rs.getString(1));
} finally {

// c3p0 DataSources will properly deal.
attemptClose(rs);
attemptClose(stmt);
attemptClose(con);
}
} catch (Exception e) {
e.printStackTrace();
}
}

static void attemptClose(ResultSet o) {
try {
if (o != null)
o.close();
} catch (Exception e) {
e.printStackTrace();
}
}

static void attemptClose(Statement o) {
try {
if (o != null)
o.close();
} catch (Exception e) {
e.printStackTrace();
}
}

static void attemptClose(Connection o) {
try {
if (o != null)
o.close();
} catch (Exception e) {
e.printStackTrace();
}
}

private UseJndiDataSource() {
}
}

...全文
96 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinaluopiao 2011-09-02
  • 打赏
  • 举报
回复
不知道怎么的 现在报错驱动不对了!! 但是驱动是对的!! 那个xml配置文件好像就用不上了啊
游一游走一走 2011-09-02
  • 打赏
  • 举报
回复
其实你要用的就是这段代码而以
DataSource unpooled = DataSources.unpooledDataSource(
"jdbc:oracle:thin:@localhost:1521:orcl", "scott", "123456");
DataSource pooled = DataSources.pooledDataSource(unpooled);

pooled .getConnection();
就可以操作了。。。不需要其它的,至于jndiName就是JAVA的一个命令服务,就像WINDOWS的注册表,应用写个键值对了,其它逻辑可以通过这个键去获取那个值,你给的代码貌似是去JOBSS取得相应的数据DataSource

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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