JDBC连接MySQL并实现模糊查询

作者:九九舅舅酒酒 时间:2021-07-06 01:42:28 

场景:

在学习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);
       }
   }
}

来源:https://blog.csdn.net/m0_56164356/article/details/121618599

标签:JDBC,连接,MySQL,模糊,查询
0
投稿

猜你喜欢

  • java8新特性-lambda表达式入门学习心得

    2021-09-26 17:14:33
  • Java内存缓存工具Guava LoadingCache使用解析

    2023-05-25 08:36:36
  • 开源一个c# 新的雪花算法

    2022-01-20 16:08:56
  • 仅用5分钟极速入门Dubbo使用教程

    2022-08-08 12:08:55
  • Java中的Object.getClass()方法解析

    2022-05-06 18:54:48
  • SpringBoot全局Controller返回值格式统一

    2022-09-12 14:07:52
  • Spring Boot产生环形注入的解决方案

    2023-11-08 20:14:04
  • 在C#使用字典存储事件示例及实现自定义事件访问器

    2022-08-14 14:34:52
  • Eclipse中maven的配置详解

    2023-01-04 08:51:09
  • Android实现完整游戏循环的方法

    2023-12-26 16:40:06
  • ReentrantLock源码详解--公平锁、非公平锁

    2023-04-22 17:18:56
  • Android开发仿扫一扫实现拍摄框内的照片功能

    2023-03-17 14:11:12
  • IDEA SpringBoot项目配置热更新的步骤详解(无需每次手动重启服务器)

    2023-11-12 00:22:41
  • Java多线程状态及方法实例解析

    2021-09-10 22:49:53
  • 详解SpringBoot中的tomcat优化和修改

    2022-12-31 04:00:44
  • JDK8时间相关类超详细总结(含多个实例)

    2022-03-25 05:30:33
  • c# 使用OpenCV识别硬币

    2021-12-18 15:09:13
  • C#导出文本内容到word文档的方法

    2021-10-15 19:00:37
  • Android APK文件在电脑(PC虚拟机)上面运行方法

    2023-10-05 08:44:06
  • 自定义Android注解系列教程之注解变量

    2022-10-17 05:42:10
  • asp之家 软件编程 m.aspxhome.com