首页 > 解决方案 > MySQL /Java 帮助 - 通过 eclipse 更新方法

问题描述

我正在为我的训练营做一项任务,这一点让我很困惑,任务的细节如下。

任务:

创建一个适用于 mySQL 的 java 程序。该程序应向用户显示以下菜单:

  1. 进入玩具
  2. 更新玩具
  3. 删除玩具
  4. 搜索玩具
  5. 出口

我已经创建了整个程序并使用我设置的数据库对其进行了测试,并且所有方面及其方法都可以正常工作 - 除了更新玩具方法。我已经放入了数据库的图片以供参考和我拥有的更新方法,这是关于第 5 次返工,我知道这是错误的,但我尝试过的没有任何效果。任何关于我哪里出错的建议或解释都会有所帮助——如果你能指出我正确的方向,那就太好了。

数据库参考图片

static void updateMethod() {
    // the connection
            String url = "jdbc:mysql://localhost:3306/toyshop";
            String username = "root";
            String password = "SlytherinPr1de";
        
        try {
            Connection conUpdate = DriverManager.getConnection(url,username,password);
            System.out.println("Connected to Database - start with record update:");
            
            //set up method
            //get the id of the item to update so it can be found in the table and updated:
            Scanner updateRow = new Scanner(System.in);
            System.out.println("Please enter the id of the item you wish to update: ");
            String toyToUpdate = updateRow.nextLine();
            
            Scanner update1 = new Scanner(System.in);
            System.out.println("Please enter the new toy name: ");
            String nameUpdate1 = update1.nextLine();
            
            Scanner update2 = new Scanner(System.in);
            System.out.println("Please enter the new company: ");
            String compUpdate2 = update2.nextLine();
            
            Scanner update3 = new Scanner(System.in);
            System.out.println("Please enter the new quantity: ");
            String qtyUpdate3 = update3.nextLine();
            
            String sql = "UPDATE toys SET name=" + nameUpdate1 + ",company=" + compUpdate2 + ",Qty=" + qtyUpdate3 + ",WHERE id =" + toyToUpdate;
            
            
            PreparedStatement statement = conUpdate.prepareStatement(sql);
            
                
            int rowsUpdated = sql;
                            
                if(rowsUpdated > 0) {
                    System.out.println("The existing record for toy ID " + toyToUpdate + " has been successfully updated.");
                    }
                
                updateRow.close();
                update1.close();
                update2.close();
                update3.close();
        }catch(SQLException e) {
            System.out.println("Method Error - check code and retry.");
            e.printStackTrace();
        }
}

标签: javamysqljdbc

解决方案


您的代码的第一个问题是使用 4 个Scanner对象来检索输入数据。
你只需要 1 Scanner

第二个问题是 SQL 语句的语法。
需要逗号来分隔要更新的列。
字符串参数值也必须用单引号括起来,但这不是安全且推荐的方式。
由于您使用的是 aPreparedStatement您还应该?为每个字符串参数设置占位符并使用方法传递它们的值setString()

static void updateMethod() {
    // the connection
    String url = "jdbc:mysql://localhost:3306/toyshop";
    String username = "root";
    String password = "SlytherinPr1de";

    try {
        Connection conUpdate = DriverManager.getConnection(url, username, password);
        System.out.println("Connected to Database - start with record update:");

        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the id of the item you wish to update: ");
        String toyToUpdate = scanner.nextLine();
        System.out.println("Please enter the new toy name: ");
        String name = scanner.nextLine();
        System.out.println("Please enter the new company: ");
        String company = scanner.nextLine();
        System.out.println("Please enter the new quantity: ");
        String qty = scanner.nextLine();
        scanner.close();

        String sql = "UPDATE toys SET name = ?, company = ?, Qty = ? WHERE id = ?";
        PreparedStatement statement = conUpdate.prepareStatement(sql);
        
        statement.setString(1, name);
        statement.setString(2, company);
        statement.setString(3, qty);
        statement.setString(4, toyToUpdate);

        int rowsUpdated = statement.executeUpdate();

        if(rowsUpdated > 0) {
            System.out.println("The existing record for toy ID " + toyToUpdate + " has been successfully updated.");
        }

        statement.close();
        conUpdate.close();
    }catch(SQLException e) {
        System.out.println("Method Error - check code and retry.");
        e.printStackTrace();
    }
}

推荐阅读