Java to operate on the local database mysql and javamysql
Single-threaded version/*****/import java. SQL. *; import java. util. date; import org. omg. CORBA. PUBLIC_MEMBER;/*** @ author Administrator **/public class Mysql {public void Chaxun () {try {Class. forName ("com. mysql. jdbc. driver ");} catch (Exception e) {// TODO: handle exception e. printStackTrace ();} String url = "jdbc: mysql: // localhost: 3306/beijing"; // Connection connection of jabc; try {connection = DriverManager. getCon Nection (url, "root", "liwanlei"); Statement statement = connection. createStatement (); String SQL = "select * from student where id = 1"; ResultSet resultSet=statement.exe cuteQuery (SQL); if (resultSet. next () {System. out. println (resultSet. getString ("name");} statement. close (); connection. close ();} catch (Exception e) {// TODO: handle exception System. out. println (e) ;}} public void Change () {try {Class. forName ("com. mysql. jdbc. driver ");} catch (Exception e) {// TODO: handle exception e. printStackTrace (); System. out. println ("exception");} String url = "jdbc: mysql: // localhost: 3306/beijing"; // The jabc path 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.exe cuteUpdate (); System. out. println ("OK");} catch (Exception e) {// TODO: handle exception System. out. println ("exception") ;}} public void Adddate () {Date aDate = new Date (); try {Class. forName ("com. mysql. jdbc. driver ");} catch (Exception E) {// TODO: handle exception System. out. println ("failed to load the driver");} 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 (int I = 1; I <10; I ++) {preparedStatement. setInt (1, I); preparedStatement. setString (2, "liwanddlei" + I); preparedStatement. setInt (3, I * 333); preparedStatement. setString (4, "beijiddng" + I); preparedStatement.exe cuteUpdate ();} preparedStatement. close (); connection. close (); Date date = new Date (); System. out. println (date. getTime ()-aDate. getTime (); System. out. println ("successfully inserted");} catch (Exception e) {// TODO: handle exception} public static void main (String [] args) {// TODO Auto-generated method stub Mysql mysql = new Mysql (); mysql. adddate ();}}
Multi-threaded insert version import java. SQL. connection; import java. SQL. driverManager; import java. SQL. preparedStatement; import java. SQL. statement; import java. util. date;/***** // **** @ author lileilei * Threadmyql. java * December 9, 2017 */public class Threadmyql extends Thread {@ Override public void run () {Date aDate = new Date (); try {Class. forName ("com. mysql. jdbc. driver ");} catch (Exception e) {// TODO: handle exception cause e M. out. println ("failed to load the driver");} 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 (int I = 1; I <100000; I ++) {preparedStatement. setInt (1, I); preparedStatement. setString (2, "asdasdasd" + I); preparedStatement. setInt (3, I * 333); preparedStatement. setString (4, "asdasdasdasdasd" + I); preparedStatement.exe cuteUpdate ();} preparedStatement. close (); connection. close (); Date date = new Date (); System. out. println (date. getTime ()-aDate. getTime ()/1000); System. out. println ("successfully inserted");} catch (Exception e) {// TODO: handle exception} public static void main (String [] args) {for (int I = 1; I <= 10; I ++) {new Threadmyql (). start ();}}}
There is little data relevance and no need to do performance. After a while, no need is found.