java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : error in SQL syntax

标签 java sql jsp jdbc

如果我运行我的主类,我会收到此错误:

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 'insert into customerLogin(companyName,username,password) values('Big Company','D' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4187)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4119)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2815)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2155)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2458)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2375)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2359)
    at dao.CustomerContactDAO.performUpdate(CustomerContactDAO.java:175)
    at dao.DAOTemplate.executeUpdate(DAOTemplate.java:27)
    at dao.CustomerContactDAO.addCustomerContact(CustomerContactDAO.java:88)
    at test.Test.main(Test.java:29)

这是我的 DAO 中的代码。我认为错误出在这些代码行中,但我不知道它是哪一行:

public boolean addCustomerContact(CustomerContact c) {
    setQuery("insert into customerContact(companyName,lastName,firstName,email,telNum,local,cellNum) values(?,?,?,?,?,?,?);"
            + "insert into customerLogin(companyName,username,password) values(?,?,?)");
    queries.add(getQuery());

    KeyValuePair onePair;

    onePair = new KeyValuePair();
    onePair.setKey(KeyValuePair.STRING);
    onePair.setValue(c.getCompanyName());
    getParameters().add(onePair);

    onePair = new KeyValuePair();
    onePair.setKey(KeyValuePair.STRING);
    onePair.setValue(c.getLastName());
    getParameters().add(onePair);

    onePair = new KeyValuePair();
    onePair.setKey(KeyValuePair.STRING);
    onePair.setValue(c.getFirstName());
    getParameters().add(onePair);

    onePair = new KeyValuePair();
    onePair.setKey(KeyValuePair.STRING);
    onePair.setValue(c.getEmail());
    getParameters().add(onePair);

    onePair = new KeyValuePair();
    onePair.setKey(KeyValuePair.INT);
    onePair.setValue(c.getTelNum() + "");
    getParameters().add(onePair);

    onePair = new KeyValuePair();
    onePair.setKey(KeyValuePair.INT);
    onePair.setValue(c.getLocal() + "");
    getParameters().add(onePair);

    onePair = new KeyValuePair();
    onePair.setKey(KeyValuePair.STRING);
    onePair.setValue(c.getCellNum() + "");
    getParameters().add(onePair);

    onePair = new KeyValuePair();
    onePair.setKey(KeyValuePair.STRING);
    onePair.setValue(c.getCompanyName());
    getParameters().add(onePair);

    onePair = new KeyValuePair();
    onePair.setKey(KeyValuePair.STRING);
    onePair.setValue(c.getUsername());
    getParameters().add(onePair);

    onePair = new KeyValuePair();
    onePair.setKey(KeyValuePair.STRING);
    onePair.setValue(c.getPassword());
    getParameters().add(onePair);
    return executeUpdate();
}

最佳答案

您正尝试在一次执行中执行两个查询。这是 JDBC 不允许的。一次执行应该是一个且只有一个语句。有些驱动程序确实允许它,但您需要使用连接属性来启用它,例如对于 MySQL,它是 allowMultiQueries,请参阅 http://dev.mysql.com/doc/refman/5.6/en/connector-j-reference-configuration-properties.html :

allowMultiQueries
Allow the use of ';' to delimit multiple queries during one statement (true/false), defaults to 'false', and does not affect the addBatch() and executeBatch() methods, which instead rely on rewriteBatchStatements.

但是我强烈建议作为两个单独的语句执行(可以选择在事务中)。这符合 JDBC,并且适用于所有数据库。

关于java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : error in SQL syntax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59334068/

相关文章:

java - 在执行条件的 else 部分之前完全迭代 Java 数组

java - 如何使用 HornetQ 延迟 JMS 消息的传递?

java - 如何在java中创建自定义数据类型?

mysql - Oracle和MySQL的代码形式区别

sql - NHibernate "SELECT ... FROM (SELECT ..."标准

mysql - 使用 count(*) 和 inner join 时查询速度慢

java - Android 中通过向 url 传递参数来检索数据

java - ${pageContext.request.contextPath} 和 request.getContextPath() 有什么区别

spring - 如何从 Controller 告诉 View 发生了错误? (Spring MVC)

jsp - 创建一个Excel文件供用户使用Apache POI下载