import java.sql.*; //工具类不是为继承的,final所以不能继承,静态代码块只执行一次 public final class jdbcUtils { //private static保护起来,能私有就私有, private static String Url="jdbc.mysql://locallhost:3306/jdbc"; private static String user="root"; private static String passworld="123"; //别人不能调用private来构造实例,想要的话:???单例+静态的public private jdbcUtils(){ } static{ try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { throw new ExceptionInInitializerError(e); } } public static Connection getConnection() throws SQLException{ return DriverManager.getConnection(Url,user,passworld); } //释放 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(); } } } } }