在使用jdbc進行兩節資料庫時,會用到 Statement 和 PreparedStatement兩個不同的對象進行執行sql語句的步驟,不過兩個對象之間有着相同和不同點,一起來看看吧
相同點
1、首先要說的是 他們兩個都是接口,PerparedStatement 接口繼承于 Statement 接口,顯而易見,PreparedStatement會比Statement 相對來說功能更完備。
2、他們都是用來執行sql 語句的 ,無論如何花裡胡哨,他們的根本作用不會變化。
一個簡單的運用Statement 的代碼示範
import java.sql.*;
public class Dbutil {
public static void main(String[] args) {
Dbutil.select();
}
public static void select(){
Connection conn;
Statement statement;
ResultSet res;
try {
Class.forName("com.mysql.jdbc.Driver");//加載驅動
String url="jdbc:mysql://localhost:3306/aaa";//資料庫名字aaa
String user="root";
String password="root";
//java連結資料庫的通路,利用mysql驅動與資料庫進行連接配接
conn = DriverManager.getConnection(url, user, password);
//通路上的車
statement = conn.createStatement();//用Statement 對象作為執行sql語句的對象
//定義sql語句,車上放的貨物
String sql="select * from student where id = 1";
res = statement.executeQuery(sql);
while(res.next()){ //執行 ResultSet.next()方法,傳回boolean類型值,如果res 中有内容傳回true,反之傳回false
int id = res.getInt(1); //利用 ResultSet 對象的方法擷取其現在其中的值
String name = res.getString(2);
int age = res.getInt(3);
System.out.println(id+" "+name+" "+age);
}
conn.close(); //将上面用到的資源進行釋放
statement.close();
res.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
不同點
1、建立的方法不同Statement 對象的建立方法:
Statement statement = conn.createStatement(); //不用傳入參數
PreparedStatement對象的建立方法:
PerparedStatement pre = conn.prepareStatement(sql); //這裡必須傳入參 數
2、執行的順序不同,我這裡說的不同并不是他們在整個過程中的順序不同,隻是在執行sql語句時的這兩個對象有所不同
Statement對象是在executeUpdate或executeQuery方法時指定sql,此時将sql語句發送和執行。
Statement s = conn.createStatement();
String sql = "INSERT into stu values(3,'名字)";
int count = s.executeUpdate(sql);
PreparedStatement:表示預編譯的 SQL 語句的對象,PrepareStatement對象是在建立時指定并發送sql,在executeUpdate或executeQuery方法時觸發sql執行。 SQL 語句被預編譯并存儲在 PreparedStatement對象中。然後可以使用此對象多次高效地執行該語句
String sql2 = "UPDATE account set salary=salary-? where id=?";
PeraredStatement pre = conn.prepareStatement(sql);
pre.setDouble(1,500);
pre.setInt(2,2);
3、sql語句編寫的不同,Statement 對象用executeQuery 方法或者executeUpdate方法執行sql 時 sql語句是一定的,而 PreparedStatement對象是先預編譯sql語句,其中需要的參數用 ? 代替,之後在用方法對 ’?’ 進行指派
PreparedStatement對象對 ’?’ 進行指派的方法
a、指派基本類型 ,當然不止圖檔中的,還有很多
b、對其他類型指派
一個簡單的運用PreparedStatement的代碼示範實作登入判斷
import java.sql.*;
import java.util.Scanner;
public class login {
public static void main(String[] args) {
System.out.println(login.select());
}
public static boolean select(){
Connection conn = null;
PreparedStatement pre = null;
ResultSet res =null;
System.out.println("輸入使用者名和密碼");
Scanner sc = new Scanner(System.in);
String username = sc.nextLine();
String userpassword = sc.nextLine();
try {
Class.forName("com.mysql.jdbc.Driver");//加載驅動
String url="jdbc:mysql://localhost:3306/aaa";//資料庫名字aaa
String user="root";
String password="root";
conn = DriverManager.getConnection(url, user, password);
String sql="select * from users where username=? and password=?";
pre = conn.prepareStatement(sql);
pre.setString(1,username);
pre.setString(2,userpassword);
res = pre.executeQuery();
return res.next();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
conn.close();
pre.close();
res.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
}