zoukankan      html  css  js  c++  java
  • java基础之JDBC三:简单工具类的提取及应用

    简单工具类:

    public class JDBCSimpleUtils {
        /**
         * 私有构造方法
         */
        private JDBCSimpleUtils() {
        }
    
        /**
         * 驱动
         */
        public static String driver = null;
        /**
         * 连接字符串
         */
        public static String url = null;
        /**
         * 用户名
         */
        public static String user = null;
        /**
         * 密码
         */
        public static String password = null;
    
        /**
         * 读取配置文件, 并将读取到的值赋值给变量.
         */
        public static void readConfig() {
            try {
                //读取properties配置文件给变量赋值
                Properties pp = new Properties();
                pp.load(new FileReader("day04_classTest\src\config.properties"));
                url = pp.getProperty("url");
                user = pp.getProperty("username");
                driver = pp.getProperty("driver");
                password = pp.getProperty("password");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 静态代码块
         * 变量赋值
         * 并注册驱动
         */
        static {
            try {
                //变量赋值
                readConfig();
                //注册驱动
                Class.forName(driver);
            } catch (Exception e) {
                //写入日志
                //Logger.WriteLog(JDBCUtils.class.getName(),"");
                e.printStackTrace();
            }
        }
    
        /**
         * 获取Connection
         *
         * @return 数据库连接
         */
        public static Connection getConnection() {
    
            try {
                return DriverManager.getConnection(url, user, password);
            } catch (SQLException ex) {
                //写入日志
                //Logger.WriteLog(JDBCUtils.class.getName(),"");
                ex.printStackTrace();
                return null;
            }
        }
    
        public static Statement getStatement(Connection conn) {
            if (conn == null) {
                return null;
            }
            try {
                return conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                        ResultSet.CONCUR_UPDATABLE);
                // 设置数据集可以滚动,可以更新
            } catch (SQLException ex) {
                //写入日志
                //Logger.WriteLog(JDBCUtils.class.getName(),"");
                release(conn);
            }
            return null;
        }
    
        /**
         * 获取一个带参数的 PreparedStatement
         * 该 PreparedStatement 已经设置数据集 可以滚动,可以更新
         *
         * @param conn      数据库连接
         * @param cmdText   需要 ? 参数的 SQL 语句
         * @param cmdParams SQL 语句的参数表
         * @return 如果获取失败将返回 null,调用时记得检查返回值
         */
        public static PreparedStatement getPreparedStatement(Connection conn, String cmdText, Object... cmdParams) {
            if (conn == null) {
                return null;
            }
            PreparedStatement pstmt = null;
            try {
                pstmt = conn.prepareStatement(cmdText, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
                int i = 1;
                for (Object item : cmdParams) {
                    pstmt.setObject(i, item);
                    i++;
                }
            } catch (SQLException e) {
                e.printStackTrace();
                release(conn, pstmt);
                //写入日志
                //Logger.WriteLog(JDBCUtils.class.getName(),"");
            }
            return pstmt;
        }
    
    
        /**
         * 释放资源
         *
         * @param conn Connection
         * @param stat Statement
         * @param rs   ResultSet
         */
        public static void release(Connection conn, Statement stat, ResultSet rs) {
            try {
                if (rs != null) {
                    rs.close();
                    rs = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
                //写入日志
                //Logger.WriteLog(JDBCUtils.class.getName(),"");
            } finally {
                try {
                    if (stat != null) {
                        stat.close();
                        stat = null;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                    //写入日志
                    //Logger.WriteLog(JDBCUtils.class.getName(),"");
                } finally {
                    try {
                        if (conn != null) {
                            conn.close();
                            conn = null;
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                        //写入日志
                        //Logger.WriteLog(JDBCUtils.class.getName(),"");
                    }
                }
            }
        }
    
        /**
         * 释放资源
         *
         * @param conn Connection
         * @param stat Statement
         */
        public static void release(Connection conn, Statement stat) {
            try {
                if (stat != null) {
                    stat.close();
                    stat = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
                //写入日志
                //Logger.WriteLog(JDBCUtils.class.getName(),"");
            } finally {
                try {
                    if (conn != null) {
                        conn.close();
                        conn = null;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                    //写入日志
                    //Logger.WriteLog(JDBCUtils.class.getName(),"");
                }
            }
        }
    
        /**
         * 释放资源
         *
         * @param conn Connection
         */
        public static void release(Connection conn) {
            try {
                if (conn != null) {
                    conn.close();
                    conn = null;
                }
            } catch (SQLException e) {
                e.printStackTrace();
                //写入日志
                //Logger.WriteLog(JDBCUtils.class.getName(),"");
            }
        }
    }

    简单工具类的应用:

        /**
         * 增删改的方法
         *
         * @param sql
         * @param obj
         */
        private static void executeNoQuery(String sql, Object... obj) {
            //获取Connection
            Connection conn = JDBCSimpleUtils.getConnection();
            //获取PreparedStatement
            PreparedStatement ps = JDBCSimpleUtils.getPreparedStatement(conn, sql, obj);
            try {
                int i = ps.executeUpdate();
                if (i > 0) {
                    System.out.println("ok");
                } else {
                    System.out.println("error");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                JDBCSimpleUtils.release(conn, ps);
            }
        }
    
    
        /**
         * 查询返回ResultSet
         *
         * @param sql
         */
        private static void getResultSet(String sql) {
            //获取Connection
            Connection conn = JDBCSimpleUtils.getConnection();
            //获取Statement
            Statement stat = JDBCSimpleUtils.getStatement(conn);
            ResultSet rs = null;
            try {
                rs = stat.executeQuery(sql);
                while (rs.next()) {
                    int id = rs.getInt("uid");
                    String name = rs.getString("uname");
                    String psw = rs.getString("psw");
                    System.out.println(id + "--" + name + "--" + psw);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                JDBCSimpleUtils.release(conn, stat, rs);
            }
        }
  • 相关阅读:
    linux用户管理初级(上)
    定时任务实战
    linux定时任务
    Linux 命令行基础(一)
    Linux 操作系统发展历程及系统版本选择
    Linux文件的权限
    Linux硬连接和软连接
    Linux 文件的属性
    linux 正则表达式
    centos7 设置时间同步
  • 原文地址:https://www.cnblogs.com/blazeZzz/p/9168896.html
Copyright © 2011-2022 走看看