简体   繁体   English

如何使用 JDBC getGeneratedKeys 获取生成的 UUID 类型?

[英]How to get a generated UUID type using JDBC getGeneratedKeys?

This is specific to MySQL and UUID type of id for which I am using uuid() function of MySQL itself.这是特定于 MySQL 和UUID类型的 id,我正在使用 MySQL 本身的uuid()函数。 It's giving me not null resultSet , but next() value is always empty.它给了我 not null resultSet ,但next()值始终为空。

Following is the code:以下是代码:

String insertQuery = "INSERT INTO fixtures "
                        + "(id, whatever1, whatever2, whatever3, whatever4, "
                        ...
                        + "created_at) "
                        + "VALUES(UUID(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

PreparedStatement preparedStatement = connection.prepareStatement(insertQuery, Statement.RETURN_GENERATED_KEYS);
.
.
.
preparedStatement.setDate(23, sqlDate);
preparedStatement.executeUpdate();
ResultSet keysResultSet = preparedStatement.getGeneratedKeys();
System.out.println(keysResultSet); // com.mysql.cj.jdbc.result.ResultSetImpl@6892b3b6
if(keysResultSet != null && keysResultSet.next()){
     System.out.println("Generated Emp Id: "+keysResultSet.getString("id"));
} else {
     System.out.println("No KEYS GENERATED");
}

There is no key generated, but actually provided:没有生成密钥,但实际上提供了:

So either leave the creation to the database:因此,要么将创建留给数据库:

String insertQuery = "INSERT INTO fixtures "
                        + "(whatever1, whatever2, whatever3, whatever4, "
                        ...
                        + "created_at) "
                        + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, "
                        + "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

As I am not aware of how to easily achieve a UUID on the MySQL side, I would suggest the original solution with a UUID on java side:由于我不知道如何在 MySQL 端轻松实现 UUID,我建议在 Java 端使用 UUID 的原始解决方案:

String insertQuery = "INSERT INTO fixtures "
                        + "(id, whatever1, whatever2, whatever3, whatever4, "
                        ...
                        + "created_at) "
                        + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, "
                        + "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
 UUID id = UUID.randomUUID();
 preparedStatement.setString(1, id.toString());

Getting the UUID then becomes irrelevant.然后获取 UUID 变得无关紧要。

For SQL Server 2008 and later you can define a primary key using an auto-generated UUID.对于 SQL Server 2008 及更高版本,您可以使用自动生成的 UUID 定义主键。 To retrieve the UUID generated by the INSERT statement, add the OUTPUT option after your column definitions, and replace your statement.executeUpdate for statement.executeQuery and retrieve the value from the ResultSet.要检索由 INSERT 语句生成的 UUID,请在列定义之后添加 OUTPUT 选项,并将 statement.executeUpdate 替换为 statement.executeQuery 并从 ResultSet 中检索值。 Here is a code fragment showing how to do it:这是一个显示如何执行此操作的代码片段:

            sqlStmt.append("INSERT INTO tableName");
            sqlStmt.append(" (column1");
            sqlStmt.append(", column2");
...
            sqlStmt.append(" OUTPUT inserted.columnUUID");
            sqlStmt.append(" VALUES (?, ?, ?, ?) ");
            statement = connection.prepareStatement(sqlStmt.toString());
            statement.clearParameters();
            int index = 0;
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next())
            {
                result = resultSet.getString(1);
                companyDB.varUUID = result;
            }

Jop Eggan's answer solves it. Jop Eggan 的回答解决了这个问题。 However, you can do the following too in case the approach gives any errors:但是,如果该方法出现任何错误,您也可以执行以下操作:

String insertQuery = "INSERT INTO fixtures "
                        + "(id, whatever1, whatever2, whatever3, whatever4, "
                        ...
                        + "created_at) "
                        + "VALUES(?, ?, ?, ?, ?, ?, ?, ?, "
                        + "?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
 UUID id = UUID.randomUUID();
 preparedStatement.setObject(1, id);

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

相关问题 在多线程环境中使用 JDBC getGeneratedKeys 函数 - Using the JDBC getGeneratedKeys function in multithreaded environment 在MySQL中使用statement.getGeneratedKeys()时是否可以获得自动生成的值? - Is it possible to get a auto-generated values when using statement.getGeneratedKeys() in MySQL? sql jdbc getgeneratedkeys返回未找到列“id”,列类型未知 - sql jdbc getgeneratedkeys returns column “id” not found, column type unknown UUID 生成的字符类型 - Type of Character generated by UUID 如何使Statement.getGeneratedKeys工作 - How to get Statement.getGeneratedKeys working 使用 AttributeConverter 转换为 UUID 会导致“没有 JDBC 类型的方言映射”异常 - Using AttributeConverter converting to UUID results in 'No Dialect mapping for JDBC type' exception 如何使用SimpleJdbcInsert和executeBatch与MYSQL JDBC驱动程序生成密钥? - How to get generated keys using SimpleJdbcInsert and executeBatch with MYSQL JDBC driver? 使用PreparedStatement的getGeneratedKeys()无法在Java中找到生成的密钥 - Unable to find a generated key in Java using PreparedStatement's getGeneratedKeys() 如何减少使用 randomUUID( ) 生成的 UUID 的长度 - how to reduce length of UUID generated using randomUUID( ) 此getGeneratedKeys()JDBC语句有什么问题? - What is wrong with this getGeneratedKeys() JDBC statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM