黑马程序员技术交流社区
标题:
JDBC静态工具类获取connection对象
[打印本页]
作者:
EchoL
时间:
2019-4-18 22:46
标题:
JDBC静态工具类获取connection对象
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;
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2