说明一下,我用的应用服务器是ibm webshpere 4.0

解决方案 »

  1.   

    和bmp差不多啊,一种是直接通过jdbc获得connection;
    一种是lookup jndi,找出datasource(当然你要事先配置好)
      

  2.   

    能否给一个连接数据库并执行一条sql语句的例子,谢谢了。
      

  3.   

    这是我的源程序,有哪里出错了吗?
    ======================================
    package Test3; import java.sql.*; 
    import javax.sql.*; 
    import javax.naming.*; 
    import javax.ejb.*; public class Test3SessionBean implements SessionBean {   private SessionContext sc = null; 
      private Context ctx = null; 
      private DataSource ds = null; 
       
      public void ejbCreate() throws CreateException {       try { 
              ctx = new InitialContext(); 
              ds = (DataSource)ctx.lookup("jdbc/db2/HelloWroldDatasource"); 
          } 
          catch (Exception e) { 
              System.out.println(e.getMessage()); 
              throw new CreateException(); 
          } 
      }   public void gettest3() throws SQLException { 
           
          Connection con = null; 
      ResultSet rs; 
          try { 
              con = ds.getConnection("dingjs", "crystal"); 
              Statement stmt = con.createStatement(); 
              rs =  stmt.executeQuery("select * from TEST2"); 
               while (rs.next()) {
     
                int id = rs.getInt("ID");
                String firstName = rs.getString("FIRSTNAME");
    String lastName = rs.getString("LASTNAME");            System.out.print(id + "   " + firstName + "   " + lastName);
     
            }           
            rs.close();
            stmt.close();
            con.close();
          } 
      catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }
      }    
      public void setSessionContext(SessionContext sc) { 
          this.sc = sc; 
      } 
       
      public void ejbRemove() {} 
      public void ejbPassivate() {} 
      public void ejbActivate() {}    

      

  4.   

    Context ctx = new InitialContext();
    DataSource ds = (DataSource)ctx.lookup("jdbc/db2/HelloWroldDatasource");
    Connection con = ds.getConnection("dingjs", "crystal");    Statement stmt = con.createStatement(); 
        ResultSet rs =  stmt.executeQuery("select * from TEST2");
        while (rs.next()) {
         int id = rs.getInt("ID");
         String firstName = rs.getString("FIRSTNAME");
     String lastName = rs.getString("LASTNAME");
         System.out.print(id + "   " + firstName + "   " + lastName);
         }
         rs.close();
         stmt.close();
         con.close();