如何在session bean 里连接oracle 只要连上100分就是你的,在线等候 我用jboss也是这样,使用entity bean没有问题,但是在session bean和一个普通class中连接oracle数据源都不行,mysql也不行,而且用jdbc直接连都没有问题的,是数据源的适用范围有限吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我用Jboss entity Bean有问题啊,麻烦你也给看看http://expert.csdn.net/Expert/TopicView1.asp?id=2328471 另外你把你的错误信息发上来啊,难度没报错? 如果是JBOSS的话,"bbb"这个JNDI不对,应写成"java:/bbb" 你这个相反从跟本上来说就不对不应该把DB访问逻辑写到你的SESSION bean 中推荐将数据访问逻辑写成一个助手类这样比较符合分层设计的要求和可维护性。。 public class CustomerADC { String sql = ""; public String insertCustomer(CustomerDTO customer){ Connection con = null; PreparedStatement pstmt = null; String customerID = ""; try{ sql = "insert into db2admin.customer values(?,?,?,?,?,?,?,?,?)"; System.out.println("sql"); con = ConnectionFactory.getConnection(); pstmt = con.prepareStatement(sql); customerID = GeneratorCustomerID.getCustomerID(); pstmt.setString(1, customerID); pstmt.setString(2, customer.getPassword()); pstmt.setString(3, customer.getFirstName()); pstmt.setString(4, customer.getLastName()); pstmt.setString(5, customer.getAddress()); pstmt.setString(6, customer.getCity()); pstmt.setString(7, customer.getState()); pstmt.setString(8, customer.getZip()); pstmt.setString(9, customer.getPhone()); pstmt.executeUpdate(); System.out.println("sql"); return customerID; }catch(Exception e){ }finally{ try{ if(con!=null) con.close(); if(pstmt!=null) pstmt.close(); }catch(Exception e){ } } return null; } public class CustomerSessionBean implements SessionBean, CustomerBusiness{ SessionContext sessionContext; public String InsertCustomer(CustomerDTO customerDTO)throws java.rmi.RemoteException{ CustomerADC customerADC = new CustomerADC(); try{ return customerADC.insertCustomer(customerDTO); }catch(Exception e){ e.printStackTrace(); } return null; } /** * 程序入口,做测试用 * @param args * @throws SQLException */ public static void main(String[] args) throws SQLException { Connection con = null; PreparedStatement ps = null; Query query = new Query();//连接池 query.createConnection("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@192.168.0.26:1521:SDJS", "ces#info","ces#info"); String strQuery = "SELECT file_id,file_name FROM t_datum_info"; con = query.getConnection(); ps = con.prepareCall(strQuery); ResultSet result = ps.executeQuery(); int i = 1; while (result.next()) { i = 1; System.out.println(result.getString(i++)); System.out.println(result.getString(i++)); } 这是一个助手类,专门负责得到数据库连结,DataSource是weblogic中datasource的jndipackage oa;import java.sql.*;import javax.naming.*;import javax.ejb.*;import java.util.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class XConnection { public XConnection() { } public static Connection getConnection() throws Exception { try { Properties h=new Properties(); h.setProperty(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory"); h.setProperty(Context.PROVIDER_URL,"t3://127.0.0.1:7001"); Context ctx = new InitialContext(h); javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("OADataSource"); // javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/ejbPool"); return ds.getConnection(); } catch (Exception e) { System.err.println("Could not locate datasource! Reason:"); e.printStackTrace(); throw e; } } } http://www.csdn.net/develop/read_article.asp?id=19720有你需要的东东,去看看吧 讨论java后台控制前台页面时间问题 问一个关于hibernate3.3的配置问题 问个简单的list排序问题 怎么样可以不需要登陆访问权限页面? AXIS做的webservice在JBOSS里如何部署 学习hibernate+struts遭遇NoClassDefFoundError,请大侠过来看看 一两个关于Struts的初级问题, RMI问题: 在struts 中怎样把jsp页面的from数据写到formBean中 about Time Service 初学hibernate遇到的低级问题,请高手指教 Hibernate中一对多关系中的空数据问题?
http://expert.csdn.net/Expert/TopicView1.asp?id=2328471 另外你把你的错误信息发上来啊,难度没报错?
String sql = ""; public String insertCustomer(CustomerDTO customer){
Connection con = null;
PreparedStatement pstmt = null;
String customerID = "";
try{
sql = "insert into db2admin.customer values(?,?,?,?,?,?,?,?,?)";
System.out.println("sql");
con = ConnectionFactory.getConnection();
pstmt = con.prepareStatement(sql);
customerID = GeneratorCustomerID.getCustomerID();
pstmt.setString(1, customerID);
pstmt.setString(2, customer.getPassword());
pstmt.setString(3, customer.getFirstName());
pstmt.setString(4, customer.getLastName());
pstmt.setString(5, customer.getAddress());
pstmt.setString(6, customer.getCity());
pstmt.setString(7, customer.getState());
pstmt.setString(8, customer.getZip());
pstmt.setString(9, customer.getPhone());
pstmt.executeUpdate();
System.out.println("sql");
return customerID;
}catch(Exception e){
}finally{
try{
if(con!=null) con.close();
if(pstmt!=null) pstmt.close();
}catch(Exception e){
}
}
return null;
}
SessionContext sessionContext; public String InsertCustomer(CustomerDTO customerDTO)throws java.rmi.RemoteException{
CustomerADC customerADC = new CustomerADC();
try{
return customerADC.insertCustomer(customerDTO);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
* 程序入口,做测试用
* @param args
* @throws SQLException
*/
public static void main(String[] args) throws SQLException {
Connection con = null;
PreparedStatement ps = null;
Query query = new Query();//连接池
query.createConnection("oracle.jdbc.driver.OracleDriver",
"jdbc:oracle:thin:@192.168.0.26:1521:SDJS",
"ces#info","ces#info"); String strQuery = "SELECT file_id,file_name FROM t_datum_info";
con = query.getConnection();
ps = con.prepareCall(strQuery);
ResultSet result = ps.executeQuery();
int i = 1;
while (result.next()) {
i = 1;
System.out.println(result.getString(i++));
System.out.println(result.getString(i++));
}
import java.sql.*;
import javax.naming.*;
import javax.ejb.*;
import java.util.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class XConnection {
public XConnection() {
} public static Connection getConnection() throws Exception { try {
Properties h=new Properties();
h.setProperty(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
h.setProperty(Context.PROVIDER_URL,"t3://127.0.0.1:7001"); Context ctx = new InitialContext(h);
javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("OADataSource"); // javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/ejbPool");
return ds.getConnection();
}
catch (Exception e) {
System.err.println("Could not locate datasource! Reason:");
e.printStackTrace();
throw e;
}
} }