jdbc:为什么在DataSource中无法获得Connection???? 自己顶!!大家有没有遇到类似情况,我在javasoft上也发现有人和我的问题一摸一样,可是也没有人回答。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 少安毋躁,给你一段代码看看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 看对你有没有帮助 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; } }} 谢谢上面两位,但是我没有weblogic或者websphere,我用的就是j2eesdk自带的那个j2ee Server,所以不能使用你们说得方法,我也试着寻找Context.INITIAL_CONTEXT_FACTORY相对应的InitialContextFactory,但是没有找到,我看一些j2ee教程是使用无参数的InitialContext(),但不知为什么不可以,另外,我不是在ejb中调用这些代码,而是不同的客户端,不知道这里有没有影响。最后,谁清楚j2eesdk中的DeployTool,写一下如何将Datasource和JNDI进行绑定。我试着自己在Tools|Server Configuration中的DataSource绑定了一个自己的DataSource,而且在启动j2ee时也可以看到正确绑定,不知这种方法是否可行。 Extjs GridPanel 加不到 TabPanel里去 那个大哥帮忙下 try里面有个return 那后面的finally还执行吗? 新手问路。。。 菜鸟求助~ 【求助】java构造一个复数类 试了无数次,请教各位高手!!! ArrayList 的初始化问题 The JAVA Web Server Tutorial书名的中文译名是什么呀? 关于 包名冲突 的问题(非简单 class 重名) 书上的例子,看看哪错了啊,郁闷 这是什么错误啊? JDK编译时,怎样看到全部出错信息?
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
看对你有没有帮助
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;
}
}
}