当前位置: 首页 > 工具软件 > JdbcUtils > 使用案例 >

JDBCUtils和druidJdbcutils

令狐功
2023-12-01
public class JDBCUtilsByDruid {
    private static DataSource dataSource;


    static {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("src\\druid.properties"));
            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection(){
        try {
            return dataSource.getConnection();
        } catch (SQLException throwables) {
            throw new RuntimeException(throwables);
        }
    }

    public static void close(ResultSet resultSet, Statement statement, Connection connection){
        if (resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throw new RuntimeException(throwables);
            }
        }
        if (statement != null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throw new RuntimeException(throwables);
            }

        }
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throw new RuntimeException(throwables);
            }
        }
    }
}
public class JDBCUtils {
    private static String user;
    private static String password;
    private static String url;
    private static String driver;

    static {
        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream("src\\com\\sky\\chapter25\\jdbc.properties"));
            user = (String) properties.get("user");
            password = (String) properties.get("password");
            url = (String) properties.get("url");
            driver = (String) properties.get("driver");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public static Connection getConnection(){

        try {
            return DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void close(ResultSet resultSet, Statement statement,Connection connection){

        if (resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throw new RuntimeException(throwables);
            }
        }
        if (statement != null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throw new RuntimeException(throwables);
            }

        }
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throw new RuntimeException(throwables);
            }
        }

    }
}
 类似资料: