jdbc工具类

properties配置文件

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms?useSSL=false&useUnicode=true&characterEncoding=utf-8
username=root
password=1234

JDBCUtils类

package com.bdqn.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

import static java.lang.Class.forName;

//操作数据库基类(公共类)
public class JDBCUtils {

    private final static String driver;
    private final static String url;
    private final static String username;
    private final static String password;

    //静态代码块,类加载的时候就初始化了
    static{
        /* Properties类表示一组持久的属性。 Properties可以保存到流中或从流中加载。 属性列表中的每个键及其对应的值都是一个字符串。*/
        Properties properties = new Properties();
        /*ClassLoader getClassLoader():返回类的类加载器。
        InputStream getResourceAsStream(String name):返回用于读取指定资源的输入流。*/
        InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("db.properties");

        try {
            /* void load(InputStream inStream) 从输入字节流中读取属性列表(键和元素对)。*/
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }

        /* String getProperty(String key) 在此属性列表中搜索具有指定键的属性。*/
        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");
    }

    //获取数据库连接
    public static Connection getConnection(){
        Connection connection = null;
        /* class类实例表示正在运行的Java应用程序中的类和接口。*/
        try {
            /*static 类<?> forName(String className) 返回与具有给定字符串名称的类或接口关联的 类对象。
            static Connection getConnection(String url, String user, String password) 尝试建立与给定数据库URL的连接。*/
            forName(driver);
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return connection;
    }

    /** 编写查询公共方法
     *     因为我们不知道sql语句的参数有多少没所以这里使用了Object[] params数组
     * @param connection
     * @param sql
     * @param params
     * @param resultSet
     * @return
     * @throws SQLException
     */
    public static ResultSet execute(Connection connection, PreparedStatement preparedStatement,
                                    ResultSet resultSet, String sql, Object[] params) throws SQLException {
        //预编译sql
        preparedStatement = connection.prepareStatement(sql);

        for (int i = 0; i < params.length; i++) {
            /* 由于数组是从0开始,而setObject,占位符要从1开始,所以(i+1)
            void setObject(int parameterIndex, Object x) 使用给定对象设置指定参数的值。*/
            preparedStatement.setObject(i+1,params[i]);//赋值
        }
        //获得查询的结果基
        resultSet = preparedStatement.executeQuery();

        return resultSet;
    }

    /** 编写增删查通用方法
     *
     * @param connection
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    public static int execute(Connection connection, PreparedStatement preparedStatement, String sql, Object[] params) throws SQLException {
        //预编译sql
        preparedStatement = connection.prepareStatement(sql);

        for (int i = 0; i < params.length; i++) {
           /* 由于数组是从0开始,而setObject,占位符要从1开始,所以(i+1)
            void setObject(int parameterIndex, Object x) 使用给定对象设置指定参数的值。*/
            preparedStatement.setObject(i+1,params[i]);//赋值
        }

        //返回受影响行数
        return preparedStatement.executeUpdate();
    }

    /** 释放资源
     *
     * @param connection
     * @param preparedStatement
     * @param rs
     * @return
     */
    public static boolean closeResource(Connection connection, PreparedStatement preparedStatement, ResultSet rs){
        boolean flag = true;
        if (rs != null){
            try {
                rs.close();
                //GC回收
                rs = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }

        if (preparedStatement != null){
            try {
                preparedStatement.close();
                //GC回收
                preparedStatement = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }

        if (connection != null){
            try {
                connection.close();
                //GC回收
                connection = null;
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                flag = false;
            }
        }
        return flag;
    }

    /** 释放资源
     *
     * @param connection
     * @param preparedStatement
     * @return
     */
    public static boolean closeResource(Connection connection, PreparedStatement preparedStatement){
        return closeResource(connection,preparedStatement,null);
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值