回顾
PreparedStatement 预编译SQL执行对象
登录案例:
SQL使用替换符(?),按?号所在的位置去替换我们的变量。
CRUD
增加、删除、修改、通过ID查询、查询所有
package com.ww.jdbc.junit;
import com.ww.jdbc_demo_01.javabean.User;
import com.ww.jdbc_demo_01.util.JdbcUtils;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class JdbcDemo05 {
//增加
@Test
public void addUser() throws SQLException {
//1、增加SQL语句
// String sql="insert into user(username,password,nickname) values(?,?,?)";
String sql="insert into user values(null,?,?,?)";
//2、获取连接
Connection connection= JdbcUtils.getConnection();
//3、创建PreparedStatement对象
PreparedStatement preparedStatement=connection.prepareStatement(sql);
//4、替换替换符
preparedStatement.setString(1,"lisi");
preparedStatement.setString(2,"lisi123456");
preparedStatement.setString(3,"李四");
// 5、执行SQL
preparedStatement.executeUpdate();
// 6、释放资源
JdbcUtils.closeAll(null,preparedStatement,connection);
}
//修改
@Test
public void updateUser() throws SQLException {
//1、修改SQL语句
String sql="update user set nickname=? where id=?";
//2、获取连接
Connection connection=JdbcUtils.getConnection();
//3、创建PreparedStatement对象
PreparedStatement preparedStatement=connection.prepareStatement(sql);
//4、替换替换符
preparedStatement.setString(1,"小李子");
preparedStatement.setInt(2,2);
//5、执行SQL
preparedStatement.executeUpdate();//********
//6、释放资源
JdbcUtils.closeAll(null,preparedStatement,connection);
}
//删除
@Test
public void deleteUser() throws SQLException {
//1、删除SQL语句
String sql="delete from user where id=?";
//2、获取连接
Connection connection=JdbcUtils.getConnection();
//3、创建PreparedStatement对象
PreparedStatement preparedStatement=connection.prepareStatement(sql);
//4、替换替换符
preparedStatement.setInt(1,5);
//5、执行SQL
preparedStatement.executeUpdate();//********
//6、释放资源
JdbcUtils.closeAll(null,preparedStatement,connection);
}
//查询所有
@Test
public void selectAllUser() throws SQLException {
//1、查询所有SQL语句
String sql="select * from user";
//2、获取连接
Connection connection=JdbcUtils.getConnection();
//3、创建PreparedStatement对象
PreparedStatement preparedStatement=connection.prepareStatement(sql);
//4、替换替换符(无参数)
//5、执行SQL,拿到结果集
ResultSet resultSet=preparedStatement.executeQuery();
//6、取出结果集,存入到实体类;实体类存入到集合
List
while(resultSet.next()){
User user=new User();
user.setId(resultSet.getInt("id"));
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
user.setNickname(resultSet.getString("nickname"));
list.add(user);
}
//7、释放资源
JdbcUtils.closeAll(resultSet,preparedStatement,connection);
//8、集合处理,(1)返回return (2)打印输出了
for(User user:list){
System.out.println(user.toString());
}
}
//通过ID查询对应记录
@Test
public void selectByIdUser() throws SQLException {
//1、通过ID查询的SQL语句
String sql="select * from user where id=?";
//2、获取连接
Connection connection=JdbcUtils.getConnection();
//3、创建PreparedStatement对象
PreparedStatement preparedStatement=connection.prepareStatement(sql);
//4、替换替换符
preparedStatement.setInt(1,6);
//5、执行SQL,拿到结果集
ResultSet resultSet=preparedStatement.executeQuery();
//6、取出结果集,存入到实体类;实体类存入到集合
List
while(resultSet.next()){
User user=new User();
user.setId(resultSet.getInt("id"));
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
user.setNickname(resultSet.getString("nickname"));
list.add(user);
}
//7、释放资源
JdbcUtils.closeAll(resultSet,preparedStatement,connection);
//8、集合处理,(1)返回return (2)打印输出了
for(User user:list){
System.out.println(user.toString());
}
}
}
package com.ww.jdbc.junit;
import com.ww.jdbc_demo_01.util.JdbcUtils;
import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JdbcDemo06 {
//修改,增加事务步骤
@Test
public void updateUser(){
//1、修改SQL语句
String sql="update user set nickname=? where id=?";
//2、获取连接
Connection connection= JdbcUtils.getConnection();
PreparedStatement preparedStatement= null;
try {
//事务---开启事务---把事务从自动,调整为手动提交
connection.setAutoCommit(false);
//3、创建PreparedStatement对象
preparedStatement = connection.prepareStatement(sql);
//4、替换替换符
preparedStatement.setString(1,"小李子22222");
preparedStatement.setInt(2,2);
//5、执行SQL
preparedStatement.executeUpdate();//********
//事务---结束事务---事务提交
connection.commit();
} catch (SQLException e) {
try {
//事务---结束事务---事务回滚
connection.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
} finally {
//6、释放资源
JdbcUtils.closeAll(null,preparedStatement,connection);
}
}
}
//获取连接
Connection connection=JdbcUtils.getConnection();
PreparedStatement preparedStatement01=null;
PreparedStatement preparedStatement02=null;
try {
//开启手动提交事务
connection.setAutoCommit(false);
//创建预编译对象
preparedStatement01=connection.prepareStatement(sql);
preparedStatement02=connection.prepareStatement(sql);
//替换替换符
preparedStatement01.setDouble(1,-100);
preparedStatement01.setString(2,"zs");
preparedStatement02.setDouble(1,100);
preparedStatement02.setString(2,"ls");
//执行
preparedStatement01.executeUpdate();
preparedStatement02.executeUpdate();
//
// int i=1;
// i=i/0;
connection.commit();
} catch (SQLException e) {
try {
connection.rollback();
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
} finally {
JdbcUtils.closeAll(null,preparedStatement01,null);
JdbcUtils.closeAll(null,preparedStatement02,connection);
}
}