jdbc中的问号问题PreparedStatement

时间:2015-10-20 07:50:33

标签: java mysql jdbc prepared-statement parameterbinding

我来回浏览互联网,发现我的问题无法解决。 我试图使用参数绑定与jdbc查询mysql表,但它在我的语句中不断报告问号的语法错误。

这是我的班级:

    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
}

}

这是我的日志:

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)

我正在使用mysql-connector-java-5.1.18-bin.jar,我已将其复制到WebContent / WEB-INF / lib并添加到项目的Build Path中。 我使用Eclipse。

你能告诉我吗?

提前致谢。

1 个答案:

答案 0 :(得分:5)

ResultSet rs = preparedStatement1.executeQuery();

而不是

ResultSet rs = preparedStatement1.executeQuery(sql);