Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

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

If I run my main class, I get this 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 '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)

This is the code in my DAO. I think the error is in these lines of code but I don't know which one it is:

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();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are trying to execute two queries in one execution. That is not allowed by JDBC. A single execution should be one and only one statement. Some drivers do allow it, but then you need to enable that with a connection property, for example for MySQL it is allowMultiQueries, see 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.

However I'd strongly suggest to execute as two separate statements (optionally in a transaction). That is JDBC compliant and will work on all databases.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...