【JDBC】【MySQL】一文带你搞懂JDBC如何连接MySQL&用JDBC实现MySQL的模糊查询

总结:​​本文通过对JDBC工具类的封装和使用工具类实现模糊查询,带你入门JDBC,其中使用的知识点在上篇博客里有!

场景:

在学习JDBC的语言中,每次都执行通用的几步:即注册驱动,获取连接,创建操作,处理结果,释放资源 过于复杂,因此不妨将上述步骤封装成工具类,只对外提供方法!



描述:

这是不使用工具类的封装写出来的代码,比较冗余复杂

package com.zdx.JDBC;

import java.sql.*;

public class JAVA1129_5 {
    public static void main(String[] args) {
        //设置空对象,注册驱动,获取连接,创建操作,处理结果集,释放资源
        String url = "jdbc:mysql://127.0.0.1:3306/hello";
        String username = "root";
        String password = "rota";

        String SQL = "insert into stu values(1,'zdx','nbnc'),(2,'cyc','qwq');";
//        String SQL1 = "update stu set sname ='xzq',major='bask' where sno = '1';";
        String SQL1="select * from stu";
        Connection connection = null;
        Statement statement = null;
        ResultSet resultset = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(url, username, password);
            statement = connection.createStatement();
            int cnt = statement.executeUpdate(SQL);
            if (cnt != 0) {
                System.out.println("执行成功");
            }
            ResultSet result = statement.executeQuery(SQL1);
            while (result.next()) {
                //随着光标移动对操作对象进行操作
                System.out.println("nbnb");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        //释放资源,必须在最后加上finally语句块执行
        finally {
                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();
                    }
                }
            }
        }
    }





解决方案:

首先类内的构造方法加私有修饰符,模仿Sun公司工具类,如Arrays类 和 Collection 。

其次注册驱动,利用静态代码块内只注册一次进行注册驱动

然后获取数据库连接,返回数据库连接对象的方法内有异常,不能catch,需要向外扔。

最后封装一个关闭的方法。

注意由于封装工具类,且对外只提供方法因此都封装成类方法(即static修饰)

package com.zdx.JDBC;

import java.sql.*;

//2021.11.2920点03分 对数据库的工具类进行封装
public class  DBUtil{
    private DBUtil(){}
    static{
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }//利用静态代码块在类加载时只加载一次的特性注册驱动。

    //获取连接的方法
    public static Connection getConnection (String url,String user,String password)throws SQLException{
       return   DriverManager.getConnection(url,user,password);//这里注意驱动管理类内调用的获取连接方法返回对象就是connection对象。
    }

    //关闭资源
    //按照顺序,结果集,数据库操作对象,连接对象!
    public static void close(Connection connection,Statement ps,ResultSet resultSet){

        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if(ps!=null){
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

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

}

对工具类进行调用实现模糊查询:

package com.zdx.JDBC;

import java.sql.*;

public class Main {

    public static void main(String[] args) {

        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;
        String url = "jdbc:mysql://127.0.0.1:3306/hello";
        String user = "root";
        String password = "rota";
        //获取连接
        try {
            connection = DBUtil.getConnection(url, user, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //获取预编译的数据库操作对象
        String SQL = "select sname from stu where sname like ?";
        try {
            ps = connection.prepareStatement(SQL);
            ps.setString(1, "_y%");
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                System.out.println(resultSet.getString("sname"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


        //释放资源
        finally {
            DBUtil.close(connection, ps, resultSet);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

傻根根呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值