JDBC常用接口
Statement接口
语句:执行SQL语句,执行操作的核心
- 用于执行静态SQL语句并返回它所生成结果的对象(Statement对象用于将 SQL 语句发送到数据库)
- 三种Statement类:
• Statement:
由createStatement创建,用于执行不带参数的简单SQL语句;
• PreparedStatement(从 Statement 继承):
用于执行带或不带参数的预编译SQL语句;
用于发送含有一个或多个输入参数的sql语句,比第一种效率高,可以防止SQL注入。一般使用这个
• CallableStatement(从PreparedStatement 继承):
用于执行数据库存储过程的调用。
由方法prePareCall创建,用于调用存储过程
SQL注入
- Statement的基本用法:
package com.first.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 测试Statement接口的用法,执行SQL语句,以及SQL注入问题
*/
public class Demo02 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
//1.加载驱动类
Class.forName("com.mysql.jdbc.Driver");
//2.建立连接(连接对象内部其实包含了Socket对象,是一个远程的连接。比较耗时!这是Connection对象管理的一个要点!)
//真正开发中,为了提高效率,都会使用连接池来管理连接对象!
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc","root","root");
//3.获得连接——获得对象
stmt = conn.createStatement();
String name = "代红";
String sql = "insert into t_user (username,pwd,regTime) values ('"+name+"',12345,now())";
stmt.execute(sql);
//以上三句写入了SQL语句
//测试SQL注入
String id = "5 or 1=1 ";//用这种方式,一直为true,所以容易被修改
String sql = "delete from t_user where id="+id;
stmt.execute(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(stmt!=null){
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
- PreparedStatement的基本用法:
package com.first.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction;
public class Demo03 {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement ps = null;//PreparedStatement是一个接口
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc","root","root");
String sql = "insert into t_user (username,pwd,regTime) values (?,?,?)";
//?占位符——避免SQL注入,?只能是字符串或者是数字或者是时间,有预处理机制
ps = conn.prepareStatement(sql);
// ps.setString(1, "代红1"); //参数索引是从1开始计算, 而不是0
// ps.setString(2, "123456");
// ps.setDate(3, new java.sql.Date(System.currentTimeMillis()));传入当前时间
//可以使用setObject方法处理参数
ps.setObject(1, "代红2");
ps.setObject(2, "34567");
ps.setObject(3, new java.sql.Date(System.currentTimeMillis()));
System.out.println("插入一行记录");
// ps.execute();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(ps!=null){
ps.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
- CallableStatement不常用,有需要的可以自行查找
常用的Statement方法:
方法 | 作用 |
---|---|
boolean execute(String sql) | 可以执行任意SQL语句,然后获得一个布尔值,表示是否返回 |
ResultSet executeQuery(String sql) | 执行SQL查询并获取到ResultSet对象 |
int executeUpdate(String sql) | 可以执行插入、删除、更新等操作,返回值是执行该操作所影响的行数 |
execute——true:有结果对象;false:一个数/没有结果集
executeUpdate——返回数字:更新的行数
executeQuery——只能查询,返回结果集对象
补充:
prepareStatement是Connection类的一个方法,作用是对sql语句进行预处理,发到数据库等待执行
该方法执行完之后会返回一个prreparedStatement对象,可以通过它设置sql中带有占位符的值,执行预处理sql语句等操作