JDBC的工具类

JDBC的工具类

使用JDBC时调用此工具类,首先更改类中相应的连接字段

  • 使用
  • 1.通过Connection获取conn连接
  • 2.通过conn创建Statement对象
  • 3.通过statement执行相应的SQL
  • 4.释放资源
  • 示例 见下方

⭐️:普通使用没问题但是:

1.遇到登陆场景使用statement会出现sql注入风险,应该使用prepareStatement对象锁定sql语句!!

2.大量访问应该使用连接池优化性能!!

//JDBC工具类
public class JDBCUtils {

    private static final String driverClass;
    private static final String url;
    private static final String user;
    private static final String password;

    static {
        driverClass="com.mysql.cj.jdbc.Driver";
        url="jdbc:mysql:///jdbctest?serverTimezone=UTC";
        user="root";
        password="123456";
    }
    /**
     * 注册驱动的方法
     * @throws ClassNotFoundException
     */
    public static void loadDriver() throws ClassNotFoundException {
        Class.forName(driverClass);
    }

    /**
     * 获得连接的方法
     * @throws Exception
     */
    public static Connection getConnection() throws Exception {
        loadDriver();
        return DriverManager.getConnection(url,user,password);
    }

    /**
     * 资源释放
     */
    public static void release(Connection connection, Statement statement){
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            connection=null;
        }
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            statement=null;
        }
    }

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


    }
//示例:
public void demo(){
        Connection conn=null;
        Statement statement=null;

        try {
            //1.获得连接
            conn=JDBCUtils.getConnection();
            //2.获得执行SQL的语句,执行SQL
            statement=conn.createStatement();
            String sql="INSERT user VALUES(null,'mmm','666','六六')";

           if(statement.executeUpdate(sql)>0){
               System.out.println("插入成功...");
           };

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //3.释放资源
            JDBCUtils.release(conn,statement);
        }
    }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值