JDBC工具类的封装

概述

JDBC工具类,简化JDBC编程

代码示例

利用工具类实现模糊查询。

import java.sql.*;

public class Demo {

    /**
     * 工具类中的构造方法都是私有的。
     * 因为工具类当中的方法都是静态的,不需要new对象,直接采用类名调用。
     */
    private Demo() {}
    /*静志代码块在类加载时执行,
    并且只执行一次。*/
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取数据库连接对象
     * @return 连接对象
     * @throws SQLException
     */
    public static Connection getConnection () throws SQLException {
            return DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/powernode?serverTimezone=UTC",
                    "root",
                    "密码");
    }

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

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

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DemoTest {
    public static void main(String[] args) {
        Connection conn =null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            //获取连接
            conn = Demo.getConnection();
            //获取预编译的数据库操作对象
            String sql = "select ename from emp where ename like ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,"_A%");
            rs = pstmt.executeQuery();
            while (rs.next()){
                System.out.println(rs.getString("ename"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //释放资源
            Demo.close(conn,pstmt,rs);
        }
    }
}

在这里插入图片描述

封装二
package com.zyh.util;

import java.sql.*;

public class JdbcUtil {

    final String URL = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC";
    final String USERNAME = "root";
    final String PASSWORD = "密码";
    Connection conn =null;
    PreparedStatement ps = null;

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public Connection getConnection (){
        try {
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }


    public PreparedStatement createStatement(String sql){
        try {
            ps = getConnection().prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ps;
    }

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

        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public void close(ResultSet rs){
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

posted @ 2020-08-04 11:41  YU_UY  阅读(337)  评论(0编辑  收藏  举报