首先在weblogic中配好连接池,其用法和其它的数据据库的连接方法都是一样的

解决方案 »

  1.   

    如一楼说的,先在weblogic中设置好连接池,可以使用以下类似代码连接...... Context ctx = null;
      EjbContext ectx = new EjbContext();
      ParamMsg parammsg = new ParamMsg();
      String pagesql = "";
      private netshop.page.pageParam pageparam;
      //private pageParam pageparam = new pageParam();  public superDao()
      {    //获取context对象
        try
        {
          //ctx = new InitialContext();
          ctx = ectx.getInitialContext();
        }catch(NamingException jnex)
        {
          System.err.println(ErrMsg.NAMING_EXCETPION_MSG);
        }catch(java.lang.Exception jlex)
        {
          jlex.printStackTrace();
        } } //进行连接
       protected  DataSource getDataSource()
       {
          DataSource ds=null;
          try{
            ds = (DataSource)ctx.lookup("MSDataSource");
          }catch(NamingException jnex)
          {
            System.err.println(ErrMsg.NAMING_EXCETPION_MSG);
          }
          return ds;
       } //关闭所有打开的资源
       protected  static void  closeAll(Connection connection, Statement statement, ResultSet resultSet)
       {
          //关闭结果集
         if (resultSet != null)
         {
           try{
             resultSet.close();
           }catch(java.sql.SQLException jsqlex)
           {
             System.err.println("SQL Error:" + jsqlex.getMessage());
           }
         }
         //关闭状态
         if (statement != null)
         {
             try {
               statement.close();
             }catch(java.sql.SQLException jsqlex)
             {
               System.err.println("SQL Error:" + jsqlex.getMessage());
             }
         }     //关闭连接
         if( connection != null)
         {
            try{
              connection.close();
            }catch(java.sql.SQLException jsqlex)
            {
              System.err.println("SQL Exception:" + jsqlex.getMessage());
            }
         }
         }    protected  static void  closeAll(Connection connection, PreparedStatement statement, ResultSet resultSet)
         {
           //关闭结果集
           if (resultSet != null) {
             try {
               resultSet.close();
             }
             catch (java.sql.SQLException jsqlex) {
               System.err.println("SQL Error:" + jsqlex.getMessage());
             }
           }
           //关闭状态
           if (statement != null) {
             try {
               statement.close();
             }
             catch (java.sql.SQLException jsqlex) {
               System.err.println("SQL Error:" + jsqlex.getMessage());
             }
           }       //关闭连接
           if (connection != null) {
             try {
               connection.close();
             }
             catch (java.sql.SQLException jsqlex) {
               System.err.println("SQL Exception:" + jsqlex.getMessage());
             }
           }
         }     //获得连接
         public  Connection getConnection()
         {
           Connection con = null;
           PreparedStatement  stm = null;
           ResultSet  rs  = null;
           DataSource ds =  null;       String uid = null;       ds = getDataSource();       try{
             con = ds.getConnection();
           }catch(SQLException jsqlex)
           {
             jsqlex.printStackTrace();
           }       return con;     }
      

  2.   

    在http://localhost:7001/console里面配置连接池;
    【services】-【jdbc】-【Connection pool】,新建连接池;
    【services】-【jdbc】-【Data Source】,新建数据源;注意其中的JNDI名;
    再用楼上的方法访问数据源,OK。
      

  3.   

    楼上的代码我编译时有以下数据类型不能被识别
    Context  EjbContext ParamMsg  DataSource  NamingException  
    是不是有什么包我没有Import进来呀
      

  4.   

    请各位帮我看看  楼上的代码有点问题 为什么连接池中要用到EJBContext呢 而且这个类也没有getInitialContext()这个函数呀
      

  5.   

    import javax.naming.InitialContext;
    import javax.naming.Context;
    import javax.sql.DataSource;
    import java.sql.*;
    import java.io.*;
    import javax.rmi.*;
    import java.util.*;  private boolean creatConnection(){
        /*********************************************************************
         功能:连接数据源,产生数据连接
         *********************************************************************/
        try{
          InitialContext ctx = new InitialContext();
          javax.sql.DataSource myDataSource = (javax.sql.DataSource) ctx.lookup("数据源JNDI名");
          con = myDataSource.getConnection();
          stm = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
          return true;
        }catch (Exception e) {
          System.out.println("连接出错!!!"+e.getMessage());
          return false;
        }  }