0

大家好,当我尝试在数据库中插入某些内容时,我遇到了问题。DB方法是:

public static void insertInEntries(String NrDeOrdine, String NrDocument,String Data, String Emitent, String Continut, String Observatii)
            throws SQLException {
        Statement statement = (Statement) conn.createStatement();
        try {
            statement.executeUpdate("INSERT INTO Intrari " + "VALUES("
                    + NrDeOrdine + "," + NrDocument + "," + Data + ","
                    + Emitent + "," + Continut + "," + Observatii + ");");
            System.out.println("Done");
        } catch (SQLException e) {
            System.err.println("Eroare:" + e);
        }
    }

在 fxml 中,我有一个按钮,该按钮在单击时执行此方法!

@FXML protected void eadaugareIn(ActionEvent event) throws SQLException {
        DBConnection.connect();
        DBConnection.getConnection();
        try
        {
            DBConnection.insertInEntries(enrOrdInput.getText().toString(), enrDocInput.getText().toString(), edataInput.getText().toString(), eemitentInput.getText().toString(), econtinutTextArea.getText().toString(), eobsTextArea.getText().toString());
        }
        catch(SQLException e){
            System.err.println("Eroare:" + e);
        }
    }

因此,如果我在第一个文本字段中输入的文本是“sa”,那么我在该字段中输入的所有内容似乎都会让我出错:

Eroare:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'sa' in 'field list'

我怎么能解决这个问题?非常感谢你!

编辑:几个小时前我找到了答案:

statement.executeUpdate("INSERT INTO `intrari`(`NrDeOrdine`, `NrDocument`, `Data`, `Emitent`, `Continut`, `Observatii`) 
    VALUES("+ '"' + NrDeOrdine + '"' + "," + '"' + NrDocument + '"' + "," + '"' 
    + Data + '"' + "," + '"' + Emitent + '"' + "," + '"' + Continut + '"' + "," 
    + '"' + Observatii + '"' + ");");
4

1 回答 1

0

将日志记录添加到您的insertInEntries方法中:

System.out.println("INSERT INTO Intrari " + "VALUES("
                + NrDeOrdine + "," + NrDocument + "," + Data + ","
                + Emitent + "," + Continut + "," + Observatii + ");");

最有可能查看输出将为您提供解决方案。

于 2012-05-16T14:11:35.860 回答