java後臺的mysql執行語句
阿新 • • 發佈:2019-02-20
Java後臺資料庫執行語句的寫法:
@Override
public void update(NotifyInfo notifyInfo) {String hqldelete = "update NotifyInfo e set e.content=:content,e.creatDate=:time where e.nid=:nid";
Query query = sessionFactory.getCurrentSession().createQuery(hqldelete);
query.setParameter("content", notifyInfo.getContent());
query.setParameter("time", notifyInfo.getCreatDate());
query.setParameter("nid", notifyInfo.getNid());
query.executeUpdate();
}
在Java中如何使用execute()、executeQuery()、executeUpdate()三個方法?
execute(String sql)
執行給定的 SQL 語句,該語句可能返回多個結果。
executeQuery(String sql)
執行給定的 SQL 語句,該語句返回單個 ResultSet 物件(資料庫語句執行後生成的資料表)
executeUpdate(String sql)
執行給定 SQL 語句,該語句可能為 INSERT、UPDATE 或 DELETE 語句,或者不返回任何內容的 SQL 語句(如 SQL DDL 語句)
execute(),executeQuery()一般在查詢中使用
executeUpdate() 在插入、更新、刪除時使用