Initial commit

This commit is contained in:
Zzzz
2026-04-27 18:40:30 +08:00
commit 2120774b05
112 changed files with 12308 additions and 0 deletions
@@ -0,0 +1,57 @@
package com.mzh.library.util;
import com.mzh.library.exception.DaoException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
public final class JdbcUtil {
private static final String CONFIG_FILE = "db.properties";
private static final String DEFAULT_DRIVER = "com.mysql.cj.jdbc.Driver";
private JdbcUtil() {
}
public static Connection getConnection() {
Properties properties = loadProperties();
String driver = properties.getProperty("db.driver", DEFAULT_DRIVER);
String url = required(properties, "db.url");
String username = required(properties, "db.username");
String password = required(properties, "db.password");
try {
Class.forName(driver);
return DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException | SQLException ex) {
throw new DaoException("Unable to open database connection", ex);
}
}
private static Properties loadProperties() {
try (InputStream inputStream = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream(CONFIG_FILE)) {
if (inputStream == null) {
throw new DaoException("Missing database configuration file: " + CONFIG_FILE, null);
}
Properties properties = new Properties();
properties.load(inputStream);
return properties;
} catch (IOException ex) {
throw new DaoException("Unable to read database configuration", ex);
}
}
private static String required(Properties properties, String key) {
String value = properties.getProperty(key);
if (value == null || value.trim().isEmpty()) {
throw new DaoException("Missing database configuration value: " + key, null);
}
return value.trim();
}
}