public class OracleConn {
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("oracle:jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@localhost:1521:數據庫SID";
String user ="數據庫用戶名";
String password = "數據密碼";
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
}
return conn;
}
//關閉連接
public static void tryClose(ResultSet set, Statement stat, Connection conn) {
if (set != null) {
try {
set.close();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
set = null;
}
}
if (stat != null) {
try {
stat.close();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
stat = null;
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
conn = null;
}
}
}
//測試連接
public static boolean testConn() {
boolean isConn = false;
Connection conn = null;
try {
Class.forName(ConfigBean.getForName());
String url = ConfigBean.getUrl();
String user = ConfigBean.getUser();
String password = ConfigBean.getPwd();
conn = DriverManager.getConnection(url, user, password);
isConn = true;
conn.close();
} catch (ClassNotFoundException e) {
// TODO 自動生成 catch 塊
e.printStackTrace();
} catch (SQLException e) {
// TODO 自動生成 catch 塊
isConn = false;
e.printStackTrace();
}
return isConn;
}
}
請務必保留此出處:http://735696.blog.51cto.com/725696/146351