硬编码格式的弊端:数据库发生改变时,要重新修改代码,重新编译和部署

解决方法:将数据库信息写在配置文件当中,让程序通过读取配置文件来获得这些信息




jdbc.driver.class=com.mysql.jdbc.Driver
jdbc.connection.url=jdbc:mysql://127.0.0.1:3306/tree
jdbc.connection.username=root
jdbc.connection.password=123321


编写配置文件database.properties

创建包com.pb.tree.util,编写类ConfigManager 读取属性文件

package cn.tree.util;

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

//读取配置文件的工具类-单例模式

public class ConfigManager {

// 读取配置文件properties.load(inputstream)

private static ConfigManager configManager;

private static Properties properties;

private ConfigManager() {

String configFile = "database.properties";

properties = new Properties();

InputStream is = ConfigManager.class.getClassLoader()

.getResourceAsStream(configFile);

try {

properties.load(is);

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

public static ConfigManager getInstance() {

if (configManager == null) {

configManager = new ConfigManager();

}

return configManager;

}

public String getString(String key) {

return properties.getProperty(key);

}

}


修改MenuDao类的查询信息的方法



package cn.tree.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

//读取配置文件的工具类-单例模式
public class ConfigManager {
// 读取配置文件properties.load(inputstream)

private static ConfigManager configManager;
private static Properties properties;

private ConfigManager() {
String configFile = "database.properties";
properties = new Properties();
InputStream is = ConfigManager.class.getClassLoader()
.getResourceAsStream(configFile);

try {
properties.load(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public static ConfigManager getInstance() {
if (configManager == null) {
configManager = new ConfigManager();
}
return configManager;
}

public String getString(String key) {
return properties.getProperty(key);
}

}


  



package cn.tree.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.tree.util.ConfigManager;

public class MenuDao {

// 查询主目录
public void getMainList() {
Connection connection = null;
Statement st = null;
ResultSet rs = null;
String driver = ConfigManager.getInstance().getString(
"jdbc.driver.class");
String url = ConfigManager.getInstance().getString(
"jdbc.connection.url");
String username = ConfigManager.getInstance().getString(
"jdbc.connection.username");
String password = ConfigManager.getInstance().getString(
"jdbc.connection.password");

try {
// 1、加载驱动
Class.forName(driver);
// 2、获取数据库连接
connection = DriverManager.getConnection(url, username, password);
// 3、获取Statement对象,执行sql语句
String sql = "select * from main";
st = connection.createStatement();
rs = st.executeQuery(sql);
// 4、处理sql执行结果
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println("id: " + id + "\t" + "name: " + name);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
// 5、释放资源
rs.close();
st.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
MenuDao menuDao = new MenuDao();
menuDao.getMainList();
}
}