回顾:
封装:临时存放我们的查询出来的记录
集合:存放所有的记录对象,取出所有的查询出来的记录
封装用来生产实体类。
集合用来存放实体类。
单元测试,导致依赖junit4
JDBC增加、删除、修改
1.JDBC查询
查询 增加、删除、修改
加载驱动 加载驱动 Class.forName() Class.forName()
创建连接 创建连接 Connection Connection
创建SQL执行对象 创建SQL执行对象 Statement Statement
执行SQL ,有结果集 执行SQL没有结果集 ResultSet 无
通过实体类存入集合 释放资源 List 实体类 close()
释放资源 close()
2、抽取工具类
加载驱动 、创建连接;
释放资源;
(1)创建配置文件
resources ----> jdbc.properties
driver= com.mysql.cj.jdbc.Driver
url = jdbc:mysql://localhost:3306/ooo
username = root
password = 123456
(2)编写工具类
static {
//1-2-1将配置文件,转换成输入流
InputStream resourceAsStream = jdbc_Utils.class.getClassLoader().getResourceAsStream("jdbc.properties");
//1-2-2 、通过属性对象(Properties),关联输入流
Properties properties = new Properties();
try {
properties.load(resourceAsStream);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
Class.forName(driver);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}finally {
try {
resourceAsStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
(2-1)编写获取连接方法 ,读取配置文件加载驱动
public static Connection getConnection(){
Connection connection = null;
try {
DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
throw new RuntimeException(e);
}
return connection;
}
(2-2) 编写释放资源方法
public static void closeAll(ResultSet resultSet, Statement statement,Connection connection) throws SQLException {
if (resultSet != null){
resultSet.close();
}
if (statement!= null){
statement.close();
}
if (connection != null){
connection.close();
}
}
3.工具类的使用
工具类为我们解救了2个问题 :(1)获取连接 (2)释放资源
@Test
// public void jdbcutilselect() throws SQLException {
// String select_sql = "select * from user";
// Connection connection = jdbc_Utils.getConnection();
// Statement statement = connection.createStatement();
// ResultSet resultSet = statement.executeQuery(select_sql);
// List<user> users = new ArrayList<>();
//
// 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"));
// users.add(user);
// }
// for (user i : users) {
// System.out.println(i.toString());
// }
//
// jdbc_Utils.closeAll(resultSet,statement,connection);
//
// }
// @Test
// public void jdbcutilselect01() throws SQLException {
// String select_sql = "select * from user where id = 1";
// Connection connection = jdbc_Utils.getConnection();
// Statement statement = connection.createStatement();
// ResultSet resultSet = statement.executeQuery(select_sql);
// List<user> users = new ArrayList<>();
//
// 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"));
// users.add(user);
// }
// for (user i : users) {
// System.out.println(i.toString());
// }
//
// jdbc_Utils.closeAll(resultSet, statement, connection);
//
// }
@Test
public void jdbcdelete() throws SQLException {
String delete_sql = "delete from user where id = 3";
Connection connection = jdbc_Utils.getConnection();
Statement statement = connection.createStatement();
statement.executeUpdate(delete_sql);
jdbc_Utils.closeAll(null, statement, connection);
}