对于jdbc的许多操作例如数据库连接和关闭,重复度太多,为了简化代码,可简单的写 JDBC的工具类,
对于数据库连接操作,为提高使用的拓展性,可通过使用配置文件来优化
简单的配置文件如下
url=jdbc:mysql://localhost:3306/ss ?serverTimezone=UTC
user=root
password=123456
Diver=com.mysql.cj.jdbc.Driver
- jdbc工具类
package hj.jdbc;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
/**
* jdbc工具类
*/
public class day2 {
private static String url;
private static String user;
private static String password;
private static String diver;
//写在静态代码块上获取数据库连接并配置文件
//由于读取该配置文件只需要读取一次,所以用静态代码块更为简化
static {
Properties d=new Properties();
try {
/*
动态获取配置文件路径
*/
Class<day2> day2Class = day2.class;
ClassLoader classLoader = day2Class.getClassLoader();
URL resource = classLoader. getResource("jdbc.properties");//URL为统一资源标识符(定位符),可以定位文件的绝对路径
String path = resource.getPath();//获取字符串路径
// System.out.println(path);
d.load(new FileReader(path));
url= d.getProperty("url");
user = d.getProperty("user");
password = d.getProperty("password");
diver = d.getProperty("Diver");
Class.forName(diver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getconnection() throws Exception{
//写在方法上获取数据库连接并配置文件
// Properties d=new Properties();
// Class<day2> day2Class = day2.class;
// ClassLoader classLoader = day2Class.getClassLoader();
// InputStream resourceAsStream = classLoader.getResourceAsStream("jdbc.properties");
//
// d.load(resourceAsStream);
//
// String url = d.getProperty("url");
// String user = d.getProperty("user");
// String password = d.getProperty("password");
// String diver = d.getProperty("Diver");
// Class.forName(diver);
Connection connection = DriverManager.getConnection(url, user, password);
return connection;
}
下面为不同条件下的关闭数据库方法
public static void close(Statement stmt, Connection con, ResultSet s){
if (stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} if (con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
} if (s!=null){
try {
s.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
public static void close2(PreparedStatement stmt, Connection con, ResultSet s){
if (stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} if (con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
} if (s!=null){
try {
s.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
public static void close1(Statement stmt, Connection con){
if (stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} if (con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void close3(PreparedStatement stmt, Connection con){
if (stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} if (con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
Connection getconnection=null;
Statement getstatement=null;
ResultSet resultSet=null;
try {
getconnection = day2.getconnection();
getstatement = getconnection.createStatement();
String sql="select * from s";
resultSet = getstatement.executeQuery(sql);
while(resultSet.next()){
String name = resultSet.getString("name");
System.out.println(name);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
day2.close(getstatement,getconnection,resultSet);
}
}
}