为什么我的 sql 呼叫在 MySQL 中有效,但在我的 Java class 中无效
Why is my sql call working in MySQL but not in my Java class
我在 MySQL workbench 中有一个 SQL 调用来测试,以便我可以确保语法正确,我仔细检查并且它有效,但是当我使用在我的 java class 中使用相同的语法将其传递给数据库并获取信息 我得到一个 SQL 像这样的异常
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";
正如我之前所说 SQL 在 MySQL 中调用工作正常,returns 正是我想要它返回的内容,但我得到了 SQL 异常我的 java 网络应用程序,任何想法这里可能出了什么问题。我的第一印象是 Java sql 库出于某种原因不喜欢这种语法,如果是这种情况,我在哪里可以找到有关如何解决该问题的文档。
你的行尾需要一个 space...
+ "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...
我在 MySQL workbench 中有一个 SQL 调用来测试,以便我可以确保语法正确,我仔细检查并且它有效,但是当我使用在我的 java class 中使用相同的语法将其传递给数据库并获取信息 我得到一个 SQL 像这样的异常
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";
正如我之前所说 SQL 在 MySQL 中调用工作正常,returns 正是我想要它返回的内容,但我得到了 SQL 异常我的 java 网络应用程序,任何想法这里可能出了什么问题。我的第一印象是 Java sql 库出于某种原因不喜欢这种语法,如果是这种情况,我在哪里可以找到有关如何解决该问题的文档。
你的行尾需要一个 space...
+ "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...