使用JDBC完成分类表CRUD的操作
工具类 通过之前的案例回顾,不难发现,有很多的代码操作是重复的,比如“获取链接”和“释放资源”等,将来在增删改查中经常遇到,开发中遇到这种情况,将采用工具类的方法进行抽取,从而达到代码的重复利用。 此处使用V1版本,之后还有替他版本。 获取链接 /** * 获取连接方法 * * @return */ public static Connection getConnection() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/web08", "root", "root"); } catch (Exception e) { e.printStackTrace(); } return conn; } View Code 释放资源 public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); }