Csdn上多的很,你自己搜一下就有N多!

解决方案 »

  1.   

    正好,我刚写了一段
    给你: public Connection getPoolDataSource(String jndi)
    {
    Connection conn = null;
    try
      {
      InitialContext itial = new InitialContext();
      DataSource ds = (DataSource)itial.lookup("java:comp/env/jdbc/"+jndi);
      conn = ds.getConnection();
      }
      catch(Exception er)
      {
      System.out.println("Exception in getPoolDataSource: " + er.toString());
      }
      return conn;
    }
      

  2.   

    不用连接池时是这样做的吧:
    Class.forName(...)
    Connection conn = DriverManager.getConnection(....);
    Statement stat = conn.createStatement();现在你就可以这样:
    public Connection getPoolDataSource(String jndi)
    {
    Connection conn = null;
    try
     {
     InitialContext itial = new InitialContext();
     DataSource ds = (DataSource)itial.lookup("java:comp/env/"+jndi);
     conn = ds.getConnection();
     }
     catch(Exception er)
     {
     System.out.println("Exception in getPoolDataSource: " + er.toString());
     }
     return conn;
    }Connection con = getPoolDataSource("jdbc/mysql");
    Statement stat = conn.createStatement();
    ...后面就一样了,
    只是得到的数据源连接的方式不同"jdbc/mysql"是在tomcat为连接池配的JNDI名字
      

  3.   

    我原来的java代码是这样的:package online;
    import java.sql.*;
    public class onlineconn1
    {
      String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String sConnStr = "jdbc:odbc:online";
      //String strUser = "sa";
      //String strPassword = "1978sx";
      Connection conn = null;
      public ResultSet rs = null;
      private java.sql.Statement stmt = null;
      public onlineconn1()
      {
        try {
               Class.forName(sDBDriver);
             }
        catch(java.lang.ClassNotFoundException e)
             {
               System.err.println("onlineconn1(): " + e.getMessage());
             }
      }
      public ResultSet executeQuery(String sql)
      {
         rs = null;
         try {
                conn = DriverManager.getConnection(sConnStr);
                Statement stmt = conn.createStatement();
                rs = stmt.executeQuery(sql);
              }
         catch(SQLException ex)
             {
                System.err.println("aq.executeQuery: " + ex.getMessage());
              }
         return rs;
      }
      public void executeUpdate(String sql)
      {
         try {
                conn = DriverManager.getConnection(sConnStr);
                Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                stmt.executeUpdate(sql);
              }
         catch(SQLException ex)
             {
                System.err.println("aq.executeUpdate: " + ex.getMessage());
              }
      }
      public void disconnectToDB() throws
          java.sql.SQLException{
                  
               if(rs!=null)
                 {
                     rs.close();
                     rs = null;  
                  }   
               
               if(stmt!=null){
                     stmt.close();
                     stmt = null;  
                  }           if(conn!=null)
                 {
                     conn.close();
                     conn = null;  
                  } 
            }
    }
    然后jsp页面中这样调用:
    <jsp:useBean id="workM" scope="page" class="online.onlineconn1" />String strSQL;
    strSQL="select * from student where name='"+name1+"' and mkey='"+key1+"'";
    try{
          workM.executeQuery(strSQL);
      if(!workM.rs.next()){
                          response.sendRedirect("stuloginerror.htm");
                                  }
         else{ 
    ......
    现在用连接池怎么改?
    谢谢!
      

  4.   

    <jsp:useBean id="workM" scope="page" class="online.onlineconn1" />String strSQL;
    strSQL="select * from student where name='"+name1+"' and mkey='"+key1+"'";
    把上面的改成
    <jsp:useBean id="workM" scope="page" class="online.onlineconn1" />String strSQL;
    strSQL="select * from student where name='"+name1+"' and mkey='"+key1+"'";
    Connection con=workM.getConnection();
    PreparedStatement pre=con.preparedStatement(sql);
    ResultSet res=pre.executeQuery();
      

  5.   

    改为连接池,我原来的java代码应如何改呢?谢谢!package online;
    import java.sql.*;
    public class onlineconn1
    {
      String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
      String sConnStr = "jdbc:odbc:online";
      //String strUser = "sa";
      //String strPassword = "1978sx";
      Connection conn = null;
      public ResultSet rs = null;
      private java.sql.Statement stmt = null;
      public onlineconn1()
      {
        try {
               Class.forName(sDBDriver);
             }
        catch(java.lang.ClassNotFoundException e)
             {
               System.err.println("onlineconn1(): " + e.getMessage());
             }
      }
      public ResultSet executeQuery(String sql)
      {
         rs = null;
         try {
                conn = DriverManager.getConnection(sConnStr);
                Statement stmt = conn.createStatement();
                rs = stmt.executeQuery(sql);
              }
         catch(SQLException ex)
             {
                System.err.println("aq.executeQuery: " + ex.getMessage());
              }
         return rs;
      }
      public void executeUpdate(String sql)
      {
         try {
                conn = DriverManager.getConnection(sConnStr);
                Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                stmt.executeUpdate(sql);
              }
         catch(SQLException ex)
             {
                System.err.println("aq.executeUpdate: " + ex.getMessage());
              }
      }
      public void disconnectToDB() throws
          java.sql.SQLException{
                  
               if(rs!=null)
                 {
                     rs.close();
                     rs = null;  
                  }   
               
               if(stmt!=null){
                     stmt.close();
                     stmt = null;  
                  }           if(conn!=null)
                 {
                     conn.close();
                     conn = null;  
                  } 
            }
    }