欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Properties實現(xiàn)配置數(shù)據(jù)庫驅(qū)動

 更新時間:2020年05月06日 11:19:12   作者:YouLan  
這篇文章主要介紹了基于Properties實現(xiàn)配置數(shù)據(jù)庫驅(qū)動,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

優(yōu)點:

便于修改連接屬性。只需在配置文件中修改,不需要在代碼中修改了。 更易于維護代碼安全性。

方法:

在src文件嘉下創(chuàng)建database.properties文本文件;添加內(nèi)容:

driver = com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/y1
name=root
password=root

創(chuàng)建工具類MyJDBCUtiles.java,添加代碼:  

package com.kong.JDBCUtils;
 
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
 
public class MyJDBCUtiles {
  private MyJDBCUtiles(){}
  private static Connection con;
  private static String driver;
  private static String url;
  private static String name;
  private static String password;
  static{
    try {
      InputStream is = MyJDBCUtiles.class.getClassLoader().getResourceAsStream("database.properties");
      Properties properties = new Properties();
      properties.load(is);
      driver = properties.getProperty("driver");
      url = properties.getProperty("url");
      name = properties.getProperty("name");
      password = properties.getProperty("password");
      Class.forName(driver);
      con = DriverManager.getConnection(url, name, password);
    }catch (Exception ep){
      throw new RuntimeException(ep+"數(shù)據(jù)庫連接失敗");
    }
  }
  public static Connection getConnection(){
    return con;
  }

其他類使用時調(diào)用即可

輸出結(jié)果

完美^_^

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論