import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
public class demo03 {
/*
PreparedStatement 防止 SQL 注入的本质,把传递进来的参数当做字符
假设其中存在转义字符,比如说 ' 会被直接转义
*/
public static void main(String[] args) {
Connection conn = null;
PreparedStatement st = null;
try {
conn = JdbcUtils.getConnection();
//使用?占位符代替参数
String sql = "insert into test (`id`,`name`) values (?,?)";
//预编译 先写sql,不执行
st = conn.prepareStatement(sql);
//第一个参数是下标,代表语句中第一个问号
//第二个参数是值
st.setInt(1,1);
st.setString(2,"mahe666");
//注意一点,日期类在使用的时候要转换为 java.sql.Date 的类型
//不需要传入sql
st.execute();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}