抽取JDBC工具类:JDBCUtils
目的:简化书写
*分析
*1.注册驱动也抽取
*2.抽取一个方法获取连接对象
*需求: 不想传递参数,还得保证工具类的通用性。
*解决:配置文件
*3.抽取一个方法释放资源
package cn.hncj3;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
/**
* Created on 2022/2/25.
* 获取连接
*
* @author Hou chaof
*/
public class JDBCUtils {
/**
* Description: 文件的读取,只需要读取一次即可拿到这些值,使用静态代码块。
*
* @date:2022/2/25 16:01
* @params: []
* @return: java.sql.Connection
*/
private static String url;
private static String user;
private static String password;
private static String driver;
static {
try {
//读取资源文件,获取值
//1.创建Properties集合类
Properties properties = new Properties();
//2.加载文件
properties.load(new FileReader("src/jdbc.properties"));
//3.获取数据,赋值
url = properties.getProperty("url"); //后边的url要和配置中的一样,不然读取不到值
user = properties.getProperty("user");
password = properties.getProperty("");
driver = properties.getProperty("driver");
//4.注册驱动
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
//释放资源
/* public static void close(Statement statement, Connection connection) {
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
*/
public static void close(ResultSet resultSet, Statement statement, Connection connection) {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
package cn.hncj2;
import cn.hncj3.JDBCUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created on 2022/2/25.
* 演示Utils类
*
* @author Hou chaof
*/
public class JdbcStuTest2 {
public static void main(String[] args) {
List<JdbcStuTest> list = new JdbcStuTest2().findAll2();
System.out.println(list);
System.out.println(list.size());
}
public List<JdbcStuTest> findAll() {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
List<JdbcStuTest> list = null;
try {
//1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//2. 获取连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "root", "");
//3.定义sql
String sql = "select * from stu";
//4.获取执行sql的对象statement
statement = connection.createStatement();
//5.执行sql
resultSet = statement.executeQuery(sql);
//6.遍历结果集,封装对象,装载集合
list = new ArrayList<JdbcStuTest>();
while (resultSet.next()) {
//获取数据
int id1 = resultSet.getInt("id");
String stuname = resultSet.getString("stuname");
int age = resultSet.getInt("age");
String addr = resultSet.getString("addr");
int math = resultSet.getInt("math");
int english = resultSet.getInt("english");
String sex = resultSet.getString("sex");
//创建对象
JdbcStuTest jdbcStuTest = new JdbcStuTest();
jdbcStuTest.setId(id1);
jdbcStuTest.setName("stuname");
jdbcStuTest.setAge(age);
jdbcStuTest.setAddr("addr");
jdbcStuTest.setMath(math);
jdbcStuTest.setEnglish(english);
jdbcStuTest.setSex("sex");
list.add(jdbcStuTest);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
//避免空指针异常
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
return list;
}
public List<JdbcStuTest> findAll2() {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
List<JdbcStuTest> list = null;
try {
connection = JDBCUtils.getConnection();
//3.定义sql
String sql = "select * from stu";
//4.获取执行sql的对象statement
statement = connection.createStatement();
//5.执行sql
resultSet = statement.executeQuery(sql);
//6.遍历结果集,封装对象,装载集合
list = new ArrayList<JdbcStuTest>();
while (resultSet.next()) {
//获取数据
int id1 = resultSet.getInt("id");
String stuname = resultSet.getString("stuname");
int age = resultSet.getInt("age");
String addr = resultSet.getString("addr");
int math = resultSet.getInt("math");
int english = resultSet.getInt("english");
String sex = resultSet.getString("sex");
//创建对象
JdbcStuTest jdbcStuTest = new JdbcStuTest();
jdbcStuTest.setId(id1);
jdbcStuTest.setName("stuname");
jdbcStuTest.setAge(age);
jdbcStuTest.setAddr("addr");
jdbcStuTest.setMath(math);
jdbcStuTest.setEnglish(english);
jdbcStuTest.setSex("sex");
list.add(jdbcStuTest);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
/*//避免空指针异常
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}*/
JDBCUtils.close(resultSet,statement,connection);
}
return list;
}
}