首頁 >資料庫 >mysql教程 >JDBC 中的execute()、executeQuery() 和executeUpdate() 方法有什麼不同?

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

WBOY
WBOY轉載
2023-09-17 14:33:121393瀏覽

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刪除