MySQL
 Computer >> コンピューター >  >> プログラミング >> MySQL

JDBCプログラムを使用してResultSetの内容を更新するにはどうすればよいですか?


ResultSetの内容を更新するには、次のように、ResultSetタイプを更新可能に渡してステートメントを作成する必要があります。

//Creating a Statement object
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);

getXXX()およびsetXXX()メソッドと同様に、ResultSetインターフェースは、結果セットupdateXXX()の行の内容を更新するメソッドも提供します。

これらのメソッドは、更新する行のインデックスを表す整数値、または列ラベルを表す文字列値を受け入れます。

ResultSetの内容を更新する必要がある場合は、テーブルに主キーが必要であることに注意してください。

以下に示すように、5つのレコードを持つEmployeesという名前のテーブルがあるとします。

+----+---------+--------+----------------+
| Id | Name    | Salary | Location       |
+----+---------+--------+----------------+
|  1 | Amit    | 3000   | Hyderabad      |
|  2 | Kalyan  | 4000   | Vishakhapatnam |
|  3 | Renuka  | 6000   | Delhi          |
|  4 | Archana | 9000   | Mumbai         |
|  5 | Sumith  | 11000  | Hyderabad      |
+----+---------+--------+----------------+

次の例は、結果セットの内容を更新する方法を示しています。

import java.sql.*;
public class ResultSetExample {
   public static void main(String[] args) throws Exception {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/TestDB";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating a Statement object
      Statement stmt = con.createStatement(
      ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
      //Retrieving the data
      ResultSet rs = stmt.executeQuery("select * from Employees");
      //Printing the contents of the table
      System.out.println("Contents of the table: ");
      printRs(rs);
      //Moving the pointer to the starting point in the ResultSet
      rs.beforeFirst();
      //Updating the salary of each employee by 5000
      while(rs.next()){
         //Retrieve by column name
         int newSal = rs.getInt("Salary") + 5000;
         rs.updateInt( "Salary", newSal );
         rs.updateRow();
      }
      System.out.println("Contents of the ResultSet after increasing salaries");
      printRs(rs);
      // Set position to second record first
      rs.beforeFirst();
      rs.absolute(2);
      System.out.println("Record we need to delete: ");
      System.out.print("ID: " + rs.getInt("id"));
      System.out.print(", Salary: " + rs.getInt("Salary"));
      System.out.print(", Name: " + rs.getString("Name"));
      System.out.println(", Location: " + rs.getString("Location"));
      System.out.println(" ");
      //Deleting the row
      rs.deleteRow();
      System.out.println("Contents of the ResultSet after deleting one records...");
      printRs(rs);
      System.out.println("Goodbye!");
   }
   public static void printRs(ResultSet rs) throws SQLException{
      //Ensure we start with first row
      rs.beforeFirst();
      while(rs.next()){
         System.out.print("ID: " + rs.getInt("id"));
         System.out.print(", Salary: " + rs.getInt("Salary"));
         System.out.print(", Name: " + rs.getString("Name"));
         System.out.println(", Location: " + rs.getString("Location"));
      }
      System.out.println();
   }
}

出力

Connection established......
Contents of the table:
ID: 1, Salary: 3000, Name: Amit, Location: Hyderabad
ID: 2, Salary: 4000, Name: Kalyan, Location: Vishakhapatnam
ID: 3, Salary: 6000, Name: Renuka, Location: Delhi
ID: 4, Salary: 9000, Name: Archana, Location: Mumbai
ID: 5, Salary: 11000, Name: Sumith, Location: Hyderabad
Conetnets of the resultset after increaing salaries
ID: 1, Salary: 8000, Name: Amit, Location: Hyderabad
ID: 2, Salary: 9000, Name: Kalyan, Location: Vishakhapatnam
ID: 3, Salary: 11000, Name: Renuka, Location: Delhi
ID: 4, Salary: 14000, Name: Archana, Location: Mumbai
ID: 5, Salary: 16000, Name: Sumith, Location: Hyderabad
Record we need to delete:
ID: 2, Salary: 9000, Name: Kalyan, Location: Vishakhapatnam
Contents of the resultset after deleting one records...
ID: 1, Salary: 8000, Name: Amit, Location: Hyderabad
ID: 3, Salary: 11000, Name: Renuka, Location: Delhi
ID: 4, Salary: 14000, Name: Archana, Location: Mumbai
ID: 5, Salary: 16000, Name: Sumith, Location: Hyderabad
Goodbye!

  1. JavaScriptを使用してユーザー選択のテキストコンテンツを取得するプログラム。

    以下は、JavaScriptを使用してユーザー選択のテキストコンテンツを取得するためのコードです- 例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <

  2. iPhone でモバイル接続を使用して天気アプリを更新する方法

    iPhone で天気予報アプリを使用するのは簡単なことのように思えるかもしれませんが、ちょっとした癖があります。Wi-Fi に接続していないと更新されません。そのため、天気予報を最新バージョンに更新する場合は、この記事で説明するトラブルシューティングを行う必要があります。 天気アプリには、天気アプリに期待するすべての機能が備わっています。この天気アプリケーションは、現在地の気象条件を表示します。家やその特定の場所を離れてアプリを更新しようとすると、更新された情報が表示されません。また、新しい都市や新しい場所を追加しようとしても、空白の画面が表示されるだけです. iPhone の天気アプリに