JDBC工具类
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
private static String driver;
private static String url;
private static String user;
private static String passworld;
static {
try {
ClassLoader classLoader = JDBCUtils.class.getClassLoader();
URL resource = classLoader.getResource("JDBCUtils.properties");
String path = resource.getPath();
Properties pro = new Properties();
FileInputStream fis = new FileInputStream(path);
pro.load(fis);
driver = pro.getProperty("Driver");
url = pro.getProperty("Url");
user = pro.getProperty("User");
passworld = pro.getProperty("PassWord");
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, passworld);
}
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配置文件
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190107214612219.png)