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