JDBC 中的execute()、executeQuery() 和executeUpdate() 方法有什麼不同?

WBOY
發布: 2023-09-17 14:33:12
轉載
1341 人瀏覽過

JDBC 中的execute()、executeQuery() 和executeUpdate() 方法有什么区别?

一旦您建立了語句對象,您可以使用Statement介面的execute()、executeUpdate()和executeQuery()方法之一來執行它。

execute()方法:該方法用於執行SQL DDL語句,它會傳回一個布林值,指定是否可以檢索ResultSet物件。

範例

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Example {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating the Statement
      Statement stmt = con.createStatement();

      //Executing the statement
      String createTable = "CREATE TABLE Employee( "
         + "Name VARCHAR(255), "
         + "Salary INT NOT NULL, "
         + "Location VARCHAR(255))";
      boolean bool = stmt.execute(createTable);

      System.out.println(bool);
   }
}
登入後複製

輸出

Connection established......
false
登入後複製

executeUpdate(): 這個方法用來執行插入、更新、刪除等語句。它傳回一個整數值,表示受影響的行數。

範例

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class ExecuteUpdateExample {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating the Statement
      Statement stmt = con.createStatement();
     
      String insertData = "INSERT INTO Employee("
         + "Name, Salary, Location) VALUES "
         + "('Amit', 30000, 'Hyderabad'), "
         + "('Kalyan', 40000, 'Vishakhapatnam'), "
         + "('Renuka', 50000, 'Delhi'), "
         + "('Archana', 15000, 'Mumbai')";

      int i = stmt.executeUpdate(insertData);
      System.out.println("Rows inserted: "+i);
   }
}
登入後複製

輸出

Connection established......
Rows inserted: 4
登入後複製

executeQuery():此方法用於執行傳回表格資料的語句(例如 select)。它傳回 ResultSet 類別的物件。

範例

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ExecuteQueryExample {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/sampleDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating the Statement
      Statement stmt = con.createStatement();

      //Retrieving data
      ResultSet rs = stmt.executeQuery("Select *from Employee");

      while(rs.next()) {
         System.out.print("Name: "+rs.getString("Name")+", ");
         System.out.print("Salary: "+rs.getInt("Salary")+", ");
         System.out.print("City: "+rs.getString("Location"));
         System.out.println();
      }
   }
}
登入後複製

輸出

Connection established......
Name: Amit, Salary: 30000, City: Hyderabad
Name: Kalyan, Salary: 40000, City: Vishakhapatnam
Name: Renuka, Salary: 50000, City: Delhi
Name: Archana, Salary: 15000, City: Mumbai
登入後複製

以上是JDBC 中的execute()、executeQuery() 和executeUpdate() 方法有什麼不同?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!