jdbc - BaseDao

package BaseDao;

import util.JdbcUtil;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

public class BaseDao {

    /**
     * 通用增删改
     * @param sql sql
     * @param args 参数
     */
    public void updataData(Connection con, String sql, Object ...args) {
        PreparedStatement ps = null;
        try {
            // 2. 预编译sql语句
            ps = con.prepareStatement(sql);
            // 3.填充占位符
            for (int i = 1; i <= args.length; i++) {
                ps.setObject(i, args[i - 1]);
            }
            // 4.执行sql
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closePreparedStatement(ps);
        }
    }


    /**
     * 通用查询
     * @param clazz 结果类
     * @param sql sql
     * @param args 参数
     * @param <T> 泛型
     * @return t
     */
    public <T> T queryData(Connection con, Class<T> clazz, String sql, Object ...args) {

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);
            for (int i = 1; i <= args.length; i++) {
                ps.setObject(i, args[i - 1]);
            }
            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int colNum = rsmd.getColumnCount();
            if (rs.next()) {
                T t = clazz.newInstance();
                for (int i = 1; i <= colNum; i++) {
                    // 列名
                    String colName = rsmd.getColumnLabel(i);
                    // 列值
                    Object colValue = rs.getObject(i);
                    // 反射
                    Field field = clazz.getDeclaredField(colName);
                    field.setAccessible(true);
                    field.set(t, colValue);
                }
                return t;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeResultSet(rs);
            JdbcUtil.closePreparedStatement(ps);
        }
        return null;
    }

    /**
     * 通用查询
     * @param clazz 结果类
     * @param sql sql
     * @param args 参数
     * @param <T> 泛型
     * @return 结果类的LIST
     */
    public <T> List<T> queryDataList(Connection con, Class<T> clazz, String sql, Object ...args) {

        PreparedStatement ps = null;
        ResultSet rs = null;
        List<T> listT = new ArrayList<>();
        try {
            ps = con.prepareStatement(sql);
            for (int i = 1; i <= args.length; i++) {
                ps.setObject(i, args[i - 1]);
            }
            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int colNum = rsmd.getColumnCount();
            while (rs.next()) {
                T t = clazz.newInstance();
                for (int i = 1; i <= colNum; i++) {
                    // 列名
                    String colName = rsmd.getColumnLabel(i);
                    // 列值
                    Object colValue = rs.getObject(i);
                    // 反射
                    Field field = clazz.getDeclaredField(colName);
                    field.setAccessible(true);
                    field.set(t, colValue);
                }
                listT.add(t);
            }
            return listT;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeResultSet(rs);
            JdbcUtil.closePreparedStatement(ps);
        }
        return null;
    }


    /**
     * 用于查询特殊值
     * @param con
     * @param sql
     * @param args
     * @param <E>
     * @return
     */
    public <E> E getValue(Connection con, String sql, Object ...args) {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = con.prepareStatement(sql);

            for (int i = 1; i <= args.length; i++) {
                ps.setObject(i, args[i - 1]);
            }
            rs = ps.executeQuery();
            if (rs.next()) {
                return (E) rs.getObject(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.closeResultSet(rs);
            JdbcUtil.closePreparedStatement(ps);
        }
        return null;
    }


}

上一篇:JDBC-Java连接数据库


下一篇:nmcli命令行修改网络连接名称