如何使用jdbc getgeneratedkeys获取生成的uuid类型?

ma8fv8wu  于 2021-06-24  发布在  Mysql
关注(0)|答案(3)|浏览(368)

这是特定于mysql和 UUID 我使用的id类型 uuid() mysql本身的功能。它给我的不是空的 resultSet ,但是 next() 值始终为空。
代码如下:

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");
}
nle07wnf

nle07wnf1#

没有生成密钥,但实际提供了:
因此,要么将创建工作留给数据库:

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

由于我不知道如何在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());

获取uuid就变得无关紧要了。

kq0g1dla

kq0g1dla2#

对于SQLServer2008和更高版本,可以使用自动生成的uuid定义主键。若要检索insert语句生成的uuid,请在列定义之后添加output选项,并替换statement.executeupdate for statement.executequery,然后从结果集检索值。下面是一个代码片段,演示如何执行此操作:

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;
            }
li9yvcax

li9yvcax3#

乔普伊根的回答解决了这个问题。但是,如果方法出现任何错误,也可以执行以下操作:

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

相关问题