2015-10-20 40 views
2

我已經通過互聯網來回,發現我的問題沒有解決方案。 我想用jdbc查詢一個mysql表的參數綁定,但它在我的語句中一直保持報告語法錯誤的問號。問題在jdbc中出現問號PreparedStatement

這裏是我的類:

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連接器的Java-5.1.18-bin.jar ,我已經複製到WebContent/WEB-INF/lib並添加到項目的構建路徑中。 我使用Eclipse。

您能否提供建議?

在此先感謝。

+2

刪除'preparedStatement1.executeQuery(SQL)的參數;'因爲你已經準備語句一行上面一行檢查HTTP://計算器.com/a/29912419/3841803 – silentprogrammer

+0

@singhakash完美,謝謝! :) – achillin

回答

2

ResultSet rs = preparedStatement1.executeQuery(); 

,而不是

ResultSet rs = preparedStatement1.executeQuery(sql);