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

时间:2018-01-15 10:58:30

标签: java mysql jdbc uuid

这是MySQL和UUID类型的id特有的,我正在使用MySQL本身的uuid()函数。它不会为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");
}

3 个答案:

答案 0 :(得分:1)

没有生成密钥,但实际提供了:

所以要么将创建留给数据库:

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然后变得无关紧要。

答案 1 :(得分:0)

对于SQL Server 2008和更高版本,您可以使用自动生成的UUID定义主键。要检索INSERT语句生成的UUID,请在列定义之后添加OUTPUT选项,然后将statement.executeUpdate替换为statement.executeQuery并从ResultSet中检索值。这是显示如何执行此操作的代码片段:

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

答案 2 :(得分:0)

Jop Eggan的答案解决了这个问题。但是,如果该方法出现任何错误,您也可以执行以下操作:

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