gpt4 book ai didi

java - 为什么我的 sql 调用在 MySQL 中有效,但在我的 Java 类中无效

转载 作者:行者123 更新时间:2023-12-01 00:42:49 26 4
gpt4 key购买 nike

我在我的 MySQL 工作台中有一个 SQL 调用来测试,以便我可以确保语法正确,我仔细检查并且它有效,但是当我在我的 java 类中使用相同的语法将它传递给数据库并且获取信息我得到这样的 SQLException

Error: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in
your SQL syntax; check the manual that corresponds to your MySQL server version for the
right syntax to use near 'JOIN customers on customers.ANum = leads.ANum JOIN automobiles
on automobiles.AN' at line 1

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version for the right
syntax to use near 'JOIN customers on customers.ANum = leads.ANum JOIN automobiles on
automobiles.AN' at line 1

这是我在 MySQL 中的语法:

SELECT leads.Anum, leads.recDate, leads.status, leads.salesman, leads.apptDt, 
leads.appTime, leads.notes,customers.fName, customers.mName, customers.lName,
customers.phone, customers.cell, customers.work, customers.bestTime, customers.email,
customers.addr1, customers.addr2, customers.city, customers.state, customers.zip,
customers.rentOwn, customers.pmtMo, customers.livedMo, customers.livedYr,
customers.ssn, customers.birthday, customers.employer, customers.incomeMo,
customers.empMo, customers.empYr, customers.creditApp, automobiles.newUsed,
automobiles.extColorWant, automobiles.intColorWant, automobiles.desiredInfo,
automobiles.extColor, automobiles.intColot, automobiles.yearHave, automobiles.make,
automobiles.model, automobiles.mileage, automobiles.goodPoor FROM leads JOIN customers
on customers.ANum = leads.ANum JOIN automobiles on automobiles.ANum = customers.ANum
where leads.dealer = 'SKA' and leads.recDate between '2013-01-01' and '2014-12-31'
order by leads.recDate DESC;

我的 Java SQL 调用的语法:

String sql = "SELECT leads.Anum, leads.recDate, leads.status, leads.salesman, leads.apptDt, leads.appTime, leads.notes,"
+ "customers.fName, customers.mName, customers.lName, customers.phone, customers.cell, customers.work,"
+ "customers.bestTime, customers.email, customers.addr1, customers.addr2, customers.city, customers.state,"
+ "customers.zip, customers.rentOwn, customers.pmtMo, customers.livedMo, customers.livedYr, customers.ssn,"
+ "customers.birthday, customers.employer, customers.incomeMo, customers.empMo, customers.empYr, customers.creditApp,"
+ "automobiles.newUsed, automobiles.extColorWant, automobiles.intColorWant, automobiles.desiredInfo, automobiles.extColor, automobiles.intColot,"
+ "automobiles.yearHave, automobiles.make, automobiles.model, automobiles.mileage, automobiles.goodPoor"
+ "FROM leads JOIN customers on customers.ANum = leads.ANum JOIN automobiles on automobiles.ANum = customers.ANum Where leads.dealer = "
+ "? and leads.recDate between ? and ? order by leads.recDate DESC";

正如我之前所说,MySQL 中的 SQL 调用工作得很好,返回的正是我想要它返回的内容,但是我在我的 Java Web 应用程序中遇到了 SQL 异常,任何想法这里可能出了什么问题。我的第一印象是,无论出于何种原因,Java sql 库都不喜欢这种语法,如果是这种情况,我在哪里可以找到有关如何解决该问题的文档。

最佳答案

行尾需要一个空格...

+ "automobiles.yearHave, automobiles.make, automobiles.model, automobiles.mileage, automobiles.goodPoor" <-- Space here
+ "FROM leads JOIN customers on customers.ANum = leads.ANum JOIN automobiles on automobiles.ANum = customers.ANum Where leads.dealer = "

这导致 FROM 子句和 SELECT 子句中的字段一起运行...

...automobiles.mileage, automobiles.goodPoorFROM leads...

关于java - 为什么我的 sql 调用在 MySQL 中有效,但在我的 Java 类中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27733479/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com