三种 SQL 执行语句
executeQuery():該方法限于僅返回一個結(jié)果集(ResultSet)的情況,SQL中使用頻率最高的查詢語句可選擇使用該方法;
executeUpdate():該方法使用SQL中更新表(包括Insert,Delete,Update情況)以及建表或刪除表的情況,它會返回受更新影響的記錄行數(shù)。
execute():該方法可以返回結(jié)果集以及受影響行數(shù)的某種組合,多用于執(zhí)行存儲過程或者動態(tài)拼接字符串產(chǎn)生的不確定類型的SQL語句,可以使用下列方法來確定其最終的執(zhí)行效果:
stmt.execute(queryStringWithUnknownResults);
while (true) {
int rowCount = stmt.getUpdateCount();
if (rowCount > 0) { // 它是更新計數(shù)
System.out.println("Rows changed = " + count);
stmt.getMoreResults();
continue;
}
if (rowCount == 0) { // DDL 命令或 0 個更新
System.out.println(" No rows changed or statement was DDL
command");
stmt.getMoreResults();
continue;
}
// 執(zhí)行到這里,證明有一個結(jié)果集
// 或沒有其它結(jié)果
ResultSet rs = stmt.getResultSet;
if (rs != null) {
. . . // 使用元數(shù)據(jù)獲得關(guān)于結(jié)果集列的信息
while (rs.next()) {
. . . // 處理結(jié)果
stmt.getMoreResults();
continue;
}
break; // 沒有其它結(jié)果
}
轉(zhuǎn)載于:https://www.cnblogs.com/hukunyu/archive/2011/03/30/2000267.html
總結(jié)
以上是生活随笔為你收集整理的三种 SQL 执行语句的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 推荐一款UI设计软件Balsamiq M
- 下一篇: 【转】ASP中的SQL注入