Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with question mark in jdbc PreparedStatement

I've gone through the internet back and forth and found no solution to my problem. I am trying to use parameter binding with jdbc for querying a mysql table, but it keeps reporting syntax error for the question mark in my statement.

Here is my class:

    package todoList_;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.HashMap;

    public class testBinding {

        // JDBC driver name and database URL
        static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        static final String DB_URL = "jdbc:mysql://localhost/TODO?useJvmCharsetConverters=true";

        // Database credentials
        static final String USER = "root";
        static final String PASS = "root";

        public static void main(String args[]) {
            // TODO Auto-generated method stub
            Connection conn = null;
            Statement stmt = null;
            HashMap<Integer, String> list = new HashMap<Integer, String>();
    try {
        // STEP 2: Register JDBC driver
        Class.forName("com.mysql.jdbc.Driver");

        // STEP 3: Open a connection
        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        // STEP 4: Execute a query
        System.out.println("Creating statement...");
        stmt = conn.createStatement();
        String sql;
        sql = "select * from todos where `id` = ?";
        PreparedStatement preparedStatement1 = conn.prepareStatement(sql);
        preparedStatement1.setInt(1, 0);
        ResultSet rs = preparedStatement1.executeQuery(sql);

        // STEP 5: Extract data from result set
        while (rs.next()) {
            // Retrieve by column name
            list.put(rs.getInt("id"), rs.getString("name"));
        }
        // STEP 6: Clean-up environment
        rs.close();
        stmt.close();
        conn.close();
    } catch (SQLException se) {
        // Handle errors for JDBC
        se.printStackTrace();
    } catch (Exception e) {
        // Handle errors for Class.forName
        e.printStackTrace();
    } finally {
        // finally block used to close resources
        try {
            if (stmt != null)
                stmt.close();
        } catch (SQLException se2) {
        } // nothing we can do
        try {
            if (conn != null)
                conn.close();
        } catch (SQLException se) {
            se.printStackTrace();
        } // end finally try
    } // end try
}
}

And here is my Log:

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:3609)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2618)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
    at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1557)
    at todoList_.testBinding.main(testBinding.java:41)

I am using mysql-connector-java-5.1.18-bin.jar, that I have copied to WebContent/WEB-INF/lib and added to the Build Path of the project. I use Eclipse.

Could you please advise?

like image 471
achillin Avatar asked Feb 08 '23 18:02

achillin


1 Answers

write

ResultSet rs = preparedStatement1.executeQuery();

instead of

ResultSet rs = preparedStatement1.executeQuery(sql);
like image 140
Sanjay Singh Rawat Avatar answered Feb 12 '23 10:02

Sanjay Singh Rawat