Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:MySQL查询。获取生成的ID_Java_Mysql_Jdbc - Fatal编程技术网

Java:MySQL查询。获取生成的ID

Java:MySQL查询。获取生成的ID,java,mysql,jdbc,Java,Mysql,Jdbc,我有一个表,其中包含一个名为“id”的列,该列是一个自动递增的INT,并设置为主键 我需要在查询运行后获取生成的ID的值。我有以下资料: String sql = "INSERT..."; Statement statement = sqlConnection.createStatement(); int result = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS); this.id = statem

我有一个表,其中包含一个名为“id”的列,该列是一个自动递增的INT,并设置为主键

我需要在查询运行后获取生成的ID的值。我有以下资料:

String sql = "INSERT...";
Statement statement = sqlConnection.createStatement();
    int result = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
    this.id = statement.getGeneratedKeys().getInt("id");
javax.servlet.ServletException: java.sql.SQLException: Before start of result set
    com.joelj.music.rest.NewUser.doPost(NewUser.java:44)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    com.joelj.music.rest.filters.Prefilter.doFilter(Prefilter.java:44)
我在最后一行代码中得到以下异常:

javax.servlet.ServletException: java.sql.SQLException: Column 'id' not found.
    com.joelj.music.rest.NewUser.doPost(NewUser.java:44)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    com.joelj.music.rest.filters.Prefilter.doFilter(Prefilter.java:44)
如果我将其更改为
statement.getGeneratedKeys().getInt(0)我得到以下信息:

String sql = "INSERT...";
Statement statement = sqlConnection.createStatement();
    int result = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
    this.id = statement.getGeneratedKeys().getInt("id");
javax.servlet.ServletException: java.sql.SQLException: Before start of result set
    com.joelj.music.rest.NewUser.doPost(NewUser.java:44)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    com.joelj.music.rest.filters.Prefilter.doFilter(Prefilter.java:44)
我也用过其他的方法,试过这样那样。我已经看过了在互联网上找到的示例代码。我只是看不出我做错了什么

注意:查询在我提供的两种解决方案中都运行。因此,连接工作正常。它正在获取不起作用的ID的值


谢谢您的帮助。

您是否尝试过使用statement.getGeneratedKeys().getInt(1)?结果集是基于一个的。

Ahh,您还有第二个问题,请检查:在调用
.getInt(1)
之前,必须调用结果集上的
.next()
。令人惊叹的。谢谢你,迈克尔