DBUtil工具类

package com.lanou.util.db;

import com.lanou.util.PropertiesUtil;

import java.sql.*;

/**
 * @ClassName DBUtil
 * @Description TODO
 * @Author man
 * @Date 2021/5/25 15:53
 * @Version 1.0
 */
public class DBUtil {
    static {
        try {
            //1.创建
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        //登陆测试
        String userName="admin";
        //String password="1' or 'a'='a";
        String password="123456";
        String sql="select id from user where name=? and password=? ";
        IRowMapper iRowMapper= resultSet -> {
            try {
                if (resultSet.next()){
                    System.out.println("登陆成功");
                }else {
                    System.out.println("登陆失败");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        };
        select(sql,iRowMapper,userName,password);
    }
    //获取数据库连接
    private static Connection getConnection() throws SQLException {
//        String url="jdbc:mysql://127.0.0.1:3306/venus?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
        String userName= PropertiesUtil.value("userName");
        String password=PropertiesUtil.value("password");
        String url=PropertiesUtil.value("url");
        return DriverManager.getConnection(url,userName,password);
    }
    /*判断用户名是否存在*/
    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 e) {
            e.printStackTrace();
        }finally {
            close(resultSet,statement,connection);
        }
        return false;
    }
    public static boolean exist(String sql,Object... canshu){
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet =null;
        try {
            connection=getConnection();
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < canshu.length; i++) {
                preparedStatement.setObject(i+1,canshu[i]);
            }
            resultSet = preparedStatement.executeQuery();
            return resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(resultSet,preparedStatement,connection);
        }
        return false;
    }
    //查询语句
    public static void select(String sql,IRowMapper rowMapper){
        Connection connection =null;
        Statement statement =null;
        ResultSet resultSet=null;
        String url="jdbc:mysql://127.0.0.1:3306/venus?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
        try {
            //2.获取mysql连接
            connection = getConnection();
            //3.创建语句
            statement = connection.createStatement();
            //4.执行语句
            resultSet = statement.executeQuery(sql);
//            return resultSet
            rowMapper.mapRow(resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //6.释放资源
            close(resultSet,statement,connection);
        }
    }
    public static void select(String sql, IRowMapper iRowMapper, Object... canshu){
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String url="jdbc:mysql://127.0.0.1:3306/venus?useSSL=false&serverTimezone=UTC";
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet =null;
        try {
            connection =getConnection();
//            Statement statement = connection.createStatement();
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < canshu.length; i++) {
                preparedStatement.setObject(i+1,canshu[i]);
            }
            resultSet = preparedStatement.executeQuery();
            iRowMapper.mapRow(resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            close(resultSet,preparedStatement,connection);
        }
    }
    //更新语句
    public static boolean update(String sql){
        Connection connection =null;
        Statement statement =null;
        //String url="jdbc:mysql://127.0.0.1:3306/venus?useSSL=false&serverTimezone=UTC";
        try {
            connection = getConnection();
            //3.创建sql
            statement = connection.createStatement();
            int effect = statement.executeUpdate(sql);
            return effect>0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //6.释放资源
            close(statement,connection);
        }
        return false;
    }
    public static boolean update(String sql,Object... canshu){
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        //String url="jdbc:mysql://127.0.0.1:3306/venus?useSSL=false&serverTimezone=UTC";
        try {
            connection = getConnection();
            //3.创建sql
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < canshu.length; i++) {
                preparedStatement.setObject(1+i,canshu[i]);
            }
            int effect = preparedStatement.executeUpdate();
            return effect>0;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //6.释放资源
            close(preparedStatement,connection);
        }
        return false;
    }

    //关闭资源
    private static void close(Statement statement,Connection connection){
        if (statement!=null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection!=null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //关闭资源
    private static void close(ResultSet resultSet,Statement statement,Connection connection){
        if (resultSet!=null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        close(statement, connection);
    }
}

IRowMapper 接口

package com.lanou.util.db;

import java.sql.ResultSet;
import java.sql.SQLException;

public interface IRowMapper {
    void mapRow(ResultSet resultSet) throws SQLException;
}

PropertiseUtil

package com.lanou.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @ClassName PropertiesUtil
 * @Description TODO
 * @Author man
 * @Date 2021/5/27 15:39
 * @Version 1.0
 */

public class PropertiesUtil {
    private static Properties properties=new Properties();
    static {
        InputStream inputStream=PropertiesUtil.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String value (String key) {
        return properties.getProperty(key);
    }
}

db.propertise

url=jdbc:mysql://localhost:3306/venus?serverTimezone=UTC
userName=root
password=Weak

StringUtil工具类MD5加密 获取UUID

package com.lanou.util;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.UUID;

/**
 * @ClassName StringUtil
 * @Description TODO
 * @Author man
 * @Date 2021/5/27 10:05
 * @Version 1.0
 */

public class StringUtil {
    public static String getId(){
        return UUID.randomUUID().toString();
    }
    public static String getMD5(String md5){
        return DigestUtils.md5Hex(md5);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值