To test the maximum number of MySQL connections with Java programs

Source: Internet
Author: User

When working previously because the Oracle8i database often appears the user too many errors, due to the large amount of data, often appear ora:12500 error, but the main reason is too much access caused, Later, Java wrote a simple JDBC connection to test the number of Oracle connections to the program. Now common MySQL, so also write a simple test its maximum number of connections to the program, introduced here to everyone:

 import java.sql.Connection;
 Import Java.sql.DriverManager;
 Import java.sql.Statement;
 Import Java.sql.ResultSet;
 Import java.sql.SQLException;
 Import java.io.IOException;
     Class testmaxconn{public static void Main (String args[]) {int count=0;
     Connection []conn=new connection[1000];
     Statement []stmt =new statement[1000];
    ResultSet []rs =new resultset[1000]; try{class.forname ("com.
MySQL.jdbc.Driver "). newinstance (); for (count=0;count<300;count++) {Conn[count] = drivermanager.getconnection ("Jdbc:mysql://localhost/mysql", "root
      ", "");
      Stmt[count]=conn[count].createstatement ();
      Rs[count]=stmt[count].executequery ("SELECT * from user");  
      while (Rs[count].next ()) {//system.out.println (rs.getstring (1) + "T" +rs.getstring (2));
     } System.out.print (count+ "\ t");
    }}catch (SQLException ex1) {System.out.println ("\ n" +ex1.tostring ()); }catch (Instantiationexception ex2) {System.out.println ("\ n" +ex2.toString ());
    }catch (classnotfoundexception ex3) {System.out.println ("\ n" +ex3.tostring ());
    }catch (illegalaccessexception ex4) {System.out.println ("\ n" +ex4.tostring ()); }finally{try{System.out.println ("\nsystem has opened" +count--+ "MySQL connections.\npress Enter key to"
        Close the connections ");
        System.in.read ();
        System.out.println ("\nclose The Connections:");
         for (; count>=0;count--) {rs[count].close ();
         Stmt[count].close ();
         Conn[count].close ();
        System.out.print (count+ "T");
      }}catch (SQLException ex) {System.out.println ("\ Close Connection exception:" +ex.tostring ()); }catch (IOException io_ex) {}}//end the ' I ' Try '}} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.