@Test / * * * 删除操作的代码实现 * / public void demo 3 ( ) { Connection conn = null; Statement stmt = null; try { / / 注册驱动 : Class.forName ( "com.mysql.jdbc.Driver" ) ; / / 获得连接 : conn = DriverManager.getConnection ( "jdbc:mysql:///web_test3" , "root" , "abc" ) ; / / 创建执行SQL语句对象 : stmt = conn.createStatement ( ) ; / / 编写SQL : String sql = "delete from user where id = 5" ; / / 执行SQL : int num = stmt.executeUpdate ( sql ) ; if ( num > 0 ) { System.out.println ( "删除用户成功!!!" ) ; } } catch ( Exception e ) { e.printStackTrace ( ) ; } finally { / / 资源释放: if ( stmt ! = null ) { try { stmt. close ( ) ; } catch ( SQLException e ) { e.printStackTrace ( ) ; } stmt = null; } if ( conn ! = null ) { try { conn. close ( ) ; } catch ( SQLException e ) { e.printStackTrace ( ) ; } conn = null; } } } |