• jdbc


    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;
        }
    
    
    }
    
    
  • 相关阅读:
    zookeeper端口号冲突:8080冲突
    Linux 开机报 or type Control-D to continue
    linux分区和系统文件和挂载
    linux添加JAVA环境变量
    root用户安装的软件在普通用户不生效
    微信小程序:text元素中加入空格
    Java Swing:JPanel添加边框
    Java Swing:JPanel中添加JPanel
    Spring Boot:项目打包成war并发布到Tomcat上运行
    IDEA将MAVEN项目打包成war包
  • 原文地址:https://www.cnblogs.com/damahuhu/p/15143081.html
Copyright © 2020-2023  润新知