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.