- 目的:简化书写,减少重复代码
- 注册驱动也抽取
- 抽取一个方法获取连接对象
- 抽取一个方法释放资源
创建一个
util包
,盛放JDBC工具类JDBCutil
抽取一个方法释放资源:
/**
* 释放资源
* */
//释放2个资源,Statement,Connection
public static void close2(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();
}
}
}
//方法重载,释放3个资源,Statement,Connection,ResultSet
public static void close(Statement stmt, Connection conn, ResultSet res) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (res!=null){
try {
res.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
抽取一个方法获取连接对象:使用配置文件
src文件夹下创建配置文件jdbc.properties
使用静态代码块,利用类加载器,读取配置文件,获取值,并且注册工具类
private static String url;
private static String user;
private static String password;
private static String driver;
/**
*使用静态代码块,读取配置文件,获取值
* */
static {
//创建properties对象,用于盛放外部引入的类名和类的成员方法
Properties pro = new Properties();
//引入配置文件
//1.创建类加载器
ClassLoader cl = JDBCutil.class.getClassLoader();
//2.根据类加载器,获取配置文件,并转化为IO字节流
InputStream in = cl.getResourceAsStream("jdbc.properties");
//将读取的IO字节流数据放入Properties集合中
try {
pro.load(in);
} catch (IOException e) {
e.printStackTrace();
}
//根据键值对,获取类名和成员方法名
url=pro.getProperty("url");
user=pro.getProperty("user");
password=pro.getProperty("password");
driver=pro.getProperty("driver");
//注册工具类
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
连接对象:
/**
* 抽取一个方法获取连接对象
*/
public static Connection connection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
总代码
package util;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JDBCutil {
private static String url;
private static String user;
private static String password;
private static String driver;
/**
*使用静态代码块,读取配置文件,获取值
* */
static {
//创建properties对象,用于盛放外部引入的类名和类的成员方法
Properties pro = new Properties();
//引入配置文件
//1.创建类加载器
ClassLoader cl = JDBCutil.class.getClassLoader();
//2.根据类加载器,获取配置文件,并转化为IO字节流
InputStream in = cl.getResourceAsStream("jdbc.properties");
//将读取的IO字节流数据放入Properties集合中
try {
pro.load(in);
} catch (IOException e) {
e.printStackTrace();
}
//根据键值对,获取类名和成员方法名
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driver = pro.getProperty("driver");
//注册工具类
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 抽取一个方法获取连接对象
*/
public static Connection connection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
/**
* 释放资源
*/
//释放2个资源,Statement,Connection
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();
}
}
}
//方法重载,释放3个资源,Statement,Connection,ResultSet
public static void close(Statement stmt, Connection conn, ResultSet res) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (res != null) {
try {
res.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
使用
package JDBC;
import util.JDBCutil;
import java.sql.*;
public class DemoSetReuslt02 {
public static void main(String[] args) {
Statement stamt = null;
Connection conn = null;
ResultSet res = null;
try {
//1.导入驱动jar包
//2.注册驱动
//3.获取数据库连接对象(连接到数据库)
conn = JDBCutil.connection();
//4.定义sql语句
String sql = "select * from account";
//5.获取执行sql的对象
stamt = conn.createStatement();
//6.执行sql
res = stamt.executeQuery(sql);
//7.处理结果
while (res.next()) {
String name = res.getString(2);
int n = res.getInt("balance");
System.out.println("姓名:" + name + " 工资:" + n);
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally { //8.释放资源
JDBCutil.close(stamt,conn,res);
}
}
}