【JDBC】编程(2)--- 写一个JDBC工具类;用 JDBC工具类 实现模糊查询

/*

JDBC工具类

功能:

  简化JDBC部分代码书写

*/

 

import java.sql.*;

public class DBUtil {

    /**
     * 工具类的构造方法都应该是私有的
     * 因为工具类是需要频繁使用的,所以我们要避免代码的重复书写
     * 将工具类方法都设为静态的,再将构造方法私有化(这样想new都new不出来),直接采用类名调用
     */

    //静态代码块在类加载时执行,并且只执行一次
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private DBUtil(){
    }

    /**
     * 获取数据库连接对象
     * @return 连接
     * @throws SQLException
     */
  
    //因为此方法是被调用的方法,所以出现异常直接上抛就行
    public static Connection getConnection () throws SQLException {
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode"
                , "root", "888");
    }

    /**
     * 关闭资源
     * @param connection 连接资源
     * @param statement 数据库操作对象
     * @param resultSet 结果集
     */
  
    public static void close(Connection connection,Statement statement,ResultSet resultSet){
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

 


/*

1、测试JDBC工具类的功能:

   实现模糊查询(以“查哪个用户的密码中的第二个字符为‘a’为例)

2、t_user:
    +----+-----------+----------+----------+
    | id | loginName | loginPwd | realName |
    +----+-----------+----------+----------+
    |  1 | abc       | 123      | 张三     |
    |  2 | 000000    | 000000   | 李四     |
    |  7 | asd333    | aa000    | NULL     |
    |  8 | q7890     | 8a8a88   | NULL     |
    |  9 | 666a      | 6a       | NULL     |
    +----+-----------+----------+----------+

*/

 

import java.sql.*;

public class JDBCTest03 {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet resultSet = null;

        try {
            //注册+获取连接
            connection = DBUtil.getConnection();

            //获取预编译的数据库操作对象
            String sql = "select loginPwd from t_user where loginPwd like ?";
            ps = connection.prepareStatement(sql);
            ps.setString(1,"_a%");

            //执行sql语句
            resultSet = ps.executeQuery();

            //处理查询结果集
            while (resultSet.next()){
                System.out.println(resultSet.getString("loginPwd"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //关闭资源
            DBUtil.close(connection,ps,resultSet);
        }
    }
}

 

控制台输出结果:

aa000
8a8a88
6a

Process finished with exit code 0

 

总结:控制台输出结果正常,JDBC工具类 功能正常。


 

posted @ 2022-01-03 12:33  猿头猿脑的王狗蛋  阅读(116)  评论(0编辑  收藏  举报
1