skip to Main Content

I’m just learning and I don’t understand how to update a separate column in the table, I use MySQL. The table is called card, I want to update the pincode column.
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘UPDATE COLUMN pincode = strPincodeNew’ at line 1

 public void newPC1(String strPincodeNew, String cardNumber) {
        try {
            Connection c = Database.connection();

            Statement stmt11 = c.createStatement();


            String sql12="ALTER TABLE card UPDATE COLUMN pincode = strPincodeNew";

            stmt11.executeUpdate(sql12);


while (pincodeNew>=....) {
                                    System.out.println("nn==== Enter a new PIN code ====n");
                                    pincodeNew = scanner.nextInt();
                                }
                                String strPincodeNew = String.valueOf(pincodeNew);
                                operation.newPC1(strPincodeNew, cardNumber);
                                System.out.println("PIN code successfully changed");

Rewrote different commands

2

Answers


  1. What you want is to change a column in a row. So you need to select both.

    Likely you want to do something like

    UPDATE card SET pincode = '1234' WHERE card_id = 'abcdef';
    

    You need to have one row (via an INSERT statement) in the table, before you can update something.

    Login or Signup to reply.
  2. Try the following. You need to correct the card number column name where necessary:

        public void newPC1(String strPincodeNew, String cardNumber) throws SQLException {
            try (Connection c = Database.connection();
                PreparedStatement ps = c.prepareStatement("UPDATE card SET pincode = ? WHERE card_number = ?")) {
                ps.setString(1, strPincodeNew);
                ps.setString(2, cardNumber);
                ps.executeUpdate();
            }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search