c3p0使用
我想在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是个什么东西!!
Java code
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() {
}
}
------解决方案--------------------
其实你要用的就是这段代码而以
DataSource unpooled = DataSources.unpooledDataSource(
"jdbc:oracle:thin:@localhost:1521:orcl", "scott", "123456");
DataSource pooled = DataSources.pooledDataSource(unpooled);
pooled .getConnection();
就可以操作了。。。不需要其它的,至于jndiName就是JAVA的一个命令服务,就像WINDOWS的注册表,应用写个键值对了,其它逻辑可以通过这个键去获取那个值,你给的代码貌似是去JOBSS取得相应的数据DataSource