项目代码如下:
config配置文件:
className=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/lob
user=root
password=root
com.itheima.util包下DBUtil.java
package com.itheima.util;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class DBUtil {
private static Properties properties = null;
static {
properties = new Properties();
try {
properties.load(new FileReader(DBUtil.class.getClassLoader().getResource("config.properties").getPath()));
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public static Connection getConn() {
String className = properties.getProperty("className");
String url = properties.getProperty("url");
String user = properties.getProperty("user");
String password = properties.getProperty("password");
Connection conn;
try {
Class.forName(className);
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new Runt