📄 connectionfactory.java
字号:
package com.briup.bms.common;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
/**
*
* @author Jimmy.Zhang
*
* 2007-7-11上午11:20:40
*/
public class ConnectionFactory {
private static String driver = null;
private static String url = null;
private static String user = null;
private static String password = null;
static {
Properties pro = new Properties();
String filename = ConnectionFactory.class.getResource("")+"application.properties";
File file = new File(filename.substring(6));
try {
FileInputStream fis = new FileInputStream(file);
pro.load(fis);
driver = pro.getProperty("driver");
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
throw new RuntimeException(e);
}
return conn;
}
public static void close(ResultSet rs,
Statement stmt,
Connection conn) {
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
} catch(SQLException e1) {}
}
public static void main(String[] args) {
System.out.println(ConnectionFactory.getConnection());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -