16.DAO设计思想与搭建骨架_JdbcUtil
package test.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public final class JdbcUtil {
private static String driver = "oracle.jdbc.driver.OracleDriver";
private static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
private static String user = "scott";
private static String password = "scott";
private JdbcUtil() {
}
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
public static void free(ResultSet rs, Statement st, Connection conn){
try {
if (rs != null)
rs.close();
} catch(SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch(SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
}
}
}