• JDBC封装


    public class JDBCUtil {
        private static  String driver = "com.mysql.jdbc.Driver";
        private static  String url = "jdbc:mysql://localhost:3306/?day14/useUnicode=true&characterEncoding=utf-8";
        private static  String username = "root";
        private static  String password = "admin";
    
        private static  Connection conn =null;
        private static  PreparedStatement statement = null;
        private static  ResultSet result = null;
    
    
    
        /**
         * 加载配置文件
         * 获取数据库连接
         * @return
         */
        static{
            try {
                Properties properties = new Properties();
                //通过类加载器 加载配置文件
                properties.load(JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
    
                driver = properties.getProperty("jdbc.driver");
                url = properties.getProperty("jdbc.url");
                username = properties.getProperty("jdbc.username");
                password = properties.getProperty("jdbc.password");
    
                //加载驱动
                Class.forName(driver);
            }catch (IOException e){
                e.printStackTrace();
            }catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 获取连接
         * @return
         */
        public static Connection getConnection(){
            try {
                conn = DriverManager.getConnection(url,username,password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return conn;
        }
    
        /**
         * 获取操作数据库对象
         * @param sql
         * @param objects
         * @return
         */
        public static PreparedStatement getStatement(String sql,Object...objects){
            //加载驱动
            try {
                //创建执行对象
                statement = conn.prepareStatement(sql);
                //如果有参数  则添加参数
                if (objects.length > 0){
                    for (int i=0;i<objects.length;i++){
                        statement.setObject(i+1,objects[i]);
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return statement;
        }
    
        /**
         * 查询
         * 返回查询结果集
         * @param sql 语句
         * @param objects 可变参数
         * @return ResultSet 结果集合
         */
        public static ResultSet executeQuery(String sql,Object...objects){
            statement = getStatement(sql,objects);
            try {
                result = statement.executeQuery();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return result;
        }
    
        /**
         * 对数据库的增,删,改
         * @param sql 语句
         * @param objects 可变参数
         * @return 操作完成的sql语句数量
         */
        public static int executeUpdate(String sql,Object...objects){
            statement = getStatement(sql,objects);
            //执行成功的条数
            int count = 0;
            try {
                count = statement.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return count;
        }
    
        /**
         * 关闭连接 释放资源
         */
        public static void closeAll(){
            try{
                if (result != null){
                    result.close();
                }
                if (statement != null){
                    statement.close();
                }
                if (conn != null){
                    conn.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    HDU 5319 Painter
    HDU 5328 Problem Killer
    HDU 5327 Olympiad
    HDU 5339 Untitled
    HDU 5335 Walk Out
    HDU 5317 RGCDQ
    HDU 5326 Work
    CF GYM 100703A Tea-drinking
    CF GYM 100703B Energy Saving
    CF GYM 100703F Game of words
  • 原文地址:https://www.cnblogs.com/loveyoul9/p/11580396.html
Copyright © 2020-2023  润新知