站长论坛

标题: Mysql的最大连接数怎样用java程序测试 [打印本页]

作者: tznktg    时间: 2007-9-7 23:37
标题: Mysql的最大连接数怎样用java程序测试
以前工作的时候由于Oracle8i数据库经常出现用户过多的错误,由于数据量大,经常出现ORA:12500错误,但主要原因是访问过多而引起的,后来就用Java写了个简单的用JDBC连接来测试Oracle最大连接数的程序.现在常用MySQL,所以又写了一个简单的测试其最大连接数的程序,在此介绍给大家:
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.Statement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.io.IOException;

  7. class testMaxConn{
  8.    public static void main(String args[]){
  9.      int count=0;
  10.      Connection []conn=new Connection[1000];
  11.      Statement  []stmt =new Statement[1000];
  12.      ResultSet  []rs =new ResultSet[1000];
  13.     try{
  14.      Class.forName
  15. ("com.MySQL.jdbc.Driver").newInstance();
  16. for(count=0;count<300;count++){
  17. conn[count] = DriverManager.getConnection
  18. ("jdbc:mysql://localhost/MySQL", "root", "");
  19.       stmt[count]=conn[count].createStatement();
  20.       rs[count]=stmt[count].executeQuery
  21. ("SELECT * FROM user");
  22.       while (rs[count].next()){
  23.   //System.out.println(rs.getString(1)+
  24. "\t "+rs.getString(2));  
  25.       }
  26.       System.out.print(count+"\t");
  27.      }
  28.      
  29.      
  30.     }catch(SQLException ex1){
  31.       System.out.println("\n"+ex1.toString());
  32.     }catch(InstantiationException ex2){
  33.       System.out.println("\n"+ex2.toString());
  34.     }catch(ClassNotFoundException ex3){
  35.       System.out.println("\n"+ex3.toString());
  36.     }catch(IllegalAccessException ex4){
  37.       System.out.println("\n"+ex4.toString());
  38.     }finally{
  39.       try{
  40.         System.out.println("\nSystem has opened
  41. "+count--+" MySQL connections.\nPress Enter key
  42. to close the connections");
  43.         System.in.read();
  44.         System.out.println
  45. ("\nClose the Connections:");
  46.         for(;count>=0;count--){
  47.          rs[count].close();
  48.          stmt[count].close();
  49.          conn[count].close();
  50.          System.out.print(count+"\t");
  51.         }
  52.       }catch(SQLException ex){
  53.        System.out.println
  54. ("\n Close connection exception:"+ex.toString());
  55.       }catch(IOException io_ex){}
  56.     }//end the first "try"
  57.    }
  58. }
复制代码





欢迎光临 站长论坛 (https://www.tzlink.com/bbs/) Powered by Discuz! X3.2