JDBC工具类
需求:抽取注册驱动,抽取一个方法获取连接对象,抽取一个方法释放资源
大佬们,帮我看一下代码哪里有问题
工具类代码:
package itcast.util;
import
import
import
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
static {
try {
//1.创建Properties集合类
Properties pro = new Properties();
//获取src路径下的文件的方式
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
String path = res.getPath();
//2,加载文件
pro.load(new FileReader(path));
//pro.load(new FileReader("E:\\itcast\\JDBC\\src\\jdbc.properties"));
//3.获取数据,赋值
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driver = pro.getProperty("driver");
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
public static void close(Statement stmt ,Connection conn) {
if ( stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void close ( ResultSet rs,Statement stmt ,Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
jdbc.properties文件代码:
"jdbc:mysql://localhost:3306/db3?useSSL=false&serverTimezone=Asia/Shanghai
user = root
password =root123
driver=com.mysql.cj.jdbc.Driver
需要使用工具类的测试代码:
import itcast.domain.Emp;
import itcast.util.JDBCUtils;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class JDBCDemo6 {
public static void main(String[] args) {
List<Emp> list = new JDBCDemo6().findAll();
System.out.println(list);
System.out.println(list.size());
}
public List<Emp> findAll() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List<Emp> list = null;
try {
//1.注册驱动
// Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
//String url = "jdbc:mysql://localhost:3306/db3?useSSL=false&&serverTimezone=Asia/Shanghai";
//conn = DriverManager.getConnection(url, "root", "root123");
//简化后测试
conn = JDBCUtils.getConnection();
//3.定义sql语句
String sql = "select * from emp";
//4.获取s执行sql的对象
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
Emp emp = null;
list = new ArrayList<Emp>();
while (rs.next()) {
int id = rs.getInt("id");
String ename = rs.getString("ename");
int job_id = rs.getInt("job_id");
int mgr = rs.getInt("mgr");
Date joindate = rs.getDate("joindate");
double salary = rs.getDouble("salary");
double bonus = rs.getDouble("bonus");
int dept_id = rs.getInt("dept_id");
emp = new Emp();
emp.setId(id);
emp.setEnamn(ename);
emp.setJob_id(job_id);
emp.setMgr(mgr);
emp.setJoindate(joindate);
emp.setSalary(salary);
emp.setBonus(bonus);
emp.getDept_id(dept_id);
list.add(emp);
// System.out.println(id + name +"--"+ job_id + "--"+ mgr + "--"+ joindate + "--"+ salary +"--"+ bonus +"--"+ dept_id);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
/*if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}*/
//简化后测试
JDBCUtils.close(rs, stmt, conn);
}
return list;
}
}