zoukankan      html  css  js  c++  java
  • Java程序连接数据库

    /**
         * 了解: 利用 Driver 接口的 connect 方法获取连接
         */
    // 第一种实现
        /**
         * 了解: 利用 Driver 接口的 connect 方法获取连接
         */
        @Test
        public void oracleJdbcTest() throws Exception {
            Driver driver = null; // sun提供的接口
            String url = "jdbc:oracle:thin:@192.168.5.139:1521:ORCL";
            Properties info = null;
            info = new Properties();
            info.put("user", "scott");
            info.put("password", "tiger");
            driver = new OracleDriver(); // Oracle数据库厂商自己实现sun提供的接口Driver
            Connection connect = driver.connect(url, info); // 获取连接
            System.out.println(connect);
        }
    ======================================
    /**
         * 了解: 使用 DriverManager 来获取数据库连接
         * 版本1: 
         * 好处: 不需要使用原生的 Driver 方法来获取连接. 
         * 缺点: 还是耦合了具体的实现类. 
         */
    //第二种实现
        @Test
        public void oracleJdbcTest1() throws Exception{
            Connection connection=null;
            DriverManager.registerDriver(new OracleDriver()); //驱动管理器注册Oracle驱动,实现Java程序可以连接Oracle数据库,如果想要连接不同的数据库则需要注册不同的数据库驱动
            String url="jdbc:oracle:thin:@192.168.5.139:1521:ORCL";
            Properties info=null;
            info=new Properties();
            info.put("user", "scott");
            info.put("password", "tiger");
            connection=DriverManager.getConnection( url, info) ; //DriverManager驱动管理器类,里面的方法都是静态的,类调用获取到一个连接
            System.out.println(connection);
        }
    ====================================
    /**
         * 了解: 更进一步, 不需要关联任何 JDBC 驱动的实现类。 
         * 但需要提供 JDBC 驱动中 Driver 接口的实现类的全类名的字符串. 
         */
    //第三种实现
        @Test
        public void oracleJdbcTest2() throws Exception, InstantiationException, IllegalAccessException, ClassNotFoundException{
            Connection connection=null;
            String className="oracle.jdbc.driver.OracleDriver";
            DriverManager.registerDriver((Driver)Class.forName(className).newInstance()); //注册驱动
            String url="jdbc:oracle:thin:@192.168.5.139:1521:ORCL";
            Properties info=null;
            info=new Properties();
            info.put("user", "scott");
            info.put("password", "tiger");
            connection=DriverManager.getConnection(url,info);
            System.out.println(connection);
        }
    ===========================================================
    /**
         * 能创建一个不和具体 Driver 耦合的获取数据库连接的方法. 即在方法中不再关联任何数据库驱动的 JDBC 实现.
         * @throws SQLException 
         */
    
    /**
         * final version: 若需要手动获取数据库连接: 
         * 
         * 更进一步, 不需要关联任何 JDBC 驱动的实现类。 
         * 但需要提供 JDBC 驱动中 Driver 接口的实现类的全类名的字符串. 
         * 
         * 实际上, 在驱动的实现类中有一个静态代码块: 创建了 Driver 实现类的对象, 并把其注册给 DriverManager
         *  static {
         *      try {
         *          java.sql.DriverManager.registerDriver(new Driver());
         *      } catch (SQLException E) {
         *          throw new RuntimeException("Can't register driver!");
         *      }
         *  }
         *  
         *  而调用 Class 的 forName 方法在加载类实例时, 会调用静态代码块.
         * 
         */
    //第四种实现(最常用)
        @Test
        public void oracleJdbcTest3() throws Exception{
            Connection connection=null;
            String className="oracle.jdbc.driver.OracleDriver";
            String url="jdbc:oracle:thin:@192.168.5.139:1521:ORCL";
            Properties info=null;
            info=new Properties();
            info.put("user", "scott");
            info.put("password", "tiger");
            Class.forName(className).newInstance();//加载驱动
            connection=DriverManager.getConnection(url,info);
            System.out.println(connection);
        }
    
  • 相关阅读:
    html5笔记
    CGI
    php和apache的关系和作用()
    sass最佳实践
    javascript笔记——jQuery插件开发的几种方式
    关于ajax解析
    http协议详解
    【python之路6】pycharm的使用
    【每日一linux命令7】用户及用户组
    【每日一linux命令6】命令中的命令
  • 原文地址:https://www.cnblogs.com/lisingshen/p/7499739.html
Copyright © 2011-2022 走看看