PreparedStatement的增删改操作
1.首先我们要知道Statement是干什么的
create a statement object for senting SQL statement to the databases
意思就是:创建一个语句对象,用于向数据库发送SQL语句
简单来说就是用这个类来对数据库进行增删改查
2.为什么不用statement用preparedstatement,
主要有两点:
一:拼接串过于麻烦
二:SQL注入问题(可以自行百度)
所以我们用他的子接口来实现这个方法
3.什么是preparedstatement
an object that represents a precompiled SQL statement
大致意思为:表示预编译SQL语句的对象
4.我会根据讲三种方法来演示(我直接写函数,具体的自己在类中加载就行,以及customers这个表我已经插入进去了)
4.1
// 向customers表中添加一条记录
@Test
public void testInsert() {
Connection conn = null;
PreparedStatement ps = null;
try {
// 1.读取配置文件中的4个基本信息
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
// 2.加载驱动
Class.forName(driverClass);
// 3.获取连接
conn = DriverManager.getConnection(url, user, password);
// System.out.println(conn);
//4.预编译sql语句,返回PreparedStatement的实例
String sql = "insert into customers(name,email,birth)values(?,?,?)";//?:占位符
ps = conn.prepareStatement(sql);
//5.填充占位符
ps.setString(1, "哪吒");
ps.setString(2, "nezha@gmail.com");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date date = sdf.parse("1000-01-01");
ps.setDate(3, new Date(date.getTime()));
//6.执行操作
ps.execute();
} catch (Exception e) {
e.printStackTrace();
}finally{
//7.资源的关闭
try {
if(ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

这个里面我要说明一个点
就是ps.setDate这个方法他是在Java.util.Date不是SQL语句下的Date,所以要进行转换
4.2因为增删改的操作无非就是SQL语句的不同
链接数据库,关闭啥的方法都重复,所以我们考虑用一个类来封装
类中直接用两个方法,一个链接方法,一个关闭方法,直接调用方法就可
package com.atguigu3.util;
import com.atguigu1.connection.ConnectionTest;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
*
*
*
*/
public class JDBCUtils {
public static Connection getConnection() throws ClassNotFoundException, IOException, SQLException {
//1.读取配置文件中的4个基本信息
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driver");
//2.加载驱动
Class.forName(driverClass);
//3.获取连接
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
return conn;
}
public static void closeResource(Connection conn, Statement ps){
try {
if(ps != null)
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
利用方法改良版增删改来啦!
//修改customers表的一条记录 @Test public void testUpdate(){ Connection conn = null; PreparedStatement ps = null; try { //1.获取数据库的连接 conn = JDBCUtils.getConnection(); //2.预编译sql语句,返回PreparedStatement的实例 String sql = "update customers set name = ? where id = ?"; ps = conn.prepareStatement(sql); //3.填充占位符 ps.setObject(1,"莫扎特"); ps.setObject(2, 18); //4.执行 ps.execute(); } catch (Exception e) { e.printStackTrace(); }finally{ //5.资源的关闭 JDBCUtils.closeResource(conn, ps); } }
这不就少了很多,但是还可以减少!!
4.3 不清楚需要几个占位符?,所以可以用一个args直接代替即可,到时候一个for循环就可以解决
@Test public void TestCommonUpdate() { String sql = "delete from customers where id = ?"; update(sql,3); }
public void update(String sql,Object...args) { Connection conn = null; PreparedStatement ps = null; try { conn = JDBCUtils.getConnection(); ps = conn.prepareStatement(sql); for (int i = 0; i < args.length;i++) { ps.setObject(i+1,args[i]); } ps.execute(); } catch (ClassNotFoundException | SQLException | IOException e) { e.printStackTrace(); } finally { JDBCUtils.closeResource(conn,ps); }
这几个关系是递进关系,得需要一个个搞明白才行
遇见问题(已解决):
1.Date的util下还是SQL语句下的Date的转换问题

浙公网安备 33010602011771号