为什么我的sql调用在MySQL中工作,而不在我的Java类中

时间:2015-01-01 18:33:18

标签: java mysql sql jdbc

我在我的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;

My Java SQL Call的语法:

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 Exception,Any Ideas可能会出错。我的第一印象是,无论出于何种原因,Java sql库都不喜欢语法,如果是这样的话,我会找到有关如何解决问题的文档。

1 个答案:

答案 0 :(得分:5)

你的行尾需要一个空格......

+ "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...