4

我来回浏览了互联网,但没有找到解决问题的方法。我正在尝试使用与 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 并添加到项目的构建路径中。我使用 Eclipse。

您能否提一些建议?

提前致谢。

4

1 回答 1

8

ResultSet rs = preparedStatement1.executeQuery();

代替

ResultSet rs = preparedStatement1.executeQuery(sql);
于 2015-10-20T08:25:09.427 回答