Java Connection MySQL DB instance

Source: Internet
Author: User

Do the game client more than a year, in the University of Java SSH, basically forget, today to see the basic connection to the database has forgotten ... Too horrible for this forgotten speed.

So write an example of a connection. Two new table Tx1,tx2 were created after the MySQL database was installed. Next, connect to the database and insert the data into the previous two tables.

The first is the public connection class:

Testconnection.java

 PackageCn.wan;Importjava.sql.Connection;ImportJava.sql.*; Public classtestconnection {PrivateString driver; PrivateString URL; PrivateString DbName; PrivateString password;    Connection Conn;    Statement STA;        PreparedStatement prepare;  Publictestconnection () { This. Driver = "Com.mysql.jdbc.Driver";  This. url = "Jdbc:mysql://localhost:3306/tx";  This. dbName = "root";  This. Password = "126"; }         PublicConnection getconnection ()throwsException {Try{class.forname (driver); Conn=drivermanager.getconnection (URL, dbName, password); } Catch(Exception e) {e.printstacktrace (); }        returnConn; }         Public voidCloseconn () {Try {            if( This. conn!=NULL)            {                 This. Conn.close (); }        } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); }    }}

Using the statement class to insert data into the TX1, it is important to note that the key of the TX1 table is an integral type, so note the notation for inserting the data.

 PackageCn.wan;Importjava.sql.Connection;Importjava.sql.Statement; Public classteststatement {//private static testconnection connection;         Public Static voidMain (string[] args)throwsException {Connection conn; Statement State=NULL; Long Start=System.currenttimemillis (); Testconnection Connection=Newtestconnection (); Try{conn=connection.getconnection (); State=conn.createstatement (); //you need to use 100 SQL statements to insert data             for(intI= 0;i<100;i++)            {                intnum=i; String str2= "Name" +i; State.executeupdate ("INSERT into TX1 values ('" +num+ "', ' str" +i+ "')"); } System.out.println ("Use statement time:" + (System.currenttimemillis ()-start)); } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); }finally{connection.closeconn (); if(state! =NULL) {state.close (); }        }                    }}

Use the PreparedStatement class to insert data into the TX2 table, and also pay attention to the wording of his statement:

 PackageCn.wan;Importjava.sql.Connection;Importjava.sql.PreparedStatement;Importjava.sql.Statement; Public classtestprepared { Public Static voidMain (string[] args)throwsException {String sqlString= "INSERT into TX2 values (?,?)"; Connection Conn=NULL; PreparedStatement State=NULL; Long Startlong=System.currenttimemillis (); Testconnection Connection=Newtestconnection (); Try{conn=connection.getconnection (); //use connection to create a Preparedstatemet objectState =conn.preparestatement (sqlString); //100 times to assign a value to the Preparedstatemet parameter, you can insert 100 records             for(inti = 0;i<100;i++) {State.setint (1, i); State.setstring (2, i+ "");            State.executeupdate (); } System.out.println ("Use Preparedstatemet Time:" + (System.currenttimemillis ()-Startlong)); } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); }        finally{connection.closeconn (); if(state! =NULL) {state.close (); }        }    }}

This connection and the two different data insertions are complete.

Java Connection MySQL DB instance

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.