跟随this,我尝试创建一个新的学生记录,为其分配一些值并将其存储到数据库中。
DSLContext ctx = ...
StudentRecord s = ctx.newRecord(Student.STUDENT);
s.setFirstname("Nicolas");
s.setLastname("Nox");
s.setGender("M");
s.setYearOfBirth((short) 1990);
s.store(); <-- ERROR
编辑1:insert()
也一样编辑结束1编辑2:学生表仅包含上面显示的四个值。该模型是自动生成的,到目前为止,选择工作正常。编辑结束2
这确实将值插入数据库,但记录未正确更新。引发以下错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: org.jooq.exception.DataAccessException: SQL [declare @result table ([ID] int); insert
into [LECTURE_DB].[dbo].[STUDENT] ([FIRSTNAME], [LASTNAME], [YEAR_OF_BIRTH], [GENDER]) output
[inserted].[ID] into @result values (?, ?, ?, ?); select [r].[ID] from @result [r];]; The statement did not return a result set.
at org.jooq_3.12.1.SQLSERVER2014.debug(Unknown Source)
at org.jooq.impl.Tools.translate(Tools.java:2717)
at org.jooq.impl.DefaultExecuteContext.sqlException(DefaultExecuteContext.java:755)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:383)
at org.jooq.impl.TableRecordImpl.storeInsert0(TableRecordImpl.java:206)
at org.jooq.impl.TableRecordImpl$1.operate(TableRecordImpl.java:177)
at org.jooq.impl.RecordDelegate.operate(RecordDelegate.java:130)
at org.jooq.impl.TableRecordImpl.storeInsert(TableRecordImpl.java:173)
at org.jooq.impl.UpdatableRecordImpl.store0(UpdatableRecordImpl.java:196)
at org.jooq.impl.UpdatableRecordImpl$1.operate(UpdatableRecordImpl.java:136)
at org.jooq.impl.RecordDelegate.operate(RecordDelegate.java:130)
at org.jooq.impl.UpdatableRecordImpl.store(UpdatableRecordImpl.java:132)
at org.jooq.impl.UpdatableRecordImpl.store(UpdatableRecordImpl.java:124)
at de.esteam.lecturedb.jooq.LectureDBSetup.insertInitialData(LectureDBSetup.java:49)
at de.esteam.lecturedb.jooq.LectureDBAnalysis.<init>(LectureDBAnalysis.java:77)
at de.esteam.lecturedb.jooq.LectureDBAnalysis.<clinit>(LectureDBAnalysis.java:44)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:206)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:464)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:405)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7535)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2438)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:208)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:183)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:317)
at org.jooq.tools.jdbc.DefaultPreparedStatement.executeQuery(DefaultPreparedStatement.java:94)
at org.jooq.impl.AbstractDMLQuery.executeReturningQuery(AbstractDMLQuery.java:1137)
at org.jooq.impl.AbstractDMLQuery.execute(AbstractDMLQuery.java:935)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:369)
... 12 more
从数据库中声明缺少的ResultSet,以更新我的应用程序中的ID。
我有点需要记录和ID,因为我需要用外键插入许多值。
jOOQ使用SQL Server OUTPUT
从DML语句中获取结果数据是jOOQ 3.12:#4498的一项新功能。它在3.12.2版中存在一些已知问题,包括:
您的陌生人。了解更多信息后,我将更新我的答案。
一种解决方法是在SQL Server中关闭生成OUTPUT
子句,该子句仍适用于像您这样的单行DML语句。将您的Settings.renderOutputForSQLServerReturningClause
标志设置为Settings.renderOutputForSQLServerReturningClause