主頁 > 知識庫 > MySQL execute、executeUpdate、executeQuery三者的區(qū)別

MySQL execute、executeUpdate、executeQuery三者的區(qū)別

熱門標簽:百度競價排名 網(wǎng)站排名優(yōu)化 呼叫中心市場需求 Linux服務(wù)器 鐵路電話系統(tǒng) 服務(wù)外包 AI電銷 地方門戶網(wǎng)站

execute、executeUpdate、executeQuery三者的區(qū)別(及返回值)

一、boolean execute(String sql)

允許執(zhí)行查詢語句、更新語句、DDL語句。

返回值為true時,表示執(zhí)行的是查詢語句,可以通過getResultSet方法獲取結(jié)果;返回值為false時,執(zhí)行的是更新語句或DDL語句,getUpdateCount方法獲取更新的記錄數(shù)量。

例子:

public static void main(String[] args) { 
 
 Connection conn = null; 
 Statement stm = null; 
 ResultSet rs = null; 
 try { 
  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
  conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=Test;user=sa;password=sasa"); 
  stm = conn.createStatement(); 
  boolean ret = stm.execute("select * from stuinfo"); 
  if(ret){ 
  rs = stm.getResultSet(); 
  while(rs.next()){ 
   System.out.println("姓名:"+rs.getString("stuName")+"\t年齡:"+rs.getString("stuScore")); 
  } 
  } 
  ret = stm.execute("update stuinfo set stuScore=62 where stuname='張三'"); 
  int count = stm.getUpdateCount(); 
  if(!ret){ 
  System.out.println(count+"條數(shù)據(jù)修改成功!"); 
  } 
 } catch (ClassNotFoundException e) { 
  e.printStackTrace(); 
 } catch (SQLException e) { 
  e.printStackTrace(); 
 }  
 } 

二、int executeUpdate(String sql)

執(zhí)行給定 SQL 語句,該語句可能為 INSERT、UPDATE 或 DELETE 語句,或者不返回任何內(nèi)容的 SQL 語句(如 SQL DDL 語句)。

返回值是更新的記錄數(shù)量

三、ResultSet executeQuery(String sql)

執(zhí)行給定的 SQL 語句,該語句返回單個 ResultSet 對象。

execute是executeUpdate與executeQuery的綜合

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

您可能感興趣的文章:
  • 通過代碼示例了解submit與execute的區(qū)別
  • PHP PDOStatement::execute講解
  • JDBC Oracle執(zhí)行executeUpdate卡死問題的解決方案
  • MySQL中預(yù)處理語句prepare、execute與deallocate的使用教程
  • Failed to execute goal org...的解決辦法
  • ThreadPoolExecutor線程池原理及其execute方法(詳解)
  • python executemany的使用及注意事項
  • 簡單解析execute和submit有什么區(qū)別

標簽:湘潭 仙桃 湖南 蘭州 衡水 黃山 崇左 銅川

巨人網(wǎng)絡(luò)通訊聲明:本文標題《MySQL execute、executeUpdate、executeQuery三者的區(qū)別》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266