在使用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;
}
}