MySQL with a simple JDBC operation

Source: Internet
Author: User

/*
Comparison between statement and PreparedStatement
Each time the SQL statement executes, the database executes the compilation of the SQL statement, preferably in the case where only one query is executed and the result is returned, and there is a SQL injection risk.
PreparedStatement is more efficient than statement when executing a variable-parameter SQL, because it is certainly more efficient for a DBMS to precompile a SQL than to compile a single SQL.
Security is better, effectively prevent SQL injection problem, due to the use of car-cleaning mechanism, pre-compiled statements will be placed in the cache, the next time the same SQL statement, you can directly from the
Cache, which greatly improves the efficiency of execution.
*/
Law One statement use
Public Static voidMain (string[] args) {//Import the driver's jar package first (you can download mysql jdbc driver online), right-click Properties-->java build path-->add external jarsString URL= "Jdbc:mysql://localhost:3306/student";//the URL address of the connection databaseString username= "root";//user name of the databaseString password= "123";//password for the databaseConnection con=NULL; Try { //1. Load MySQL Database driverClass.forName ("Com.mysql.jdbc.Driver"); //2. Establish the database connection, connection is the java.sql packagecon=drivermanager.getconnection (URL, username,password); if(con!=NULL) {System.out.println ("Database connection succeeded"); }Else{System.out.println ("Database Connection not successful"); } //3 Creating SQL statement objectsStatement statement=con.createstatement (); //String sql= "insert into login values (' Xiaoge ', ' female ', ' Shandong ')"; //String sql= "Delete from login where name= ' Jack '";String sql= "Update login set name= ' Lucy ' where city= ' Nanjing '"; //4 EXECUTE Statement intR=statement.executeupdate (SQL); if(r>0) { //System.out.println ("Insert success.."); //System.out.println ("Update success..");System.out.println ("Delete success ..."); } Else { //System.out.println ("fail.."); } } Catch(Exception e) {e.printstacktrace (); //If an error occurs, print the exception information at the command line where and why the error occurred in the program. } finally { //5. Releasing Resources if(con!=NULL) { Try{con.close (); Con=NULL; } Catch(SQLException e) {e.printstacktrace (); } } } }
//Fah PreparedStatement
Public Static voidMain (string[] args) {Try{class.forname ("Com.mysql.jdbc.Driver"); Connection Connection=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/student", "root", "123"); String sqlString= "INSERT into login values (?,?,?)"; PreparedStatement PreparedStatement=connection.preparestatement (sqlString); intrset=preparedstatement.executeupdate (); //small Mark starts at 1//preparedstatement.setstring (1, "Lifang");//preparedstatement.setstring (2, "female");//preparedstatement.setstring (3, "Guangdong");preparedstatement.setstring (1, "Tom"); Preparedstatement.setstring (2, "Man"); Preparedstatement.setstring (3, "Beijing"); intresult=preparedstatement.executeupdate (); if(result>0) {System.out.println ("Insert Success ..."); } Else{System.out.println ("Insert Fail ..."); } } Catch(Exception e) {e.printstacktrace (); } }

MySQL with a simple JDBC operation

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.