带有SubQuery引发错误的准备好的语句

时间:2018-09-02 08:51:05

标签: java mysql jdbc prepared-statement

我有2个表EmployeeDepartment

DepartmentEmployee一对多关系。

dep_idEmployee表中代表部门ID的外键列。

查询用例:从属于{<1> DEVELOPMENT ”部门的Employee表中获取所有员工。

以下SQL语句对我有用:

String sqlRead = "SELECT * FROM EMPLOYEE AS e 
                  WHERE e.dep_id = (SELECT dep_id 
                                    FROM DEPARTMENT AS d 
                                    WHERE d.NAME = 'DEVELOPMENT')";

我想为它创建一个PreparedStatement。当我运行以下查询时,出现SQL语法错误:

String sqlRead = "SELECT * FROM EMPLOYEE AS e 
                  WHERE e.dep_id = (SELECT dep_id 
                                    FROM DEPARTMENT AS d 
                                    WHERE d.NAME = ?)";

PreparedStatement prepareStatement = conn.prepareStatement(sqlRead);    
prepareStatement.setString(1, "DEVELOPMENT");
ResultSet readResultSet = prepareStatement.executeQuery();

错误:

  

java.sql.SQLSyntaxErrorException:您的SQL错误   句法;检查与您的MySQL服务器版本相对应的手册   在第1行的'?)'附近使用正确的语法   com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:118)     在   com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)     在   com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)     在   com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1247)     在   com.tutorialspoint.eclipselink.service.PlainJDBC.main(PlainJDBC.java:44)

您能帮助我了解我的查询出了什么问题吗

0 个答案:

没有答案