• 7.24Java之配置JDBC配置文件


    7.24Java之配置JDBC配置文件

    思路

    • 创建File目录,存放数据库的一些信息

    • 创建MySQL目录,存放三个连接配置类

      • MySQL配置类:存放从File目录下获取到的数据库的配置信息--->是数据库类和配置文件交互的中间类

      • MySQL有用的类(util):存放文件连接到具体的数据库的方法,返回Properties内容对象

      • MySQL连接类:具体的连接到数据库的方法--->反射连接


    后期预计做一个SQL类,存放写的sql语句

    File目录

    DRIVER=
    URL=
    USERNAME=
    PASSWORD=

    MySQLConfig

    package MySQLConnection;

    /**
    * 一个数据库连接工具类,负责连接数据库。
    * 提供连接MySQL数据库需要的属性信息
    * @since JDK 1.8
    * @date 2021/07/24
    * @author Lucifer
    */
    public class MySQLConfig {
       /*设置配置文件的四个属性*/
       private static String driver = null;
       private static String url = null;
       private static String username = null;
       private static String password = null;

       public static void setDriver(String driver) {
           MySQLConfig.driver = driver;
      }

       public static void setUrl(String url) {
           MySQLConfig.url = url;
      }

       public static void setUsername(String username) {
           MySQLConfig.username = username;
      }

       public static void setPassword(String password) {
           MySQLConfig.password = password;
      }

       public static String getDriver() {
           return driver;
      }

       public static String getUrl() {
           return url;
      }

       public static String getUsername() {
           return username;
      }

       public static String getPassword() {
           return password;
      }

       /*提供一个构造器*/
       public MySQLConfig() {
      }

       @Override
       public String toString() {
           return "MySQLConfig Information Is:" + "\r\n"
                   + "Driver--->" + getDriver() + "\r\n"
                   + "Url--->" + getUrl() + "\r\n"
                   + "UserName--->" + getUsername() + "\r\n"
                   + "PassWord--->" + getPassword();
      }
    }

    MySQLUtil

    package MySQLConnection;

    import java.io.FileReader;
    import java.io.IOException;
    import java.io.Reader;
    import java.util.Properties;

    /**
    * 提供File类去读取不同的数据库配置文件--->这是一个方法,形参传入不同的数据库配置文件路径
    * 提供静态初始化方法去获取里面的信息
    * @since JDK 1.8
    * @date 2021/07/24
    * @author Lucifer
    */
    public class MySQLUtil extends MySQLConfig{
      /*定义文件路径--->oms的路径*/
      private static String omsFilePath = "D:/workspace/JDBC/src/File/OmsOnlineMySQL.properties";
      /*定义文件路径--->erp的路径*/
      private static String erpFilePath = "D:/workspace/JDBC/src/File/ErpOnlineMySQL.properties";
      /*定义文件路径--->gms的路径*/
      private static String gmsFilePath = "D:/workspace/JDBC/src/File/GmsOnlineMySQL.properties";
      /*定义文件路径--->rbac的路径*/
      private static String rbacFilePath = "D:/workspace/JDBC/src/File/RbacOnlineMySQL.properties";
      /*定义文件路径--->listing的路径*/
      private static String skuFilePath = "D:/workspace/JDBC/src/File/SkuListingOnlineMySQL.properties";
      /*定义文件路径--->stock的路径*/
      private static String stockFilePath = "D:/workspace/JDBC/src/File/StockOnlineMySQL.properties";
      /*创建内容引用属性*/
      private static Properties properties = new Properties();

      /*提供一个构造器*/
      public MySQLUtil() {
      }

      /*提供一个获取内容(Properties)的方法返回一个内容对象,形参传入文件路径*/
      public static Properties getConfig(String filePath){
          //创建读取内容的引用
          Reader in;
          try {
              in = new FileReader(filePath);
              //load方法 从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象
              properties.load(in);
          }catch (IOException e){
              System.out.println(e.getMessage());
              e.printStackTrace();
          }
          //获取内容当中的配置信息
          setDriver(properties.getProperty("DRIVER"));
          setUrl(properties.getProperty("URL"));
          setUsername(properties.getProperty("USERNAME"));
          setPassword(properties.getProperty("PASSWORD"));
          //返回一个内容对象
          return properties;
      }

      /*连接OMS数据库*/
      public static Properties getOmsConfig(){
          //创建读取内容的引用
          Reader in;
          try {
              in = new FileReader(omsFilePath);
              //load方法 从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象
              properties.load(in);
          }catch (IOException e){
              System.out.println(e.getMessage());
              e.printStackTrace();
          }
          //获取内容当中的配置信息
          setDriver(properties.getProperty("DRIVER"));
          setUrl(properties.getProperty("URL"));
          setUsername(properties.getProperty("USERNAME"));
          setPassword(properties.getProperty("PASSWORD"));
          //返回一个内容对象
          return properties;
      }

      /*连接ERP数据库*/
      public static Properties getErpConfig(){
          //创建读取内容的引用
          Reader in;
          try {
              in = new FileReader(erpFilePath);
              //load方法 从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象
              properties.load(in);
          }catch (IOException e){
              System.out.println(e.getMessage());
              e.printStackTrace();
          }
          //获取内容当中的配置信息
          setDriver(properties.getProperty("DRIVER"));
          setUrl(properties.getProperty("URL"));
          setUsername(properties.getProperty("USERNAME"));
          setPassword(properties.getProperty("PASSWORD"));
          //返回一个内容对象
          return properties;
      }

      /*连接GMS数据库*/
      public static Properties getGmsConfig(){
          //创建读取内容的引用
          Reader in;
          try {
              in = new FileReader(gmsFilePath);
              //load方法 从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象
              properties.load(in);
          }catch (IOException e){
              System.out.println(e.getMessage());
              e.printStackTrace();
          }
          //获取内容当中的配置信息
          setDriver(properties.getProperty("DRIVER"));
          setUrl(properties.getProperty("URL"));
          setUsername(properties.getProperty("USERNAME"));
          setPassword(properties.getProperty("PASSWORD"));
          //返回一个内容对象
          return properties;
      }

      /*连接RBAC数据库*/
      public static Properties getRbacConfig(){
          //创建读取内容的引用
          Reader in;
          try {
              in = new FileReader(rbacFilePath);
              //load方法 从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象
              properties.load(in);
          }catch (IOException e){
              System.out.println(e.getMessage());
              e.printStackTrace();
          }
          //获取内容当中的配置信息
          setDriver(properties.getProperty("DRIVER"));
          setUrl(properties.getProperty("URL"));
          setUsername(properties.getProperty("USERNAME"));
          setPassword(properties.getProperty("PASSWORD"));
          //返回一个内容对象
          return properties;
      }

      /*连接listing数据库*/
      public static Properties getListingConfig(){
          //创建读取内容的引用
          Reader in;
          try {
              in = new FileReader(skuFilePath);
              //load方法 从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象
              properties.load(in);
          }catch (IOException e){
              System.out.println(e.getMessage());
              e.printStackTrace();
          }
          //获取内容当中的配置信息
          setDriver(properties.getProperty("DRIVER"));
          setUrl(properties.getProperty("URL"));
          setUsername(properties.getProperty("USERNAME"));
          setPassword(properties.getProperty("PASSWORD"));
          //返回一个内容对象
          return properties;
      }

      /*连接STOCK数据库*/
      public static Properties getStockConfig(){
          //创建读取内容的引用
          Reader in;
          try {
              in = new FileReader(stockFilePath);
              //load方法 从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象
              properties.load(in);
          }catch (IOException e){
              System.out.println(e.getMessage());
              e.printStackTrace();
          }
          //获取内容当中的配置信息
          setDriver(properties.getProperty("DRIVER"));
          setUrl(properties.getProperty("URL"));
          setUsername(properties.getProperty("USERNAME"));
          setPassword(properties.getProperty("PASSWORD"));
          //返回一个内容对象
          return properties;
      }
    }

    MySQLConnection

    package MySQLConnection;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.Properties;

    /**
    * 定义具体的数据库连接方法
    * @since JDK 1.8
    * @date 2021/07/24
    * @author Lucifer
    */
    public class MySQLConnection extends MySQLConfig{
       /*定义链接需要的属性*/
       private static final String Driver = getDriver();
       private static final String Url = getUrl();
       private static final String UserName = getUsername();
       private static final String PassWord = getPassword();

       /*静态代码块初始化反射*/
       static
      {
           try {
               Class.forName(Driver).newInstance();
          } catch (InstantiationException e) {
               System.out.println(e.getMessage());
               e.printStackTrace();
          } catch (IllegalAccessException e) {
               System.out.println(e.getMessage());
               e.printStackTrace();
          } catch (ClassNotFoundException e) {
               System.out.println(e.getMessage());
               e.printStackTrace();
          }
      }

       /**
        * 定义具体的连接方法
        * 面向接口编程--->使用接口接收对象
        */
       public static Connection mysqlConnection(Properties properties){
           //创建Connection引用
           Connection connection = null;
           /*连接数据库*/
           try {
               /*使用Connection接口接收实现类对象*/
               connection = DriverManager.getConnection(Url,UserName,PassWord);
               //判断连接是否成功
               if (!connection.isClosed()){
                   System.out.println("数据库连接成功!");
                   System.out.println(new MySQLConfig().toString());
              }
          }catch (SQLException e){
               /*显示异常状态*/
               System.out.println("SQLState:" + e.getSQLState());
               System.out.println("VendorError:" + e.getErrorCode());
               e.printStackTrace();
          }
           return connection;
      }
    }

     

    It's a lonely road!!!
  • 相关阅读:
    PL/SQL Developer 和 instantclient客户端安装配置(图文)
    VirtualBox + Centos 使用NAT + Host-Only 方式联网
    zookeeper的安装
    Socket编程基础篇
    WebSocket教程(二)
    WebSocket教程(一)
    Js判断浏览器类型
    JVM内存模型
    js 正则去除指定的单词
    Java线上应用故障排查之一:高CPU占用
  • 原文地址:https://www.cnblogs.com/JunkingBoy/p/15054958.html
Copyright © 2020-2023  润新知