java RETURN_GENERATED_KEYS

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java RETURN_GENERATED_KEYS相关的知识,希望对你有一定的参考价值。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Main {
  private static final String URL = "jdbc:mysql://localhost/testdb";

  private static final String USERNAME = "root";

  private static final String PASSWORD = "";

  public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    String insert = "INSERT INTO orders (username, order_date) VALUES ('foobar', '2007-12-13')";
    Statement stmt = conn.createStatement();

    stmt.executeUpdate(insert, Statement.RETURN_GENERATED_KEYS);

    ResultSet keys = stmt.getGeneratedKeys();
    int lastKey = 1;
    while (keys.next()) {
      lastKey = keys.getInt(1);
    }
    System.out.println("Last Key: " + lastKey);
    conn.close();
  }
}

以上是关于java RETURN_GENERATED_KEYS的主要内容,如果未能解决你的问题,请参考以下文章

Java 布尔运算

java [Java] Java常用代码#java

Java - 35 Java 实例

Java While 循环

Java 字符串

Java If ... Else