首页 > 解决方案 > 提示时数据库表不更新

问题描述

这是我一直想问的另一个问题,关于我在上一个问题中问过的同一个“卡坦定居者”数据库。这一次是关于更新数据库表本身。

我成功地将 Java 应用程序连接到 Oracle 数据库,而其他数据库表在应用程序提示时成功更新,但由于某种原因,这个表尤其没有更新。它也没有给出错误,所以我排除它可能是连接或 SQL 命令问题。

这是我要更新的表:

CREATE TABLE giocatore(
    numero INT,
    colore VARCHAR2(20),
    punto_partenza INT,
    punti_vittoria INT DEFAULT 0,
    vittorioso CHAR(1) DEFAULT 'f',
    quant_clay INT,
    quant_wool INT,
    quant_wheat INT,
    quant_wood INT,
    quant_rock INT,
    CONSTRAINT player_pk PRIMARY KEY(numero)
    );

这是有问题的 Java 应用程序代码:

private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        try{
            String url="jdbc:oracle:thin:@//localhost:1521/orcl";
            OracleDataSource ods = new OracleDataSource();
            ods.setURL(url);
            ods.setUser("LAB_NICK");
            ods.setPassword("10Niroxxe98");
            Connection conn = ods.getConnection();
            String PlayerNumberAsString = playerNum.getSelectedItem().toString();
            int PlayerNumber = Integer.parseInt(PlayerNumberAsString);
            String StartingHexagonNumberAsString = startHex.getSelectedItem().toString();
            int StartingHexagonNumber = Integer.parseInt(StartingHexagonNumberAsString);
            int VictoryPoints = (int) victoryPts.getValue();
            int OwnedAmountOfClay = (int) clayNum.getValue();
            int OwnedAmountOfWood = (int) woodNum.getValue();
            int OwnedAmountOfMinerals = (int) rockNum.getValue();
            int OwnedAmountOfWheat = (int) wheatNum.getValue();
            int OwnedAmountOfWool = (int) woolNum.getValue();
            String PlayerColor = colorChoice.getSelectedItem().toString();
            String IsVictorious;
            if(victoryCheck.isSelected()){
                IsVictorious = "v";
            }else{
                IsVictorious = "f";
            }
            PreparedStatement stmt = conn.prepareStatement("UPDATE giocatore SET punti_vittoria = ?, colore = ?, vittorioso = ?, punto_partenza = ?, quant_clay = ?, quant_wool = ?, quant_wheat = ?, quant_wood = ?, quant_rock = ? WHERE numero = ?");
            stmt.setInt(1, VictoryPoints);
            stmt.setString(2, PlayerColor);
            stmt.setString(3, IsVictorious);
            stmt.setInt(4, StartingHexagonNumber);
            stmt.setInt(5, OwnedAmountOfClay);
            stmt.setInt(6, OwnedAmountOfWool);
            stmt.setInt(7, OwnedAmountOfWheat);
            stmt.setInt(8, OwnedAmountOfWood);
            stmt.setInt(9, OwnedAmountOfMinerals);
            stmt.setInt(10, PlayerNumber);
            stmt.close();
            conn.close();
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }

我也想知道如何刷新 JTable,但这是另一个问题。感谢您的理解。

编辑:我之前发布的代码完全可以工作。这是我实际询问的代码。对于给您带来的重大不便,我深表歉意。

标签: javajdbc

解决方案


推荐阅读