Packagequery;Importjava.sql.Connection;ImportJava.sql.DriverManager;ImportJava.sql.ResultSet;Importjava.sql.SQLException;Importjava.sql.Statement;ImportJava.util.Random;ImportJava.util.jar.Attributes.Name; Public classQuery { Public Static voidMain (string[] args) {//driver nameString Driver = "Com.mysql.jdbc.Driver"; //URL point to the database name to access 9millionString url = "Jdbc:mysql://127.0.0.1:3306/9million"; //user name when MySQL is configuredString user = "root"; //password for MySQL configurationString Password = ""; Try { //Load DriverClass.forName (driver); //Continuous DatabaseConnection conn =drivermanager.getconnection (URL, user, password); if(!conn.isclosed ()) System.out.println ("Succeeded connecting to the database!"); //statement used to execute SQL statementsStatement Statement =conn.createstatement (); //the SQL statement to execute for(inti= 0; I <= 9000 000; i + +) {String rdname= RandomString (8); intID =i; System.out.println (Rdname);
missing quotation marks will make an error, cannot find Collumn "ID" or "Rdname"
int can not be added, but the type of string must be added.
Low-level implementation under the study of available space String SQLL = "INSERT into TestData (id,name) VALUES ('"+id+"','"+rdname+"')";
Statement.execute (SQLL); } conn.close (); } Catch(ClassNotFoundException e) {System.out.println ("Sorry,can ' t find the driver!"); E.printstacktrace (); } Catch(SQLException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } } /*** Generate random String **/ Private StaticRandom Randgen =NULL; Private Static Char[] Letters =NULL; Public Static FinalString RandomString (intlength) { if(Length < 1) { return NULL; } if(Randgen = =NULL) {Randgen=NewRandom ();//numbersandletters = ("0123456789abcdefghijklmnopqrstuvwxyz" +//"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"). ToCharArray ();Letters = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"). ToCharArray (); //numbersandletters = ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"). ToCharArray (); } Char[] Randbuffer =New Char[length]; for(inti=0; i<randbuffer.length; i++) {Randbuffer[i]= Letters[randgen.nextint (25)]; //Randbuffer[i] = Numbersandletters[randgen.nextint (+)]; } return NewString (Randbuffer); }}
9000 000 data randomly inserted in MySQL