由于注册驱动、获取连接和关闭连接的代码过于冗余,所以创建一个简单的工具类DButil
该工具类可实现注册驱动、创建连接和关闭连接的操作。由于进行了封装,所以我们只需要在使用时,调用这些静态方法即可。
到后面可以将一些增删改查的方法也直接封装到该类中,但目前只在类中实现连接的创建与关闭。
在这里要注意的是,因为需要注册的driver、数据的地址、端口、数据库名称以及用户名和密码,这些在创建连接时的必要条件是开放的,而我们对类又进行了封装,不能直接在类中修改。所以我们使用了配置文件,只需要将当前的jar包中driver的路径,数据库的url,登录数据库使用的用户名和密码写在配置文件db.properties中,并将该文件直接放在项目src目录下,运行时可直接对文件中的值进行获取。
db.properties文件的编写规范使用键值对的形式。
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/dbname
username=root
password=123456
DButil.java
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DButil {
//使用私有的静态成员变量存储要获取的值
private static String driver;
private static String url;
private static String userName;
private static String passWord;
/*
当DButil类被加载时,直接进行注册驱动。
因为创建连接等操作都需要注册驱动
而且注册驱动的操作只需要被执行一次即可
所以将注册驱动部分写在静态代码块中
*/
static{
InputStream in;
Properties properties;
try {
//通过类加载器加载资源文件
in = DButil.class.getClassLoader().getResourceAsStream("db.properties");
properties = new Properties();
properties.load(in);
//通过文件获取到值
driver = properties.getProperty("driver");
url = properties.getProperty("url");
userName = properties.getProperty("username");
passWord = properties.getProperty("password");
//注册驱动
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 用于获取连接
* @return 根据资源文件中的信息所创建的连接
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, userName, passWord);
}
/**
* 用于关闭传入的所有连接
* 连接的关闭需要从“小”到“大”
* 分别使用try/catch对连接进行关闭,为了避免出现异常后有连接无法被关闭
* @param conn 连接对象
* @param statement 执行sql的对象
* @param resultSet 返回集对象
*/
public static void closeConnection(Connection conn, Statement statement, ResultSet resultSet) {
try {
if(resultSet != null) resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(statement != null) statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 通过Jutil的方式来进行测试
* 使用DButil创建连接,并打印
* 查看使用能创建成功
* 用完后要将连接关闭
* @throws SQLException
*/
@Test
public void test() throws SQLException {
Connection conn = DButil.getConnection();
System.out.println(conn);
DButil.closeConnection(conn,null,null);
}
}
在调用测试方法进行测试时,打印出了conn连接的hascode,证明DButil工具类编写成功!
循序渐进,使用DButil获取连接,然后进行一个简单的DQL操作,最后使用DButil关闭所有连接
Demo
import com.qf.util.DButil;
import org.junit.Test;
import java.sql.*;
public class TestDQL {
@Test
public void test(){
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try {
conn = DButil.getConnection();
statement = conn.createStatement();
String sql = "select stu.s_id '学生编号',stu.s_name '学生姓名' ,stu.s_birth '生日',res.count '选课总数'," +
"res.sum '总成绩' from student stu left join (select sc.s_id,count(sc.c_id) count,sum(sc.s_score) sum " +
"from score sc group by sc.s_id) res on stu.s_id=res.s_id";
rs = statement.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
Date birth = rs.getDate(3);
String count = rs.getString(4);
String score = rs.getString(5);
System.out.println(id+","+name+","+birth+","+","+count+","+score);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DButil.closeConnection(conn, statement, rs);
}
}
}
运行结果
成功!
慢慢学着,等我学好了,我就把增删盖查那些全都封装到我的DButil类中