JDBC Driver for Xero

Build 22.0.8479

INSERT ステートメント

新しいレコードを作成するには、INSERT ステートメントを使用します。

INSERT 構文

INSERT ステートメントは、挿入するカラムと新しいカラム値を指定します。複数のカラム値は、次の例に示すように、VALUES 句のカンマ区切りリストで指定できます。

INSERT INTO <table_name> 
( <column_reference> [ , ... ] )
VALUES 
( { <expression> | NULL } [ , ... ] ) 
  

<expression> ::=
  | @ <parameter> 
  | ?
  | <literal>
Statement およびPreparedStatement クラスのexecuteUpdate メソッドを使って、データ操作コマンドを実行し影響を受ける行を取得できます。 最後に挿入されたレコードのContactId を取得するには、getGeneratedKeys を使用します。さらに、prepareStatement を呼び出すときは、ステートメントクラスのRETURN_GENERATED_KEYS フラグを設定します。
String cmd = "INSERT INTO Contacts (Name) VALUES (?)";
PreparedStatement pstmt = connection.prepareStatement(cmd,Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1, "John Doe");
int count = pstmt.executeUpdate();
System.out.println(count+" rows were affected");
ResultSet rs = pstmt.getGeneratedKeys();
while(rs.next()){	  
  System.out.println(rs.getString("ContactId"));
}
connection.close();	

Copyright (c) 2023 CData Software, Inc. - All rights reserved.
Build 22.0.8479