【无标题】Java封装BaseDao工具类的详细步骤

BaseDao 自己要封装的一个类

这个类对数据库的数据进行增删改查的

在这个类的方法中只需要写两个方法 一个是增删改 一个查

1.先复制JdbcUtil这个工具类到咱们的utils文件夹中
2.复制db.properties到src文件夹下面
3.在src下面新建lib文件夹 将mysqljar包导入当前工程中

JdbcUtil工具类

package utils;
import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;

/**
 * 1.获取Connection对象
 * 2.关闭资源
 */
public class JdbcUtil {
    private static String url = null;
    private static String user =null;
    private static String password = null;
    static {
        try {
        //只要JdbcUtil类加载了,就会执行静态代码块中的代码
        //读取配置文件中的信息:properties文件
        //Properties类表示一组持久的属性。 Properties可以保存到流中或从流中加载。
        // 属性列表中的每个键及其对应的值都是一个字符串。
            Properties properties = new Properties();
            properties.load(new FileInputStream("src/db.properties"));
            //数据都在properties 对象中
            String driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            user = properties.getProperty("user");
            password = properties.getProperty("password");
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection () {

        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  connection;
    }

    //关闭资源
    //增删改  需要关闭两个   查 关闭三个
    public static void close (Connection connection) {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close (Statement statement, Connection connection) {
        try {
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    public static void close (ResultSet resultSet, Statement statement, Connection connection) {
        try {
            resultSet.close();
            statement.close();
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}

db.properties配置文件

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/java2304?useSSL=false
user=root
password=123456

下载mysql jar包

1.百度 https://mvnrepository.com/
2.在搜索框中输入mysql
3.找到版本  5.1.47  版本jar包点进去

BaseDao工具类

package utils;

import org.apache.commons.beanutils.BeanUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BaseDao {
    public int update(String sql, Object[] parameters) {
        Connection connection = JdbcUtil.getConnection();
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            ParameterMetaData parameterMetaData = preparedStatement.getParameterMetaData();
            int parameterCount = parameterMetaData.getParameterCount();
            if (parameters != null && parameters.length == parameterCount) {
                for (int i = 1; i <= parameterCount; i++) {
                    preparedStatement.setObject(i, parameters[i - 1]);
                }
                int i = preparedStatement.executeUpdate();
                return i;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(preparedStatement, connection);
        }
        return 0;
    }

    public <T> List<T> query(String sql, Object[] parameters, Class<T> cls) {
        Connection connection = JdbcUtil.getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            int parameterCount = preparedStatement.getParameterMetaData().getParameterCount();
            if (parameters != null && parameters.length == parameterCount) {
                for (int i = 1; i <= parameterCount; i++) {
                    preparedStatement.setObject(i, parameters[i - 1]);
                }
            }
           resultSet = preparedStatement.executeQuery();
            List<T> list = new ArrayList<>();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();

            while (resultSet.next()){
                T t = cls.getConstructor(null).newInstance(null);
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnName(i);
                    //System.out.println(columnName);
                    Object object = resultSet.getObject(columnName);
                    System.out.println(object);
                    BeanUtils.setProperty(t,columnName,object);
                }
                list.add(t);
            }
            return list.size() !=0 ? list : null;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JdbcUtil.close(resultSet,preparedStatement,connection);
        }
        return  null;

    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值