Java operation Local Database MySQL

Source: Internet
Author: User

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

Related Article

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.