zoukankan      html  css  js  c++  java
  • JDBC解耦案例

    原始JDBC连接

    package jdbc;
    
            import org.junit.jupiter.api.Test;
    
            import java.sql.Connection;
            import java.sql.DriverManager;
            import java.sql.SQLException;
    
    public class JDBCUtils {
        public static Connection connection;
        private static String url="jdbc:mysql://localhost:3306/aaa?useUnicode=true&characterEncoding=utf-8";
        private static String username="root";
        private static String password="root";
    
        static{
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection(url,username,password);
    
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public static Connection getConnection(){
            return connection;
        }
    
        @Test
        public void test(){
            Connection connection = JDBCUtils.getConnection();
            System.out.println(connection);
        }
    }
    

      

    解耦JDBC连接

     

    package jdbc;
    
    import org.junit.jupiter.api.Test;
    
    import java.io

    FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;
    
    public class JDBCUtils2 {
        private static Connection connection;
        private static String url;
        private static String username;
        private static String password;
    
        static {
            try {
                //动态加载驱动
                Class.forName("com.mysql.jdbc.Driver");
                //加载配置文件
                Properties properties = new Properties();
                properties.load(new FileInputStream("src/main/java/db.properties"));
                url = properties.getProperty("url");
                username = properties.getProperty("username");
                password = properties.getProperty("password");
    
                connection = DriverManager.getConnection(url,username,password);
    
    
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public static Connection getConnection(){
            return connection;
        }
    
        @Test
        public void test(){
            Connection connection = JDBCUtils2.getConnection();
            System.out.println(connection);
        }
    }
    

      

  • 相关阅读:
    MySQL 中 InnoDB 支持的四种事务隔离级别名称,以及逐 级之间的区别?
    NOW()和 CURRENT_DATE()有什么区别?
    什么是非标准字符串类型?
    列的字符串类型可以是什么?
    实践中如何优化 MySQL ?
    可以使用多少列创建索引?
    数据库中的事务是什么?
    锁的优化策略?
    SQL 注入漏洞产生的原因?如何防止?
    强调事项段、其他事项段、关键审计事项
  • 原文地址:https://www.cnblogs.com/zyx110/p/11273860.html
Copyright © 2011-2022 走看看