check if value (Accountnumber) exist in a java dat

2019-08-02 20:57发布

问题:

I have the following method in a class called savings, am using JDBC database to save and view data on java application.

I have SAVINGS table in my database ZaiLab with the following fields,

ID,ACCOUNTNUMBER,CUSTOMERNAME,BALANCE,MINMUM)

and the following VALUEs will be entered by the user using JOPtionPane.

(id,accountNumber,customername,balance,minmum);

the application should then check if accountNumber entered by the user already exist, if not it should save the record to a table SAVINGS, if yes it should display the appropriate message. "Account already exist".

public void openSavingsAccount(int Id, int Amount) {

    try {
        String host = "jdbc:derby://localhost:1527/ZaiLab";
        String uname = "siduduzo";
        String upass = "Password01";
        Connection con = DriverManager.getConnection(host, uname, upass);

        Statement stmt = con.createStatement();

        String SQL = "SELECT * FROM SAVINGS";
        ResultSet rs = stmt.executeQuery(SQL);

        int minmum = 1000;
        balance = minmum;

        while (rs.next()) {
            int acc_col = rs.getInt("ACCOUNTNUMBER");
            if (acc_col == accountNumber) {
                JOptionPane.showMessageDialog(null, "Sorry, account " + accountNumber
                        + " aready Exist");
            } else if (Amount < minmum) {
                JOptionPane.showMessageDialog(null, "Can not Open the Account, Minimum amount to deposit must be R1000");
            } else {

                balance = balance + Amount;
                id = Id;
                stmt.executeUpdate("INSERT INTO `SAVINGS`(ID,ACCOUNTNUMBER,CUSTOMERNAME,BALANCE,MINMUM)VALUE ('" + id + "','" + accountNumber + "','" + customername + "'," + balance + ",'" + minmum + "')");

            }

        }

    } catch (SQLException err) {
        System.out.println(err.getMessage());
    }

}

回答1:

Right now you are selecting all rows from SAVINGS and attempt to insert a new account for each row that doesn't have the 'new' account number.

Instead, you should select only the row with the new account number, and insert when it doesn't exist.

You should also use prepared statements to protect you against SQL injection.

For example:

try (PreparedStatement checkAccountExists = con.prepareStatement(
        "SELECT 1 FROM SAVINGS WHERE ACCOUNTNUMBER = ?")) {
    checkAccountExists.setInt(1, accountNumber);

    try (ResultSet rs = checkAccountExists.executeQuery()) {
        if (rs.next()) {
            // handle account already exists
        } else {
            try (PreparedStatement insert = con.prepareStatement(
                    "INSERT INTO SAVINGS(ID, ACCOUNTNUMBER, CUSTOMERNAME, BALANCE, MINMUM) VALUES (?, ?, ?, ? , ?)")) {
                insert.setInt(1, id);
                insert.setInt(2, accountNumber);
                insert.setString(3, customername);
                insert.setInt(4, balance);
                insert.setInt(5, minmum);

                insert.executeUpdate();
            }
        }
    }
}

Alternatively, you could define a unique constraint on ACCOUNTNUMBER in your database and just do the insert and handle the constraint violation if the record already exists.



标签: java sql jdbc