package cn.itcast.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import cn.itcast.jdbc.dataSource.MyDataSource;
public final class JdbcUtils {
private static MyDataSource myDataSource = null ;
private JdbcUtils () {
}
static {
try {
Class.forName("com.mysql.jdbc.Driver" );
myDataSource = new MyDataSource();
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
public static Connection getConnection () throws SQLException {
return myDataSource.getConnection();
}
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 {
try {
if (conn != null )
myDataSource.free(conn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}