zoukankan      html  css  js  c++  java
  • DriverManager是驱动的管理类调用配置文件创建Connection对象

    /**
    * DriverManager是驱动的管理类。
    * 1)可以通过重载的getConnection()方法获取数据库连接,而Driver类需要调用Properties,较为方便
    * 2)可以同时管理多个驱动程序:若注册了多个数据库连接,则调用getConnection()方法时传入的参数不同,即返回不同的数据库连接
    * @throws ClassNotFoundException
    * @throws SQLException
    * @throws IOException
    */

    @Test
    public void testGetConnection2() throws ClassNotFoundException, IOException, SQLException{
    System.out.println(getConnection2());
    }

    public Connection getConnection2() throws IOException, ClassNotFoundException, SQLException{
    //1、准备连接数据库的4个字符串
    //1)创建properties对象
    Properties properties=new Properties();
    //2)获取jdbc.properties对应的输入流
    InputStream in=getClass().getClassLoader().getResourceAsStream("jdbc.properties");
    //3)加载2)对应的输入流
    properties.load(in);
    //4)具体决定四个字符串
    String url=properties.getProperty("jdbcUrl");
    String user=properties.getProperty("user");
    String password=properties.getProperty("password");
    String driverClass=properties.getProperty("driverClass");
    //2、加载数据库驱动程序(对应的Driver实现类中有注册驱动的静态代码块)
    Class.forName(driverClass);
    //3、通过DriverManager的getConnection()方法获取数据库连接
    Connection connection=DriverManager.getConnection(url, user, password);
    return connection;
    //System.out.println(connection);
    //return connection;
    }

  • 相关阅读:
    分布式锁
    zookeeper
    工作流笔记第四天_流程变量
    工作流笔记第三天_流程实例
    工作流笔记第二天_流程定义的CRUD
    工作流笔记第一天_简单介绍activiti
    groovy修改代码不用重启通过监听记录改变时间重新加载
    遇到的前端问题
    常用正则表达式大全
    Hibernate中Session.get()方法和load()方法的详细比较
  • 原文地址:https://www.cnblogs.com/xiaona19841010/p/5192309.html
Copyright © 2011-2022 走看看