要是考虑到JDBC连接中用到Connection 在每次对数据进行增删查改都要开启、关闭,在实例开发项目中,浪费了很大的资源。因此采用连接池技术。
//数据库连接
public class jdbcConnection{
private static String driver;
privete static String url;
private static String username;
private static String password;
static{
try{
//通过当前类获取类加载器
ClassLoader classLoader = jdbcConnection.class.getClassLoader();
//通过类加载器的方法获得一个输入流
InputStream is = classLoader.getResourceAsStream(“db.properties”);
//创建一个properties类
Properties props = new Properties();
//加载输入流
Props.load(is);
//获取相关参数的值
driver = props.getProperty(“driver”);
url = props.getProperty(“url”);
username = props.getProperty(“username”);
Password = props.getProperty(“password”);
}catch(IOException e){
e.printStackTrace();
}
}
//获取连接方法
public static Connection getConnection(){
Connection conn = null;
try{
Class.forName(driver);
conn = DriverManager.getConnection(url,username,password);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}