首先定义工具类,工具类实现与数据库连接的操作和关闭资源操作(方法重载),jdbc.properties文件中(idea中我把他放在模块里面)负责修改连接服务器用户、服务器密码、服务器地址和相应的驱动。
package com.atguigu3.util;
import com.atguigu.connection.ConnnectionTest;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
public static Connection getConnection() throws Exception {
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");
Class.forName(driverClass);
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
public static void closeResource(Connection conn, PreparedStatement ps) {
try {
if (ps!=null){
ps.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if (conn!=null){
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static void closeResource(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (ps!=null){
ps.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if (conn!=null){
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if (rs!=null){
rs.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
然后实现增删改操作,主方法中前面具体的实现了增加和修改操作,类中写了一个通用的增删改操作,主方法测试正确。
package com.atguigu3.preparedstatement.crud;
import com.atguigu3.util.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
public class PrepareStatementUpdateTest {
public static void main(String[] args) {
//增加操作
/*Connection conn=null;
PreparedStatement ps=null;
try {
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");
Class.forName(driverClass);
conn= DriverManager.getConnection(url,user,password);
String sql="insert into customers(name,email,birth)values(?,?,?)";
ps=conn.prepareStatement(sql);
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 java.sql.Date(date.getTime()));
ps.execute();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
try {
if (ps!=null){
ps.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if (conn!=null){
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}*/
//修改操作
/* Connection conn= null;
PreparedStatement ps= null;
try {
conn = JDBCUtils.getConnection();
String sql="UPDATE `customers` SET name=? WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setObject(1,"莫扎特");
ps.setObject(2,18);
ps.execute();
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtils.closeResource(conn,ps);
}*/
//测试通用代码块
/*String sql="delete from customers where id=?";
update(sql,3);*/
String sql2="update `order` set order_name=? where order_id=?";
update(sql2,"DD","2");
}
//通用的增删改操作
public static 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 (Exception e) {
e.printStackTrace();
}finally{
JDBCUtils.closeResource(conn,ps);
}
}
}
可以发现这里调用update函数时,并没有提示是否增删改成功或者失败,所以这里还有进阶版本,重点在 return ps.executeUpdate()这个操作,用于返回增删改操作执行的次数,返回的是int类型的数据。
package com.atguigu4.exer;
import com.atguigu3.util.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Scanner;
public class Exer1Test {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("请输入用户名:");
String name=scanner.next();
System.out.println("请输入邮箱:");
String email=scanner.next();
System.out.println("请输入生日:");
String birthday=scanner.next();
String sql="insert into customers(name,email,birth) values(?,?,?)";
int insertCount=update(sql,name,email,birthday);
if(insertCount>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
}
public static int 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]);
}
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCUtils.closeResource(conn,ps);
}
return 0;
}
}
在讲到事务的时候,由于两个更新操作之间不能有关闭连接的操作,因为关闭连接会自动提交,上面的更新操作中数据库的连接都是关闭的,所以当有事务参与的时候不能更新一次就关闭一次,所以修改代码如下,删除数据库的连接和关闭,将数据库的连接交给事务处理。
public static int update(Connection conn,String sql,Object...args){
PreparedStatement ps= null;
try {
ps = conn.prepareStatement(sql);
for (int i=0;i<args.length;i++){
ps.setObject(i+1,args[i]);
}
return ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCUtils.closeResource(null,ps);
}
return 0;
}