【发布时间】:2020-02-14 09:04:56
【问题描述】:
完成教程后,我想创建一个小的select Query。
但是当调用fetch() 时返回以下错误:
org.jooq.exception.DataAccessException: SQL [select `LECTURE_DB`.`dbo`.`STUDENT`.`ID`, `LECTURE_DB`.`dbo`.`STUDENT`.`FIRSTNAME`, `LECTURE_DB`.`dbo`.`STUDENT`.`LASTNAME`, `LECTURE_DB`.`dbo`.`STUDENT`.`YEAR_OF_BIRTH`, `LECTURE_DB`.`dbo`.`STUDENT`.`GENDER` from `LECTURE_DB`.`dbo`.`STUDENT` -- SQL rendered with a free trial version of jOOQ 3.12.1]; Falsche Syntax in der Nähe von '`'.
at org.jooq_3.12.1.MYSQL.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.AbstractResultQuery.fetch(AbstractResultQuery.java:353)
at org.jooq.impl.SelectImpl.fetch(SelectImpl.java:2693)
at de.esteam.lecturedb.jooq.Classes.Startup.main(Startup.java:32)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Falsche Syntax in der Nähe von '`'.
将query 复制到 SQL Server 时,查询也不正确。
我该如何解决这个问题?查询本身是正确的(正确的列)。
代码由 codegen 自动生成。
主要方法:
public static void main(String[] args)
{
// TODO Auto-generated method stub
String userName = "SampleUser";
String password = "SamplePwd";
String url = "jdbc:sqlserver://SampleURL;databaseName=LECTURE_DB";
// Connection is the only JDBC resource that we need
// PreparedStatement and ResultSet are handled by jOOQ, internally
try {
Connection conn = DriverManager.getConnection(url, userName, password);
DSLContext create = DSL.using(conn, SQLDialect.MYSQL);
Result<Record> result = create.select().from(Student.STUDENT).fetch();
for (Record r : result) {
Integer id = r.getValue(Student.STUDENT.ID);
String firstName = r.getValue(Student.STUDENT.FIRSTNAME);
String lastName = r.getValue(Student.STUDENT.LASTNAME);
System.out.println("ID: " + id + " first name: " + firstName + " last name: " + lastName);
}
}
// For the sake of this tutorial, let's keep exception handling simple
catch (Exception e) {
e.printStackTrace();
}
}
【问题讨论】:
-
是 MySQL 还是 SQL Server?如果是 SQL Server,你不应该使用
SQLDialect.MYSQL。
标签: java sql-server jooq