• 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();
                }
            }
        }
        
    
    }
  • 相关阅读:
    android 开发-自定义多节点进度条显示
    android开发 ,对接支付宝,服务器(PHP)校验失败
    android开发支付宝接口开发流程(密钥篇)
    android开发 解决启动页空白或黑屏问题
    关于的 recorder robotium 的Eclipse插件(URL:http://recorder.robotium.com/updates/或者说不可用)
    git android.google 源码:Unknown SSL protocol error in connection to code.google.com:443
    解决方案:android monkeyrunner:Timeout while trying to create chimp mananger(device = MonkeyRunner.waitForConnection()一直报错的问题)
    JasperReport学习札记6-JRXML的标签
    SQL Server 的动态语句(SQLServer 的String.format用法)(SQLServer的调用SQL占位符的使用)
    IReport5.6.0创建数据库连接找不到驱动(iReport中ClassNotFoundError错误的解决)
  • 原文地址:https://www.cnblogs.com/huike/p/7198310.html
Copyright © 2020-2023  润新知