import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement;
public final class JdbcUtils { // 构造方法私有化 private JdbcUtils() { } // 注册驱动 static { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new ExceptionInInitializerError(); } } // 创建连接 public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url,user,password); } // 释放资源 public static void release(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) conn.close(); }catch(SQLException e) { e.printStackTrace(); } } } } private static String url ="jdbc:mysql://localhost:3306/library"; private static String user ="root"; private static String password ="shuangyuzuo";
}