标签:
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 }
标签:
原文地址:http://www.cnblogs.com/maple-shanqiu/p/5470776.html