package demo.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtils {
private static String driver = “oracle.jdbc.OracleDriver”;
private static String url = “jdbc:oracle:thin:@localhost:1521:orcl”;
private static String user = “scott”;
private static String password = “tiger”;
static{
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection(){
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/*
* 执行java程序:
* java -Xms100M -Xmx200M HelloWorld
*/
public static void release(Connection conn, Statement st, ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
rs = null;//垃圾回收: 是否可以通过代码干预垃圾回收??
}
}
if(st!=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
st = null;//垃圾回收: 是否可以通过代码干预垃圾回收??
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn = null;//垃圾回收: 是否可以通过代码干预垃圾回收??
}
}
}
}