当前位置: 技术问答>java相关
jdbc:为什么在DataSource中无法获得Connection????
来源: 互联网 发布时间:2015-09-17
本文导语: 我用j2ee -verbose启动j2ee服务,用下面代码取得数据联接, Context ctx = new InitialContext(); JdbcDataSource ds = (JdbcDataSource)ctx.lookup("jdbc/Cloudscape"); Connection conn = ds.getConnection(); JNDI没有错误,可以正确得到ds,但是却不能得...
我用j2ee -verbose启动j2ee服务,用下面代码取得数据联接,
Context ctx = new InitialContext();
JdbcDataSource ds = (JdbcDataSource)ctx.lookup("jdbc/Cloudscape");
Connection conn = ds.getConnection();
JNDI没有错误,可以正确得到ds,但是却不能得到conn,
就是说ds.getConnection();有错误。
异常是
No local string for datasource.wrongclient
这是怎么回事???????
Context ctx = new InitialContext();
JdbcDataSource ds = (JdbcDataSource)ctx.lookup("jdbc/Cloudscape");
Connection conn = ds.getConnection();
JNDI没有错误,可以正确得到ds,但是却不能得到conn,
就是说ds.getConnection();有错误。
异常是
No local string for datasource.wrongclient
这是怎么回事???????
|
package ejb;
import java.util.*;
import java.io.*;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
public class DBConnection implements Serializable{
protected Connection con=null;
private String url = "com.ibm.websphere.naming.WsnInitialContextFactory" ;
private String user = "db2admin" ;
private String pass = "db2admin" ;
private String Dbs = "jdbc/DataSource" ;
public DBConnection() { }
DataSource ds = null;
public void dbOpen() throws java.lang.Exception{
try
{
if (con == null || con.isClosed()) {
// create parameter list to access naming system
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, url);
// access naming system
Context context = new InitialContext(env);
// get DataSource factory object from naming system
ds = (DataSource)context.lookup(Dbs);
con = ds.getConnection(user, pass);
System.out.println("DB OPEN");
} else {
System.out.println("OPENED DB");
}
} catch (Exception e)//数据源不存在,用jdbcjdbc连接
{
System.out.println("数据源不存在");
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
Properties p=new Properties();
p.put("user",user);
p.put("password",pass);
con=DriverManager.getConnection("jdbc:db2:db_sdbc",p);
System.out.println("连接数据库异常:"+e.toString());
}
}
/*public void beginTrans() throws SQLException{
con.setAutoCommit(false);
}
public void commitTrans() throws SQLException{
con.commit();
}
public void rollbackTrans() throws SQLException{
con.rollback();
}
public boolean isTrans() throws SQLException{
return !con.getAutoCommit();
}*/
public void dbClose() {
try {
if (con != null && !con.isClosed()) {
con.rollback();
con.close();
System.out.println("DB CLOSE");
} else {
System.out.println("CLOSED DB");
}
}
catch (Exception e) {
System.out.println("关闭数据库异常:"+e.toString());
}
finally {
con = null;
}
}
}
import java.util.*;
import java.io.*;
import java.sql.*;
import javax.naming.*;
import javax.sql.DataSource;
public class DBConnection implements Serializable{
protected Connection con=null;
private String url = "com.ibm.websphere.naming.WsnInitialContextFactory" ;
private String user = "db2admin" ;
private String pass = "db2admin" ;
private String Dbs = "jdbc/DataSource" ;
public DBConnection() { }
DataSource ds = null;
public void dbOpen() throws java.lang.Exception{
try
{
if (con == null || con.isClosed()) {
// create parameter list to access naming system
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, url);
// access naming system
Context context = new InitialContext(env);
// get DataSource factory object from naming system
ds = (DataSource)context.lookup(Dbs);
con = ds.getConnection(user, pass);
System.out.println("DB OPEN");
} else {
System.out.println("OPENED DB");
}
} catch (Exception e)//数据源不存在,用jdbcjdbc连接
{
System.out.println("数据源不存在");
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
Properties p=new Properties();
p.put("user",user);
p.put("password",pass);
con=DriverManager.getConnection("jdbc:db2:db_sdbc",p);
System.out.println("连接数据库异常:"+e.toString());
}
}
/*public void beginTrans() throws SQLException{
con.setAutoCommit(false);
}
public void commitTrans() throws SQLException{
con.commit();
}
public void rollbackTrans() throws SQLException{
con.rollback();
}
public boolean isTrans() throws SQLException{
return !con.getAutoCommit();
}*/
public void dbClose() {
try {
if (con != null && !con.isClosed()) {
con.rollback();
con.close();
System.out.println("DB CLOSE");
} else {
System.out.println("CLOSED DB");
}
}
catch (Exception e) {
System.out.println("关闭数据库异常:"+e.toString());
}
finally {
con = null;
}
}
}
|
少安毋躁,给你一段代码看看
m_ht=new Hashtable();
m_ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
m_ht.put(Context.PROVIDER_URL,t3://localhost:7001);
m_ctx=new InitialContext(m_ht);
m_ds=(DataSource)m_ctx.lookup(datasourceJNDIname);
m_conn=m_ds.getConnection();
这是我写ejb时候连接数据源的代码,我的应用服务器是weblogic
看对你有没有帮助
m_ht=new Hashtable();
m_ht.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
m_ht.put(Context.PROVIDER_URL,t3://localhost:7001);
m_ctx=new InitialContext(m_ht);
m_ds=(DataSource)m_ctx.lookup(datasourceJNDIname);
m_conn=m_ds.getConnection();
这是我写ejb时候连接数据源的代码,我的应用服务器是weblogic
看对你有没有帮助