package com.mysql.utils;
import java.sql.*;
import java.util.ResourceBundle;
public class DBUtil {
private static String driverClass ;
private static String jdbcUrl;
private static String user;
private static String password ;
static{
ResourceBundle resource = ResourceBundle.getBundle("dbconfig");
driverClass = resource.getString("driverClass");
jdbcUrl= resource.getString("jdbcUrl");
user = resource.getString("user");
password = resource.getString("password");
}
public static Connection getConnection() throws Exception {
Class.forName(driverClass) ;
Connection conn = (Connection) DriverManager.getConnection(jdbcUrl, user, password);
return conn;
}
public static PreparedStatement getPreparedStatement(String sql) throws Exception {
PreparedStatement preparedStatement = getConnection().prepareStatement(sql);
return preparedStatement;
}
public static ResultSet getResultSet(PreparedStatement preparedStatement) throws SQLException {
return preparedStatement.executeQuery();
}
public static void closeResource(ResultSet rs, PreparedStatement ps ) throws SQLException {
if (rs!=null){
rs.close();
}
if (ps !=null) {
ps.close();
}
}
public static void closeConnection() throws Exception {
getConnection().close();
}
}
注:其他数据库只需要将配置文件修改即可