[英]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.