Skip navigation links

MySQL Forums :: Connector/JDBC and Java :: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException

Advanced Search

Posted by: ravi magod ()
Date: June 03, 2012 10:06PM

I am trying to query with a prepared statement using MySqlDataSource. I am getting MySqlSyntaxError. Some on Help

The Java Code is

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.*;

public class MySqlCxn2 {

//private MysqlDataSource x= new MysqlDataSource();
public static void main(String[] argv) throws Exception {
MysqlDataSource a= new MysqlDataSource();
Connection cxn= (Connection) a.getConnection();
PreparedStatement prepStmt;
ResultSet rs;

try {

String query= "SELECT * FROM billcollector.agentlist a WHERE a.AgentId = ? ;";

prepStmt= cxn.prepareStatement(query);

prepStmt.setInt(1, 1);
rs = prepStmt.executeQuery(query); // Exception thrown here

if ({

} catch (SQLException e) {
// TODO Auto-generated catch block


Exception is as follows:

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 '?' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(
at com.mysql.jdbc.Util.getInstance(
at com.mysql.jdbc.SQLError.createSQLException(
at com.mysql.jdbc.MysqlIO.checkErrorPacket(
at com.mysql.jdbc.MysqlIO.checkErrorPacket(
at com.mysql.jdbc.MysqlIO.sendCommand(
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(
at com.mysql.jdbc.ConnectionImpl.execSQL(
at com.mysql.jdbc.ConnectionImpl.execSQL(
at com.mysql.jdbc.StatementImpl.executeQuery(
at magoddata.MySqlCxn2.main(



Options: ReplyQuote

Subject Written By Posted
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException ravi magod 06/03/2012 10:06PM
Re: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException Todd Farmer 06/03/2012 11:46PM
Re: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException Mark Matthews 06/04/2012 11:29AM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.