简体   繁体   English

使用Java从SQL表获取主键值

[英]Get the primary key value from SQL table using java

I'm using java to insert users to a sql database table. 我正在使用Java将用户插入sql数据库表。 The insert works great. 插入效果很好。 However, the user id is the primary key and gets auto increment. 但是,用户ID是主键,并且会自动递增。 I want to get the value of the user id after I inserted the new user to the table. 将新用户插入表后,我想获取用户ID的值。

Here is the insert part: 这是插入部分:

PreparedStatement create_statement;
StringBuilder sb = new StringBuilder();
sb.append("INSERT INTO ");
sb.append(this.get_database());
sb.append(".dbo.");
sb.append(this.get_table());
sb.append(" ( username, password, email_addr, first_name, middle_name, last_name, dob, street_addr, phone_num, bio)   ");
sb.append(" VALUES( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? );");

try {
    create_statement = this.sql_connection.prepareStatement(sb.toString());
    create_statement.setString(1, this.username);
    create_statement.setString(2, this.password);
    create_statement.setString(3, this.email_addr);
    create_statement.setString(4, this.first_name);
    create_statement.setString(5, this.middle_name);
    create_statement.setString(6, this.last_name);
    create_statement.setString(7, this.dob);
    create_statement.setString(8, this.street_addr);
    create_statement.setString(9, this.phone_num);
    create_statement.setString(10, this.bio);
    create_statement.executeUpdate();

I'm looking for the answer for an hour, some do it by using ResultSet . 我正在寻找一个小时的答案,有些是通过使用ResultSet来完成的 However, I keep getting errors or null when trying to get the data from create_statement 但是,尝试从create_statement获取数据时,我总是收到错误消息或为null

Help :( 救命 :(

Compare executeQuery and executeUpdate here: 在此处比较executeQuery和executeUpdate:

http://docs.oracle.com/javase/1.4.2/docs/api/java/sql/PreparedStatement.html http://docs.oracle.com/javase/1.4.2/docs/api/java/sql/PreparedStatement.html

You won't get a ResultSet from executeUpdate because it returns an int. 您不会从executeUpdate获得ResultSet,因为它返回一个int。 Nor can you use executeQuery here, because you're INSERTing, not SELECTing anything. 您也不能在此处使用executeQuery,因为您要插入,而不是选择任何内容。 Use this instead 改用这个

create_statement = this.sql_connection.prepareStatement(sb.toString(),
                   Statement.RETURN_GENERATED_KEYS);
//your code
ResultSet res = create_statement.getGeneratedKeys();

And even when learning, variables should be named appropriately, it's not a create statement, is it? 即使在学习时,变量也应适当命名,这不是create语句,对吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java + Hibernate在SQL Server的主键中插入值? - How to insert value into primary key in SQL server by using java + hibernate? 通过使用带有 SQL WHERE 子句的 Java 变量从结果集中获取主键值 - Getting primary key value from resultset by using Java variable with SQL WHERE clause 从 ResultSet Java 中获取主键列 - Get Primary Key Column from ResultSet Java Java JDBC在表中插入数据后获取字符串主键值 - Java JDBC Get string primary key value after inserting data in table 在hibernate中从外键表中获取主键表的结果 - get results of primary key table from foreign key table in hibernate 如何从JAVA中的ResultSet或ResultSetMetaData对象获取数据库表的主键的列名? - How can I get the column name of the primary key of a Database Table from the ResultSet or ResultSetMetaData object in JAVA? 从Java中的SQLlite数据库表中获取第一个和最后一个主键值? - Get first and last primary key values from SQLlite db table in Java? 使用 PostgreSQL 中的值序列获取主键约束冲突(从 Java 调用) - Getting primary key constraint violation using sequence for value in PostgreSQL (called from Java) Java,MySQL使用ID(主键)更新表 - Java, MySQL updating table using ID (primary key) 使用存储过程选择后如何获取主键值? - How to get the Primary Key value after Select using Stored Procedure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM