Home  >  Q&A  >  body text

Update column data in MySQL table

I'm just learning and don't understand how to update individual columns in a table, I'm using MySQL. The table name is card and I want to update the pincode column. java.sql.SQLSyntaxErrorException: There is an error in your SQL syntax; check the manual for your MySQL server version for the correct syntax to use near line 1 "UPDATE COLUMN pincode = strPincodeNew"

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("\n\n==== 输入新的PIN码 ====\n");
                                    pincodeNew = scanner.nextInt();
                                }
                                String strPincodeNew = String.valueOf(pincodeNew);
                                operation.newPC1(strPincodeNew, cardNumber);
                                System.out.println("PIN码已成功更改");

Rewritten different commands

P粉358281574P粉358281574283 days ago383

reply all(1)I'll reply

  • P粉482108310

    P粉4821083102024-01-11 14:44:59

    Please try the following. If necessary, please correct the card number listing:

    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();
            }
        }

    reply
    0
  • Cancelreply