package com.jd.util.db;

import java.sql.*;
import java.util.Properties;

public class DBUtil {

static {
    //1、加载驱动类
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

//2.获取MySQL连接
private static Connection getConnection() throws SQLException {
    String url = PropertiesUtil.value("url");
    String userName=PropertiesUtil.value("userName");
    String password=PropertiesUtil.value("password");
    return DriverManager.getConnection(url,userName,password);
}
//关闭资源方法
private static void close(Connection connection,Statement statement){
    if(statement!=null) {
        try {
            statement.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    if(connection!=null) {
        try {
            connection.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
//关闭资源方法重载
private static void close(Connection connection,Statement statement,ResultSet resultSet){
    if(resultSet!=null) {
        try {
            resultSet.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    if(statement!=null) {
        try {
            statement.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
    if(connection!=null) {
        try {
            connection.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}

//查询方法
public static void select(String sql,IRowMapper rowMapper){

    Connection connection =null;
    Statement statement =null;
    ResultSet resultSet =null;
    try {
        //2、获取MySQL连接
        connection = getConnection();
        //3、创建语句
        statement = connection.createStatement();
        //4、执行SQL语句
        resultSet = statement.executeQuery(sql);
        //5、处理结果
        rowMapper.mapRow(resultSet);
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    } finally {
        //6、释放资源
        close(connection,statement,resultSet);
    }
}

//判断是否存在
public static boolean exist(String sql){
    Connection connection= null;
    Statement statement =null;
    ResultSet resultSet =null;
    try {
        connection = getConnection();
        statement = connection.createStatement();
        resultSet = statement.executeQuery(sql);
        return resultSet.next();

    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
    close(connection,statement,resultSet);
    return false;
}
//判断是否存在
public static boolean exist(String sql,Object...params){
    Connection connection= null;
    PreparedStatement preparedStatement=null;
    ResultSet resultSet =null;
    try {
        connection = getConnection();
        //创建sql语句
        preparedStatement = connection.prepareStatement(sql);
        //运行sql语句
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1,params[i]);
        }
        resultSet = preparedStatement.executeQuery();
        return resultSet.next();
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
    close(connection,preparedStatement,resultSet);
    return false;
}
//更新数据
public static boolean update(String sql){
        Connection connection =null;
        Statement statement =null;
        int effect=0;
    try {
        //2.连接MySQl数据库 venus
        connection = getConnection();
        //3.创建SQL语句
        statement = connection.createStatement();
        //4.执行SQL
        effect = statement.executeUpdate(sql);
        //5.处理结果
        return effect>0;
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }finally {//6.释放资源
       close(connection,statement);
    }
    return false;
}

public static boolean update(String sql,Object...params){
    Connection connection =null;
    PreparedStatement preparedStatement =null;
    int effect=0;
    try {
        //2.连接MySQl数据库 venus
        connection = getConnection();
        //3.创建SQL语句
        preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1,params[i]);
        }
        //4.执行SQL
        effect = preparedStatement.executeUpdate();
        //5.处理结果
        return effect>0;
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }finally {//6.释放资源
        close(connection,preparedStatement);
    }
    return false;
}
//查询数据
public static void select(String sql,IRowMapper rowMapper,Object...params){
    Connection connection =null;
    PreparedStatement preparedStatement=null;
    ResultSet resultSet=null;
    try {
        //2、获取连接
        connection= getConnection();
        //3、创建语句
        preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1,params[i]);
        }
        //4、执行SQL
        resultSet= preparedStatement.executeQuery();
        //5、处理结果
        rowMapper.mapRow(resultSet);
        //6、释放资源

    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }finally {
        close(connection,preparedStatement,resultSet);
    }
}

//更改多条数据
public static boolean batch(String...sqls){
    //加载驱动

    Connection connection =null;
    Statement statement=null;
    try {
        //连接mysql
        connection=getConnection();
        connection.setAutoCommit(false);
        //创建sql语句
        statement = connection.createStatement();
        for (String sql:sqls){
            statement.addBatch(sql);
        }
        //执行sql
        statement.executeBatch();
        connection.commit();
        return true;
    } catch (SQLException throwables) {
        if (connection!=null) {
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        throwables.printStackTrace();
    }finally {
        //资源释放
      close(connection,statement);
    }

    return false;
}

}

Posted on 2021-05-27 16:22  lezai0514  阅读(82)  评论(0)    收藏  举报