Java inserts n data into the database

Source: Internet
Author: User
Tags mysql index

To test the MySQL index, insert tens of thousands of data into the database before testing. Manual insertion is too cumbersome to write a piece of code.

First on the code:

 PackageAction;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement; Public classInsertData { PublicConnection getconnection () {//driver nameString Driver = "Com.mysql.jdbc.Driver"; //the URL points to the database name you want to access ScutcsString url = "Jdbc:mysql://127.0.0.1:3306/wang"; //user name when MySQL is configuredString user = "root"; //password for MySQL configurationString password = "12345"; Connection Conn=NULL; Try {         //Load DriverClass.forName (driver); //Continuous Databaseconn =drivermanager.getconnection (URL, user, password); }        Catch(Exception e) {System.out.println ("Sorry,can ' t find the driver!"); E.printstacktrace ();//Todo:handle Exception        }        returnConn; }         Public intInsertData (intCount,string value)throwssqlexception{insertdata InsertData=NewInsertData (); Connection Conn=insertdata.getconnection (); if(!conn.isclosed ()) System.out.println ("Begin insert!"); //statement used to execute SQL statementsStatement Statement =conn.createstatement (); //the SQL statement to execute        intN=0;  for(inti = 0; I <=count; i++) {String SQL= "INSERT into student (Sno,sname) VALUES ('" +i+ "', '" +value+ "')"; N=statement.executeupdate (SQL); N++; } System.out.println ("Insert end!"); returnN; }            /**     * @paramargs *@throwsSQLException*/     Public Static voidMain (string[] args)throwsSQLException {//TODO auto-generated Method StubInsertData Insertdata=NewInsertData (); Connection Conn=insertdata.getconnection (); if(!conn.isclosed ()) System.out.println ("Succeeded connecting to the database!"); intN=0; N=insertdata.insertdata (10000, "Data content"); System.out.println ("The Count is:" +N); //statement used to execute SQL statementsStatement Statement =conn.createstatement (); //the SQL statement to executeString sql = "SELECT * FROM Student"; //result setResultSet rs =statement.executequery (SQL); System.out.println ("-----------------"); System.out.println (The results of the execution are as follows:); System.out.println ("-----------------"); System.out.println ("School Number" + "\ T" + "name"); System.out.println ("-----------------"); String name=NULL;  while(Rs.next ()) {name= Rs.getstring ("sname"); System.out.println (Rs.getstring ("Sno") + "\ T" +name);         } rs.close ();    Conn.close (); }}


This inserts the data too slowly, do not know whether the program is slow, or the database is too slow. This goes back to study again, waiting for the following.

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.