我用jboss也是这样,使用entity bean没有问题,但是在session bean和一个普通class中连接oracle数据源都不行,mysql也不行,而且用jdbc直接连都没有问题的,是数据源的适用范围有限吗?

解决方案 »

  1.   

    我用Jboss entity Bean有问题啊,麻烦你也给看看
    http://expert.csdn.net/Expert/TopicView1.asp?id=2328471 另外你把你的错误信息发上来啊,难度没报错?
      

  2.   

    如果是JBOSS的话,"bbb"这个JNDI不对,应写成"java:/bbb"
      

  3.   

    你这个相反从跟本上来说就不对不应该把DB访问逻辑写到你的SESSION bean 中推荐将数据访问逻辑写成一个助手类这样比较符合分层设计的要求和可维护性。。
      

  4.   

    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;
        }
      

  5.   

    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;
        }
      

  6.   

    /**
         * 程序入口,做测试用
         * @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++));
            }
      

  7.   

    这是一个助手类,专门负责得到数据库连结,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;
                    }
            } }
      

  8.   

    http://www.csdn.net/develop/read_article.asp?id=19720有你需要的东东,去看看吧