the difference between 1.Statement and PreparedStatement
(1) The database has a little advantage in executing SQL statements if you use the PreparedStatement statement: because the data
The library will precompile the PreparedStatement statement, and the next time the same SQL statement is executed, the database side does not
will be pre-compiled, and directly with the database buffer, improve the efficiency of data access
(2) Use the Statement object. When you perform a one-time access to the database, use the Statement object to place
Acting The overhead of PreparedStatement objects is larger than Statement, and for one-time operations it does not bring additional
The benefits.
(3) statement each execution of SQL statements, the relevant database to execute the compilation of SQL statements, PreparedStatement
Is precompiled, PreparedStatement supports batch processing
(4) Whether it is Statement or preparedstatement, adding and deleting the executeupdate ()
The new operation, while the query executes the executeQuery (), belongs to the query operation and is not mixed.
2. Statement execute the code to delete and change the search
import java.sql.*;
Public class Createtitle {
Public static void Main (string[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
class.forname ("Com.mysql.jdbc.Driver");//Registration driver, must lead to the project
} catch (ClassNotFoundException Cnfe) {
System.out.println ("No Drive class found");
}
try {
String sql = "Insert Intofirstleveltitle (Titlename,creator,createtime) VALUES (' Access database ', ' GXS ', Now ())";//Get connection
conn = Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/newsuseunicode=true&characterencoding=utf-8" , "Root", "123456");
stmt = Conn.createstatement ();//Create Statement Object
int line = stmt.executeupdate (sql);//Returns the number of rows that updated the record
System.out.println (line);
rs = Stmt.getgeneratedkeys ();//Get Results Centrally store update record primary key
Rs.next ();
System.out.println (Rs.getint (1));
} catch (SQLException SqlE) {
Sqle.printstacktrace ();
} finally {//Frees resources
try {
if (rs! = null)
Rs.close ();
if (stmt! = null)
Stmt.close ();
IF (conn! = null)
Conn.close ();
} catch (SQLException e) {
E.printstacktrace ();
}}}}
3. PreparedStatement execute the code to delete and change the search
Import java.sql.*;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
public class Createfisrstprepared {
public static void Main (string[] args) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {//1. Registration driver, prerequisite must load driver package
Class.forName ("Com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
SYSTEM.OUT.PRINTLN ("Driver not Found");
}
try {
conn = Drivermanager.getconnection ("Jdbc:mysql://localhost:3306/news", "root", "123456");
String sql = "Insert Intofirstleveltitle (Titlename,creator,createtime) VALUES (?,?,?)";
pstmt = conn.preparestatement (sql);//Precompiled SQL statements
To set parameters in an SQL statement
Pstmt.setstring (1, "preparedstatement exercise");
Pstmt.setstring (2, "GXS");
SimpleDateFormat formate = new SimpleDateFormat ("Yyyy-mm-dd hh:mm:ss");
Pstmt.setstring (3, Formate.format (New Date ()));
int linenum = Pstmt.executeupdate ();
System.out.println (LineNum);
rs = Pstmt.getgeneratedkeys ();
Rs.next ();
System.out.println (Rs.getint (1));
} catch (SQLException e) {
E.printstacktrace ();
} finally {
try {
if (rs! = null)
Rs.close ();
if (pstmt! = null)
Pstmt.close ();
IF (conn! = null)
Conn.close ();
} catch (SQLException e) {
E.printstacktrace ();
} } } }
(Note: In the code is the MySQL database, see a Intofirstleveltitle table, the table has titlename,creator,createtime three properties
Process the database, and add the MySQL jar package in the program to realize the function of connecting the database.
Statement and PreparedStatement (increase and revise) difference and code