zoukankan      html  css  js  c++  java
  • jdbc从基础到优化

    package com.xk.demotest.tools;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class DaoDBConectTools {
        /*
        // 传统jdbc连接
        private static final String driverName = "com.mysql.jdbc.Driver";
        private static final String url = "jdbc:mysql://localhost:3306/20170626javaweb01";
        private static final String user = "root";
        private static final String password = "root";
    
        public DaoDBConectTools() {
            Connection connect = null;
            Statement state = null;
            try {
                Class.forName(driverName);
                connect = DriverManager.getConnection(url, user, password);
                state = connect.createStatement();
                String sql = "";
                state.executeQuery(sql);
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    state.close();
                    connect.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    */
        
        private static final Properties pro = new Properties(); // ①创建properties对象
        //加载配置文件和驱动
        static {
            InputStream iStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"); // ②引入配置文件
            try {
                pro.load(iStream); // ②引入配置文件
                String driverName = pro.getProperty("driverName"); // ③加载驱动
                Class.forName(driverName);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        
        //获取连接对像并创建连接
        public static Connection connection() {
            String url = pro.getProperty("url");
            String user = pro.getProperty("userName");
            String password = pro.getProperty("password");
            Connection connect = null;
            try {
                connect = DriverManager.getConnection(url, user, password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return connect;
        }
        
        //关闭数据库连接
        public static void close(Connection connect, Statement state) {
            if (state != null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connect != null) {
                try {
                    connect.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
    
    }
    View Code
    package com.xk.demotest.tools;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.Properties;
    
    public class DaoDBConectTools {
        /*
        // 传统jdbc连接
        private static final String driverName = "com.mysql.jdbc.Driver";
        private static final String url = "jdbc:mysql://localhost:3306/20170626javaweb01";
        private static final String user = "root";
        private static final String password = "root";
    
        public DaoDBConectTools() {
            Connection connect = null;
            Statement state = null;
            try {
                Class.forName(driverName);
                connect = DriverManager.getConnection(url, user, password);
                state = connect.createStatement();
                String sql = "";
                state.executeQuery(sql);
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    state.close();
                    connect.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    */
        
        private static final Properties pro = new Properties(); // ①创建properties对象
        //加载配置文件和驱动
        static {
            InputStream iStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"); // ②引入配置文件
            try {
                pro.load(iStream); // ②引入配置文件
                String driverName = pro.getProperty("driverName"); // ③加载驱动
                Class.forName(driverName);
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        
        //获取连接对像并创建连接
        public static Connection connection() {
            String url = pro.getProperty("url");
            String user = pro.getProperty("userName");
            String password = pro.getProperty("password");
            Connection connect = null;
            try {
                connect = DriverManager.getConnection(url, user, password);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return connect;
        }
        
        //关闭数据库连接
        public static void close(Connection connect, Statement state) {
            if (state != null) {
                try {
                    state.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connect != null) {
                try {
                    connect.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        
    
    }
  • 相关阅读:
    jQuery-封装的表单元素选择器
    jQuery-获取DOM元素的各类选择器
    jQuery-外部引用和直接引用
    数据库-查询平均成绩大于60分的同学的学号和平均成绩
    数据库-查询“001”课程比“002”课程成绩高的所有学生的学号
    ps让图片背景透明
    echarts中饼图的legend自定义icon图片(扇形为例)
    获取bootstrap模态框点击的对应项(e.relatedTarget.dataset)
    echarts属性的设置(完整大全)
    小程序获取地理位置授权
  • 原文地址:https://www.cnblogs.com/huike/p/7198310.html
Copyright © 2011-2022 走看看