呵,你的InitialContext没东东啊你如果是用weblogic服务器,这儿有一个例子: private static Context getInitialContext() throws Exception {
String url = "t3://localhost:7001";
String user = "bibi";
String password = "12345678";
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS,
password == null ? "" : password);
}
return new InitialContext(properties);
}
catch (Exception ex) {
throw ex;
}
}
然后去调用它:
Context ctx = getInitialContext();
DataSource ds = (javax.sql.DataSource) ctx.lookup("myDataSource");
String url = "t3://localhost:7001";
String user = "bibi";
String password = "12345678";
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS,
password == null ? "" : password);
}
return new InitialContext(properties);
}
catch (Exception ex) {
throw ex;
}
}
然后去调用它:
Context ctx = getInitialContext();
DataSource ds = (javax.sql.DataSource) ctx.lookup("myDataSource");
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.naming.java.javaURLContextFactory");
然后其它照着套试试
在server.xml中加:
<Context path="" docBase="www"
debug="5" reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_www_log." suffix=".txt"
timestamp="true"/>
<Resource name="jdbc/MSSQL7.0DB"
auth="Container"
type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/MSSQL7.0DB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>300000</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>1000</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>981050</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.jnetdirect.jsql.JSQLDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:JSQLConnect://172.16.10.99:1433/database=takam</value>
</parameter>
<parameter>
<name>removeAbandoned</name>
<value>true</value>
</parameter>
<parameter>
<name>removeAbandonedTimeout</name>
<value>60</value>
</parameter>
<parameter>
<name>logAbandoned</name>
<value>true</value>
</parameter>
</ResourceParams>
</Context>
然后可以用如下方法调用 :
Connection con =null;
Statement stmt=null;
ResultSet rs =null;
javax.sql.DataSource ds = (javax.sql.DataSource)new javax.naming.InitialContext().lookup("java:comp/env/jdbc/MSSQL7.0DB");
if (ds != null) con = ds.getConnection();
if (con != null) stmt=con.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY
);
BasicDataSource source=null;
try{
source = new BasicDataSource(); //以下设置的参数都可以从配置文件中获得.而可以不局限在server.xml
String connectURL="jdbc:mysql://localhost:3306/vter_info";
source.setDriverClassName("org.gjt.mm.mysql.Driver");
source.setUrl(connectURL);
source.setUsername("root");
source.setPassword("");
source.setMaxActive(1000);
source.setMaxIdle(30);
source.setMaxWait(10000);我的意思你明白了吗?
Context initCtx = new InitialContext();为:
java.util.Hashtable properties = new java.util.Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.commons.dbcp.BasicDataSourceFactory");
Context initCtx = new InitialContext(properties);
试试看
Datasource 是访问实际连接池的
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;//
// Here are the dbcp-specific classes.
// Note that they are only used in the setupDriver
// method. In normal use, your classes interact
// only with the standard JDBC API
//
import java.sql.DriverManager;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.AbandonedConfig;public class DBCPPOOL {private static PropertyReader pr = PropertyReader.getInstance("conf");
private static String driver = pr.getProperty("db.driver").trim();
private static String uri = pr.getProperty("db.uri").trim();
private static String user = pr.getProperty("db.user").trim();
private static String password = pr.getProperty("db.password").trim();
private static DataSource dataSource =null;public DBCPPOOL() {
}public static Connection getConnection()
throws Exception {
Connection conn = null; if (dataSource == null){
dataSource = setupDataSource(driver,uri,user,password);
} conn = dataSource.getConnection();
return conn; }private static DataSource setupDataSource(String driver,String connectURI,String user,String password)
throws Exception{ //加载驱动程序
Class.forName(driver);
//
// First, we'll need a ObjectPool that serves as the
// actual pool of connections.
//
// We'll use a GenericObjectPool instance, although
// any ObjectPool implementation will suffice.
//
ObjectPool connectionPool = new GenericObjectPool(null); //
// Next, we'll create a ConnectionFactory that the
// pool will use to create Connections.
// We'll use the DriverManagerConnectionFactory,
// using the connect string passed in the command line
// arguments.
//
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI,user,password); //
// Now we'll create the PoolableConnectionFactory, which wraps
// the "real" Connections created by the ConnectionFactory with
// the classes that implement the pooling functionality.
//
AbandonedConfig abandonedConfig = new AbandonedConfig();
abandonedConfig.setRemoveAbandoned(true);
//abandonedConfig.setRemoveAbandonedTimeout(60);
abandonedConfig.setLogAbandoned(true); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true,abandonedConfig); //
// Finally, we create the PoolingDriver itself,
// passing in the object pool we created.
//
PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
//Logger.log( "DBCPPOOL Active Connections:" + connectionPool.toString() );
//Logger.log( "DBCPPOOL Idle Connections:" + connectionPool.getNumIdle() ); return dataSource;
}}
我弄了半天也没有搞出来,
程序总是出错,可能就是没有找到数据源了!
如何设置JNDI和数据库之间的联系啊!