delete 必須使用where 條件指定刪除哪一行數據。通常用具有唯一性的主鍵進行標示。
1 package qddx.JDBC; 2 import java.sql.*; 3 public class DeletebbsVo { 4 public void deletebbsVo(int id){ 5 Connection conn = null; 6 PreparedStatement pst = null; 7 try{ 8 conn = JDBC_Connection.getConnection(); 9 String sql = "delete from article where id=?"; 10 pst=conn.prepareStatement(sql); 11 pst.setInt(1, id); 12 pst.executeUpdate(); 13 System.out.println("刪除成功"); 14 }catch(SQLException e){ 15 e.printStackTrace(); 16 }finally{ 17 JDBC_Connection.free(null, conn, pst); 18 } 19 } 20 public static void main(String[] args) { 21 // TODO Auto-generated method stub 22 DeletebbsVo delete = new DeletebbsVo(); 23 int id = 3; 24 bbsVo vo = new bbsVo(); 25 QueryById queryById = new QueryById();//調用查詢方法,查詢id為3 的數據是否存在。 26 vo = queryById.QuerybbsVoById(id); 27 if(vo !=null){//查詢不為空 則進行刪除 28 delete.deletebbsVo(id); 29 }else{ 30 System.out.println("刪除失敗 id為"+id+"的數據不存在"); 31 32 } 33 34 } 35 36 }