Single thread version/** * */ImportJava.sql.*;Importjava.util.Date;ImportOrg.omg.CORBA.PUBLIC_MEMBER;/** * @authorAdministrator **/ Public classMysql { Public voidChaxun () {Try{class.forname ("Com.mysql.jdbc.Driver"); } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); } String URL= "Jdbc:mysql://localhost:3306/beijing";//path of the JABCConnection Connection; Try{Connection=drivermanager.getconnection (URL, "root", "Liwanlei"); Statement Statement=connection.createstatement (); String SQL= "SELECT * FROM student where id=1"; ResultSet ResultSet=statement.executequery (SQL); if(Resultset.next ()) {System.out.println (resultset.getstring ("Name")); } statement.close (); Connection.close (); } Catch(Exception e) {//Todo:handle ExceptionSystem.out.println (e); } } Public voidChange () {Try{class.forname ("Com.mysql.jdbc.Driver"); } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); System.out.println (exception); } String URL= "Jdbc:mysql://localhost:3306/beijing";//path of the JABCConnection Connection; Try{Connection=drivermanager.getconnection (URL, "root", "Liwanlei"); Statement Statement=connection.createstatement (); String SQL= "UPDATE student SET name=?" WHERE id=? "; PreparedStatement PreparedStatement=connection.preparestatement (SQL); Preparedstatement.setstring (1, "Liwanlei"); Preparedstatement.setint (2, 1); Preparedstatement.executeupdate (); System.out.println ("OK");} Catch(Exception e) {//Todo:handle ExceptionSYSTEM.OUT.PRINTLN ("Exception"); } } Public voidadddate () {Date adate=NewDate (); Try{class.forname ("Com.mysql.jdbc.Driver"); } Catch(Exception e) {//Todo:handle ExceptionSYSTEM.OUT.PRINTLN ("Load driver failed"); } String URL= "Jdbc:mysql://localhost:3306/beijing"; Connection Connection; Try{Connection=drivermanager.getconnection (URL, "root", "Liwanlei"); Statement Statement=connection.createstatement (); String SQL= "INSERT into student (id,name,age,address) VALUES (?,?,?,?)"; PreparedStatement PreparedStatement=connection.preparestatement (SQL); for(inti=1;i<10;i++) {Preparedstatement.setint (1, i); Preparedstatement.setstring (2, "Liwanddlei" +i); Preparedstatement.setint (3, i*333); Preparedstatement.setstring (4, "beijiddng" +i); Preparedstatement.executeupdate (); } preparedstatement.close (); Connection.close (); Date Date=NewDate (); System.out.println ((Date.gettime ()-adate.gettime ())); System.out.println ("Inserting Success"); } Catch(Exception e) {//Todo:handle Exception } } Public Static voidMain (string[] args) {//TODO auto-generated Method StubMysql mysql=NewMysql (); Mysql. Adddate (); }}
Multi-threaded Insert versionImportjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.PreparedStatement;Importjava.sql.Statement;Importjava.util.Date;/** * *//** * @authorLileilei *threadmyql.java*2017 December 9*/ Public classThreadmyqlextendsthread{@Override Public voidrun () {Date adate=NewDate (); Try{class.forname ("Com.mysql.jdbc.Driver"); } Catch(Exception e) {//Todo:handle ExceptionSYSTEM.OUT.PRINTLN ("Load driver failed"); } String URL= "Jdbc:mysql://localhost:3306/beijing"; Connection Connection; Try{Connection=drivermanager.getconnection (URL, "root", "Liwanlei"); Statement Statement=connection.createstatement (); String SQL= "INSERT into student (id,name,age,address) VALUES (?,?,?,?)"; PreparedStatement PreparedStatement=connection.preparestatement (SQL); for(inti=1;i<100000;i++) {Preparedstatement.setint (1, i); Preparedstatement.setstring (2, "ASDASDASD" +i); Preparedstatement.setint (3, i*333); Preparedstatement.setstring (4, "ASDASDASDASDASDASD" +i); Preparedstatement.executeupdate (); } preparedstatement.close (); Connection.close (); Date Date=NewDate (); System.out.println ((Date.gettime ()-adate.gettime ())/1000); System.out.println ("Inserting Success"); } Catch(Exception e) {//Todo:handle Exception } } Public Static voidMain (string[] args) { for(inti = 1; I <=10; i++) { Newthreadmyql (). Start (); } }}
Data Association is small, there is no need to do performance, did not need to find
Java operation Local Database MySQL