簡體   English   中英

JDBC PreparedStatement導致MySQL語法錯誤

[英]JDBC PreparedStatement results in MySQL syntax error

我收到錯誤消息“您的SQL語法有錯誤;請檢查與您的MySQL服務器版本相對應的手冊,以在第1行的“ orderr”附近使用正確的語法” ,因此我認為錯誤是用了兩個',但是在我的代碼中我沒有用' 注意,該表實際上被命名為訂購者。

public void insertIntoDatabase(String table, Object... entries) {       // take a table and 
    Connection con = connect();                                         //add entries
    PreparedStatement preparedStatement = null;
    PreparedStatement preparedStatement2 = null;
    ResultSet rs = null;

    StringBuffer columnNames = new StringBuffer();
    StringBuffer sbEntries = new StringBuffer();
    for (int i = 0; i < entries.length; i++) {
        if (entries[i] instanceof Integer)
            sbEntries.append((Integer) entries[i]); 
        else if (entries[i] instanceof String)      
            sbEntries.append((String) entries[i]); 

        if (i != entries.length - 1)//if not last entry add 
            sbEntries.append(" ,"); // a ' ,'.
    }
    try {
        preparedStatement = con.prepareStatement("select * from ? ;");
        preparedStatement.setString(1, table);
        preparedStatement2 = con
                .prepareStatement("Insert into ?( ? ) values ( ? );");
        ResultSet resultSet = preparedStatement.executeQuery(); // get the
                                                                // number of
                                                                // columns
        ResultSetMetaData rsmd; // for the table
        rsmd = resultSet.getMetaData();
        int columnCount = rsmd.getColumnCount();
        for (int i = 1; i < columnCount + 1; i++) { // get column names, add to 
            columnNames.append(rsmd.getColumnName(i)); // to sb
            if (i != columnCount)
                columnNames.append(" ,");
        }
        columnCount = rsmd.getColumnCount();
        preparedStatement2.setString(1, table);                     
        preparedStatement2.setString(2, columnNames.toString());    //add sb's to statement
        preparedStatement2.setString(3, sbEntries.toString());
        preparedStatement2.executeUpdate();

    } catch (SQLException e) {
        System.out.println("2" + e.getMessage());
    }
    finally{
        try {
            if (rs != null) {
                rs.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if(preparedStatement2 != null){
                preparedStatement2.close();
            }
            if (con != null) {
                con.close();
            }

        } catch (SQLException e) {
            System.out.print("3" +e.getMessage());
        }

    }

}

在大多數數據庫中,您無法參數化表名稱之類的對象名稱,在MySQL中,理論上您可以做到,因為默認情況下MySQL Connector / J不使用服務器端參數,而是在將查詢發送到服務器之前重寫查詢。 但是,該值將作為帶引號的字符串插入,並且對象名稱不能是帶引號的字符串,因此仍然無法使用。

如此INSERT INTO ? 還是SELECT ... FROM ? 將不起作用,因為它會生成INSERT INTO 'theTable'SELECT ... FROM 'theTable'

對象名稱必須是實際查詢的一部分。 不要為它們使用參數。 大多數其他數據庫(或其驅動程序)在此位置具有參數都會引發異常。

您的代碼中有兩個錯誤。

  1. 您無法使用預處理語句在SQL語句中設置表名。
  2. 你有; 在SQL語句中。

處理完以上幾點后,您的代碼應該可以使用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM