INSERT INTO语句中的MS Access语法错误

时间:2014-03-23 18:36:42

标签: java sql ms-access netbeans insert-into

我在INSERT INTO语句"中有一个"语法错误。这是一个复制代码(代码在我使用GUI构建器FYI创建的JFrame中):

private void jButtonSaveActionPerformed(java.awt.event.ActionEvent evt) {                                            
    String InputTeam1, InputTeam2;
    Integer InputTeam1Goals, InputTeam2Goals;

    FixtureNumber = Integer.parseInt(jLabel3.getText());

    InputTeam1 = jComboBox1.getSelectedItem() + "";
    InputTeam2 = jComboBox2.getSelectedItem() + "";
    InputTeam1Goals = Integer.parseInt(jTextField2.getText());
    InputTeam2Goals = Integer.parseInt(jTextField3.getText());

    try
    {
        String filename = "Database.mdb";
        String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
        database += filename.trim() + ";DriverID=22;READONLY=false";
        conn = DriverManager.getConnection(database, "", "");

        Statement sta = conn.createStatement();
      //Line below is the INSERT statement
        int s = sta.executeUpdate("INSERT INTO tblLeagueFixtures VALUES '" + InputTeam1 + "', '" + InputTeam2 + "', '" + InputTeam1Goals + "', '" + InputTeam2Goals + "' WHERE FixtureNumber = '"+ FixtureNumber +"'");
        System.out.println("Inserted into database.");
    }
    catch (Exception e3)
    {
        System.out.println("Exception ReadPlayerStats:" + e3);
    }
}

代码的目的是从用户获取数据(Team1等),然后将其插入数据库。我尝试过更改撇号,将VALERE放在VALUES之前,但结果是一样的。无论是插入新数据还是更新现有数据,错误仍然存​​在。

[解决]

int s = sta.executeUpdate("UPDATE tblLeagueFixtures SET [Team1] = '"+ InputTeam1 +"', [Team2] = '"+ InputTeam2 +"', [Team1Goals] = "+ InputTeam1Goals +", [Team2Goals] = "+ InputTeam2Goals +" WHERE [FixtureNumber] = "+ FixtureNumber);

1 个答案:

答案 0 :(得分:2)

INSERT命令中至少有2个问题。

1)您无法在WHERE声明中使用INSERT INTO VALUES子句。

2)在INSERT INTO VALUES语句中,值必须出现在括号中。

int s = sta.executeUpdate("INSERT INTO tblLeagueFixtures VALUES ('" + InputTeam1 + "', '" + InputTeam2 + "', '" + InputTeam1Goals + "', '" + InputTeam2Goals + "')");

要更新数据,您必须使用正确的语法。有关更多信息,请参阅this