zoukankan      html  css  js  c++  java
  • <一>获取数据库连接

    一、JDBC_通过Driver接口获取数据库连接

    1、 Driver是一个接口:数据库厂商必须提供实现的接口,可以从其中 获取数据库连接。

    2、JDBC URL由三部分组成,各部分用冒号隔开,格式:jdbc:<子协议>:<子名称>

      协议:JDBC URL中的协议总是jdbc  jdbc

      子协议:子协议用于标识一个数据库驱动程序   mysql

      子 名称:一种标识数据库的方法,子名称可以依不同的子协议而变化,用自名称的目的是为了定位数据库,提供足够的信息。   localhost:3306/test

    3、Properties类

         JAVA的包中,有提供专门的操作属性文件的类,java.uitl.Properties类,该类是一个集合类,所以,Properties会将属性以集合的方式读写;

      该类的详细介绍参考:   http://blog.csdn.net/qinpeng100423/article/details/9125857

    4、代码实现

    @Test
     public void test() throws SQLException {
        Driver driver=new Driver();
        String url="jdbc:mysql://192.168.56.1:3306/test";
        Properties info=new Properties();
        info.put("user", "root");
        info.put("password", "kk");
        Connection connection=driver.connect(url, info);
        System.out.println(connection);
     }

    5、编写一个通用的方法,在不修改原程序的情况下,可以获取任何数据库的连接?

      ---将数据库驱动Driver实现类的全类名、url、user、password放入一个配置文件中,通过修改配置文件的方式实现和具体的数据库解耦。

    实现如下:

    1>准备连接数据库的4个字符串

    2>获取jdbc.properties对应的输入流

    3>创建Properties对象

    4>加载对应的输入流

    5>具体决定以上4个字符串

    6>加载数据库驱动程序

    7>获取数据库连接

     public Connection getConnection() throws Exception{
      
        String driverClass=null;
        String jdbcUrl=null;
        String user=null;
        String password=null;
      
        //读取类路径下的jdbc.properties文件
        InputStream in=getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties=new Properties();

       //加载输入流
        properties.load(in);
      
        driverClass=properties.getProperty("driver");
        jdbcUrl=properties.getProperty("jdbcUrl");
        user=properties.getProperty("user");
        password=properties.getProperty("password");
        //通过反射创建Driver对象
        Driver driver=(Driver)Class.forName(driverClass).newInstance();
      
        Properties info=new Properties();
        info.put("user", user);
        info.put("password", password);
        Connection connection=driver.connect(jdbcUrl,info);
        return connection;
     }

    jdbc.properties配置文件:

      driver=com.mysql.jdbc.Driver
      jdbcUrl=jdbc:mysql://192.168.56.1:3306/test
      user=root
      password=kk

    二、通过DriverManager的getConnection()方法获取数据库连接

    1、DriverManager是驱动的管理类:

        1>可以通过重载的getConnection()方法获取数据库连接,较为方便

        2>可以同时管理多个驱动程序:若注册了多个数据库连接,则调用getConnection()方法时传入的参数不同,即返回不同的数据库连接。

    2、代码实现

    1>准备连接数据库的4个字符串

    2>获取jdbc.properties对应的输入流

    3>创建Properties对象

    4>加载对应的输入流

    5>具体决定以上4个字符串

    6>加载数据库驱动程序(对应的Driver实现类中有注册驱动的静态代码块)

    7>获取数据库连接

    @Test
     public void testDriverManager() throws Exception{
        //1、准备数据库连接的4个字符串
        String driverClass=null;   //驱动的全类名
        String jdbcUrl=null;      //JDBC URL
        String user=null;
        String password=null;
      
        //读取类路径下的jdbc.properties文件
        InputStream in=getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties=new Properties();
        properties.load(in);
        driverClass=properties.getProperty("driver");
        jdbcUrl=properties.getProperty("jdbcUrl");
        user=properties.getProperty("user");
        password=properties.getProperty("password");
        //加载数据库驱动程序
        Class.forName(driverClass);
        //通过DriverManager的getConnection()方法获取数据库连接
        Connection connection=DriverManager.getConnection(jdbcUrl,user,password);
        System.out.println(connection);
     }
     @Test
     public void testDriverManager1() throws Exception{
        //1、准备数据库连接的4个字符串
        String driverClass="com.mysql.jdbc.Driver";   //驱动的全类名
        String jdbcUrl="jdbc:mysql:///test";      //JDBC URL
        String user="root";
        String password="kk";
      
        //加载数据库驱动程序
        Class.forName(driverClass);
        //通过DriverManager的getConnection()方法获取数据库连接
        Connection connection=DriverManager.getConnection(jdbcUrl,user,password);
        System.out.println("向日葵:"+connection);
     }

      

    三、数据库连接释放工具类

    JDBCTools.java

    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 JDBCTools {

       public static Connection getConnection() throws Exception {

          String driverClass = null;
          String jdbcUrl = null;
          String user = null;
          String password = null;

          // 读取类路径下的jdbc.properties文件
          InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
          Properties properties = new Properties();
          properties.load(in);

          driverClass = properties.getProperty("driver");
          jdbcUrl = properties.getProperty("jdbcUrl");
          user = properties.getProperty("user");
          password = properties.getProperty("password");
          // 加载数据库驱动程序
          Class.forName(driverClass);
          // 通过DriverManager的getConnection()方法获取数据库连接
          Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
          return connection;

       }
     
       public static void release(Statement st,Connection conn){
      
          if (st!=null) {
             try {
                st.close();
             } catch (SQLException e) {
                e.printStackTrace();
             }
          }
      
          if (conn!=null) {
             try {
                conn.close();
             } catch (SQLException e) {
                e.printStackTrace();
             }
          }
        }
    }

      

  • 相关阅读:
    .NET Core开发实战(第10课:环境变量配置提供程序)--学习笔记
    .NET Core开发实战(第9课:命令行配置提供程序)--学习笔记
    .NET Core开发实战(第8课:配置框架:让服务无缝适应各种环境)--学习笔记
    .NET Core开发实战(第7课:用Autofac增强容器能力)--学习笔记(下)
    .NET Core开发实战(第7课:用Autofac增强容器能力)--学习笔记(上)
    .NET Core开发实战(第6课:作用域与对象释放行为)--学习笔记(下)
    .NET Core开发实战(第6课:作用域与对象释放行为)--学习笔记(上)
    .NET Core开发实战(第5课:依赖注入:良好架构的起点)--学习笔记(下)
    .NET Core开发实战(第5课:依赖注入:良好架构的起点)--学习笔记(中)
    .NET Core开发实战(第5课:依赖注入:良好架构的起点)--学习笔记(上)
  • 原文地址:https://www.cnblogs.com/iamkk/p/6057489.html
Copyright © 2011-2022 走看看