天天看點

Java中删除資料庫中的資訊

public boolean deletePost(int postId){

Connection conn = null;

PreparedStatement ps = null; 

String sql = null;   

try {

conn = DbUtil.getConn();

sql = "delete from post  where postId=? ";

ps = conn.prepareStatement(sql);

ps.setInt(1, postId);

ps.executeUpdate();

System.out.println("文章删除成功!");

} catch (SQLException e) {

e.printStackTrace();

return false;  

}finally{

DbUtil.closeConn(conn, ps, null);

return true;

}

ps = conn.prepareStatement(sql);

ps = conn.prepareCall(sql);

prepareStatement :建立一個 

PreparedStatement

 對象來将參數化的 SQL 語句發送到資料庫

prepareCall:建立一個 

CallableStatement

 對象來調用資料庫存儲過程

prepareStatement :建立一個 

PreparedStatement

 對象來将參數化的 SQL 語句發送到資料庫

prepareCall:建立一個 

CallableStatement

 對象來調用資料庫存儲過程

prepareStatement :建立一個 

PreparedStatement

 對象來将參數化的 SQL 語句發送到資料庫