delete 必须使用where 条件指定删除哪一行数据。通常用具有唯一性的主键进行标示。
package qddx.JDBC;
import java.sql.*;
public class DeletebbsVo {
public void deletebbsVo(int id){
Connection conn = null;
PreparedStatement pst = null;
try{
conn = JDBC_Connection.getConnection();
String sql = "delete from article where id=?";
pst=conn.prepareStatement(sql);
pst.setInt(1, id);
pst.executeUpdate();
System.out.println("删除成功");
}catch(SQLException e){
e.printStackTrace();
}finally{
JDBC_Connection.free(null, conn, pst);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
DeletebbsVo delete = new DeletebbsVo();
int id = 3;
bbsVo vo = new bbsVo();
QueryById queryById = new QueryById();//调用查询方法,查询id为3 的数据是否存在。
vo = queryById.QuerybbsVoById(id);
if(vo !=null){//查询不为空 则进行删除
delete.deletebbsVo(id);
}else{
System.out.println("删除失败 id为"+id+"的数据不存在");
}
}
}