zoukankan      html  css  js  c++  java
  • jdbc连接数据库的五种方式

    方式一

    @Test
        public void testConnection1() throws SQLException {
            // 获取Driver实现类对象
            Driver driver = new com.mysql.cj.jdbc.Driver();
            // jdbc:mysql协议
            // localhost:ip地址
            // 3306:默认的mysql的端口号
            // school:数据库
            String url = "jdbc:mysql://localhost:3306/school";
            // 将用户名和密码封装在Properties中
            Properties info = new Properties();
            info.setProperty("user", "root");
            info.setProperty("password", "xxx");
            Connection connection = driver.connect(url, info);
            System.out.println(connection);
        }

    方式二

    /*
         * 是程序更具有可移植性 利用反射
         */
        @Test
        public void testConnection2() throws Exception {
            // 获取Driver实现类对象
            Class clazz = Class.forName("com.mysql.jdbc.Driver");
            // newInstance需要拥有空参构造器
            Driver driver = (Driver) clazz.newInstance();
            // 提供要连接的数据库
            String url = "jdbc:mysql://localhost:3306/school";
            // 提供连接需要的用户名和密码
            Properties properties = new Properties();
            properties.setProperty("user", "root");
            properties.setProperty("password", "xxx");
            // 获取连接
            Connection connect = driver.connect(url, properties);
            System.out.println(connect);
    
        }

      方式二在方式一的基础上进行了迭代,利用反射来避免第三方API的出现,使程序更具有移植性。

    方式三

    // 方式三使用DriverManager来代替Driver
        @Test
        public void testConnection3() throws Exception {
            // 获取Driver实现类对象
            Class clazz = Class.forName("com.mysql.cj.jdbc.Driver");
            Driver driver = (Driver) clazz.newInstance();
            // 提供另外三个连接的基本信息
            String url = "jdbc:mysql://localhost:3306/school";
            String user = "root";
            String password = "xxx";
            // 注册驱动
            DriverManager.registerDriver(driver);
            // 获取连接
            Connection connection = DriverManager
                    .getConnection(url, user, password);
            System.out.println(connection);
    
        }

    方式四

    // 方式四只用加载驱动不用显示的注册驱动
        @Test
        public void testConection4() throws Exception{
            // 加载Driver
            Class.forName("com.mysql.cj.jdbc.Driver");
            
            // 提供另外三个连接的基本信息
            String url = "jdbc:mysql://localhost:3306/school";
            String user = "root";
            String password = "xxx";
            //省略如下操作,
            /*// 注册驱动
            DriverManager.registerDriver(driver);*/
            // 获取连接
            Connection connection = DriverManager
                    .getConnection(url, user, password);
            System.out.println(connection);
        }

    可能大家会有这样的疑问,为什么这里不需要注册驱动,我们可以从Driver类的源码得知答案

    static  {
         try {
         DriverManager.registerDriver(new Driver());
       } catch (SQLException E) {
           throw new RuntimeException("Can't register driver!");
         } 
       }

    在Driver类加载的过程中,已经帮我们进行了驱动的注册!

    方式五:最终版本

    将连接所需要的信息写入properties文件中

    例如:jdbc.preperties

    user=root
    password=xxx
    url=jdbc:mysql://localhost:3306/school
    driverClass=com.mysql.cj.jdbc.Driver

    //实现数据和代码的分离。实现了解耦
        //方式五:将数据库需要的四个基本信息声明在配置文件中,通过读取配置文件获取连接
        @Test
        public void testConection5() throws Exception{
            //读取配置文件的基本信息
            InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties properties = new Properties();
            properties.load(is);
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String url = properties.getProperty("url");
            String driverClass = properties.getProperty("driverClass");
            //加载驱动
            Class.forName(driverClass);
            //获取连接
            Connection connection = DriverManager.getConnection(url, user, password);
            System.out.println(connection);
            
        }
    笨鸟先飞
  • 相关阅读:
    存储过程系列之存储过程sql查询存储过程的使用
    SQL Server 连接字符串和身份验证详解
    存储过程
    Objective-C:MRC(引用计数器)在OC内部的可变对象是适用的,不可变对象是不适用的(例如 NSString、NSArray等)
    Objective-C:MRC(引用计数器)获得对象所有权的方式(init、retain、copy等)
    Objective-C:MRC手动释放对象内存举例(引用计数器)
    C语言:内存的分配与管理
    Objective-C:继承、分类(Category、extension)、协议(protocol),个人理解,仅供参考
    Objective-C:在类中设置不同协议
    Objective-C:继承的体现
  • 原文地址:https://www.cnblogs.com/zoutingrong/p/14589588.html
Copyright © 2011-2022 走看看