package day02.demo02;
import java.io.FileReader;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
/**
* 得到配置文件中的Driver,URL,User,Password;
*/
public static Connection getConection() throws SQLException {
Properties properties = new Properties();
FileReader fr = null;
Connection connection = null;
String driver = null;
String url = null;
String user = null;
String password = null;
try {
fr = new FileReader("echo\\jdbcConfig.properties");
properties.load(fr);
driver = properties.getProperty("Driver");
url = properties.getProperty("URL");
user = properties.getProperty("User");
password = properties.getProperty("Password");
fr.close();
//将驱动注册到内存中
Class.forName(driver);
//返回connection对象
} catch (Exception e) {
e.printStackTrace();
}
connection = DriverManager.getConnection(url, user, password);
return connection;
}
|
|