how to validate information against the database in java

Solutions on MaxInterview for how to validate information against the database in java by the best coders in the world

showing results for - "how to validate information against the database in java"
Zebulon
05 Aug 2020
1public boolean regFamily(FamilyAccount myFam, Customer myCust) throws Exception {
2    int fid = 0;
3    try {
4
5        String selectStatement2 = "SELECT * from familyok.user where nric = ?  and  familyid is NOT NULL ";
6        PreparedStatement pStmt2 = con.prepareStatement(selectStatement2);
7        pStmt2.setString(1, myCust.getNric());
8        ResultSet rs2 = pStmt2.executeQuery();
9        if (rs2.next()) {
10
11            String insertStatement = "Insert into familyok.familyaccount (familyname, fnric1, fnric2, fnric3)";
12            insertStatement = insertStatement + "values (?,?,?,?)";
13            PreparedStatement prepStmt = con.prepareStatement(insertStatement);
14            prepStmt.setString(1, myFam.getFamilyname());
15            prepStmt.setString(2, myFam.getFnric1());
16            prepStmt.setString(3, myFam.getFnric2());
17            prepStmt.setString(4, myFam.getFnric3());
18            int status = prepStmt.executeUpdate();
19            if (status != 0) {
20
21                String selectStatement = "SELECT fid FROM familyok.familyaccount WHERE fnric1=?";
22                PreparedStatement pStmt = con.prepareStatement(selectStatement);
23                pStmt.setString(1, myFam.getFnric1());
24                ResultSet rs = pStmt.executeQuery();
25                if (rs.next()) {
26                    System.out.println(rs.getInt("fid") + "\t");
27                    fid = rs.getInt("fid");
28
29                    String updateStatement = "update familyok.user set familyid=?, familyname1=? where nric in (?,?,?)";
30                    PreparedStatement preparedStmt = con.prepareStatement(updateStatement);
31                    preparedStmt.setInt(1, fid);
32                    preparedStmt.setString(2, myFam.getFamilyname());
33                    preparedStmt.setString(3, myFam.getFnric1());
34                    preparedStmt.setString(4, myFam.getFnric2());
35                    preparedStmt.setString(5, myFam.getFnric3());
36
37                    int status2 = preparedStmt.executeUpdate();
38                    System.out.println("update=" + preparedStmt.toString());
39
40                    if (status2 != 0) {
41                        System.out.println("Family Account Created");
42                        return true;
43                    }
44                }
45            }
46
47        }
48
49        else
50
51        {
52
53            System.out.println("Can't Register");
54            return false;
55
56        }
57
58    } catch (Exception ex) {
59        throw new Exception("Error: " + ex.getMessage());
60    }
61    return false;
62
63}
64