前言
这里直接把jdbc中的连接和释放资源写进util包,方便后续使用。
代码展示及讲解
工具类JDBCUtils 整体展示
public class JDBCUtils {
private static String url;
private static String user;
private static String password;
private static String driver;
/**
*
* @Description: 使用静态代码块读取 配置文件中的内容
* 这样只要读取一次就可以了
*
* @auther: FARO_Z
* @date: 2:16 上午 2020/7/19
* @param:
* @return:
*
*/
static {
//1.创建Properties集合类
Properties pro=new Properties();
//2.加载文件
try {
/*
pro.load(new FileReader(path));中的path换成直接写src/jdbc.properties 可能会出现读不出来的情况
所以要通过下面的方式动态获取配置文件的路径
注意:使用下面这种方式,路径中一定不能有中文出现
路径必须全部都是英文
*/
// ClassLoader classloader=JDBCUtils.class.getClassLoader();
// URL resource =classloader.getResource("jdbc.properties");
// String path=resource.getPath();
// System.out.println(path);
// pro.load(new FileReader(path));
pro.load(new FileReader("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();
}
}
/**
*
* @Description: 获取链接
*
* @auther: FARO_Z
* @date: 2:04 上午 2020/7/19
* @param: []
* @return: java.sql.Connection
*
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
/**
*
* @Description: 释放资源,这是第一个重载
*
* @auther: FARO_Z
* @date: 2:06 上午 2020/7/19
* @param: [stmt, conn]
* @return: void
*
*/
public static void close(Statement stmt,Connection conn) {
if (stmt!=null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn!=null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
/**
*
* @Description: 释放资源,第二个重载
*
* @auther: FARO_Z
* @date: 2:07 上午 2020/7/19
* @param: [rs, stmt, conn]
* @return: void
*
*/
public static void close(ResultSet rs,Statement stmt, Connection conn) {
if (stmt!=null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn!=null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (rs!=null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
方法详解
静态代码块
这里通过配置文件存储url,uesr,password
配置文件放在src目录下
并通过Properties集合来获取配置文件中对应键值
static {
//1.创建Properties集合类
Properties pro=new Properties();
//2.加载文件
try {
/*
pro.load(new FileReader(path));中的path换成直接写src/jdbc.properties 可能会出现读不出来的情况
所以要通过下面的方式动态获取配置文件的路径
注意:使用下面这种方式,路径中一定不能有中文出现
路径必须全部都是英文
*/
// ClassLoader classloader=JDBCUtils.class.getClassLoader();
// URL resource =classloader.getResource("jdbc.properties");
// String path=resource.getPath();
// System.out.println(path);
// pro.load(new FileReader(path));
//获取配置文件中的值
pro.load(new FileReader("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();
}
}
getConnection
这里在获得配置文件中的url,user,password后,直接调用JDBC中的DriverManager获取连接并返回
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
close()
close()方法有两个,一个不带ResultSet参,一个带
这是因为当执行查询操作的时候
查询到结果或存放在结果集ResultSet里
使用完ResultSet后,要释放其资源
public static void close(ResultSet rs,Statement stmt, Connection conn) {
if (stmt!=null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn!=null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (rs!=null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
使用测试
这里我们将下表中的数据存入ArrayList并输出
public class JDBCDemo8 {
public static void main(String[] args) {
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
String sql="select * from account";
ArrayList<Account> list=new ArrayList<Account>();
try {
conn=JDBCUtils.getConnection();//获取数据库连接
stmt=conn.createStatement();//获取执行对象
rs=stmt.executeQuery(sql);//执行查找
while (rs.next()) {
//三个参数分别是: id name balance
list.add(new Account(rs.getInt(1),rs.getString(2),rs.getInt(3)));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtils.close(rs,stmt,conn);//释放资源
}
ArrayListUtil.outputArrayList(list);//打印结果
}
}
最后结果如下图