com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
Posted by: ravi magod
Date: June 03, 2012 10:06PM

Hi
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();
a.setServerName("localhost");
a.setDatabaseName("BillCollector");
a.setPassword("kimbu403");
a.setUser("root");
System.out.println(a.getURL());
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 (rs.next()){
System.out.println(rs.getString("Name"));



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

}

Exception is as follows:

jdbc:mysql://localhost:3306/BillCollector
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(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2490)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2651)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2677)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2627)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1556)
at magoddata.MySqlCxn2.main(MySqlCxn2.java:31)

Regards

RaviMagod

Options: ReplyQuote


Subject
Written By
Posted
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
June 03, 2012 10:06PM


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.