DBUtil

package com.lanou.util;

import java.sql.*;

/**
 * 数据库方法
 */
public class DBUtil {
    //静态代码块 加载一次就可以
    static {
        //1.加载驱动类
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    /**
     * 链接数据库mysql
     * @return
     * @throws SQLException
     */
    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);
    }
    //执行查询语句
    public static void  Query(String sql,IRomMapper romMapper){
        Statement statement = null;
        Connection connection = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            romMapper.mapRow(resultSet);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //释放资源
            close(resultSet,statement,connection);
        }
    }
    //执行修改表语句1
    public static boolean Update(String sql){
        Connection connection = null;
        Statement statement = null;
        try {
            connection = getConnection();
            statement = connection.createStatement();
            int effect = statement.executeUpdate(sql);
            return effect>0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(statement,connection);
        }
        return false;
    }
    //执行修改表语句2
    public static boolean Update(String sql,Object...ccc){
        Connection connection = null;
        PreparedStatement preparedStatement =null;
        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i <ccc.length; i++) {
                preparedStatement.setObject(i+1,ccc[i]);
            }
            int effect = preparedStatement.executeUpdate();
            return effect>0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(preparedStatement,connection);
        }
        return false;
    }
    //判断是否存在01
    public static boolean exist(String sql){
        Statement statement = null;
        Connection connection = null;
        ResultSet resultSet = null;
        try {
            connection = getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
            return resultSet.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(resultSet,statement,connection);
        }
        return false;
    }
    //判断是否存在02
    public static boolean exist(String sql,Object...bbb){
        Connection connection = null;
        ResultSet resultSet = null;
        PreparedStatement preparedStatement=null;
        try {
            connection = getConnection();
            preparedStatement=connection.prepareStatement(sql);
            for (int i = 0; i <bbb.length; i++) {
                preparedStatement.setObject((i+1),bbb[i]);
            }
            resultSet = preparedStatement.executeQuery();
            return resultSet.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(resultSet,preparedStatement,connection);
        }
        return false;
    }

    public static void select(String sql,IRomMapper 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(resultSet,statement,connection);
    }
      }

    public  static void select(String sql,IRomMapper romMapper,Object...aaa){
        ResultSet resultSet=null;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            //获取链接
             connection= getConnection();
            //执行语句,预编译
             preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i <aaa.length; i++) {
                preparedStatement.setObject((i+1),aaa[i]);
            }
             resultSet = preparedStatement.executeQuery();
        romMapper.mapRow(resultSet); } catch (SQLException throwables) { throwables.printStackTrace(); }finally { //释放资源 close(resultSet,preparedStatement,connection); } } //关闭资源的方法 private static void close(ResultSet resultSet,Statement statement,Connection connection){ if(resultSet!=null){ try { resultSet.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } //调用 close(statement,connection); } //关闭资源的方法重写 public static void close(Statement statement,Connection connection){ if(statement!=null){ try { statement.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if(connection!=null){ try { connection.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }

  

posted on 2021-05-27 16:17  Fire·  阅读(69)  评论(0编辑  收藏  举报

导航