JDBC封装管理

38 篇文章 0 订阅
7 篇文章 0 订阅
  • 数据库配置文件的编写
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/jdbc?serverTimezone=Asia/Shanghai&?useUnicode=true&characterEncoding=utf8&useSSL=false
username = root
password = 1234
  • 编写公共类
package com.jdbc;

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

public class BaseDao {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    // 静态代码块,类加载的时候就初始化
    static {
        Properties properties = new Properties();
        // 通过类加载器读取对应的资源
        InputStream resources = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        System.out.println(BaseDao.class.getClassLoader());
        try {
            properties.load(resources);
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        username = properties.getProperty("username");
        password = properties.getProperty("password");
        System.out.println(driver);
        System.out.println(url);
        System.out.println(username);
        System.out.println(password);
    }

    // 获取数据库的连接
    public static Connection getConnection(){
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
    
    //编写查询公共方法
    public static ResultSet executeFind(Connection connection ,String sql ,Object[] params,ResultSet resultSet ,PreparedStatement preparedStatement) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            // setObject占位符,从1开始
            preparedStatement.setObject(i+1,params[i]);
        }
        resultSet = preparedStatement.executeQuery();
        return resultSet;
    }
    //编写增删改的公共方法
    public static int executeCRUD(Connection connection ,String sql ,Object[] params,PreparedStatement preparedStatement ) throws SQLException {
        preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            // setObject占位符,从1开始
            preparedStatement.setObject(i+1,params[i]);
        }
        int updateRows = preparedStatement.executeUpdate();
        return updateRows;
    }

    // 资源释放方法
    public static boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet ){
        boolean flag = true;
        if(resultSet != null){
            try {
                resultSet.close();
                // GC回收
                resultSet = null;
            } catch (SQLException e) {
                flag = false;
                e.printStackTrace();
            }
        }
        if(preparedStatement != null){
            try {
                preparedStatement.close();
                // GC回收
                preparedStatement = null;
            } catch (SQLException e) {
                flag = false;
                e.printStackTrace();
            }
        }
        if(connection != null){
            try {
                connection.close();
                // GC回收
                connection = null;
            } catch (SQLException e) {
                flag = false;
                e.printStackTrace();
            }
        }
        return flag;
    }
}
  • 公共类的使用
import com.pojo.User;

import java.sql.Connection;

/**
 * 定义查询接口
 */
public interface UserDao {

    // 得到要登录的用户
    public User getLoginUser(Connection connection , String userCode);
}
package com.dao.user;

import com.dao.BaseDao;
import com.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
 * 调用公用类
 */
public class UserDaoImpl implements UserDao{
    @Override
    public User getLoginUser(Connection connection, String userCode) {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        if(connection != null){
            String sql = "select * from users where user_code = ? ";
            Object[] params = { userCode };
            try {
                resultSet = BaseDao.executeFind(connection, preparedStatement, sql, params);
                if(resultSet.next()){
                    user = new User();
                    user.setId(resultSet.getInt("id"));
                    user.setName(resultSet.getString("name"));
                    user.setUserCode(resultSet.getString("user_code"));
                }
                boolean resource = BaseDao.closeResource(connection, preparedStatement, resultSet);
                System.out.println(resource);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return user;
    }

    public static void main(String[] args) {
        UserDaoImpl userDao = new UserDaoImpl();
        Connection connection = BaseDao.getConnection();
        User user = userDao.getLoginUser(connection, "ad");
        System.out.println(user);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

脑袋不够用的小渣渣

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

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

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

打赏作者

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

抵扣说明:

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

余额充值