how to validate information against the database in java

public boolean regFamily(FamilyAccount myFam, Customer myCust) throws Exception {
    int fid = 0;
    try {

        String selectStatement2 = "SELECT * from familyok.user where nric = ?  and  familyid is NOT NULL ";
        PreparedStatement pStmt2 = con.prepareStatement(selectStatement2);
        pStmt2.setString(1, myCust.getNric());
        ResultSet rs2 = pStmt2.executeQuery();
        if (rs2.next()) {

            String insertStatement = "Insert into familyok.familyaccount (familyname, fnric1, fnric2, fnric3)";
            insertStatement = insertStatement + "values (?,?,?,?)";
            PreparedStatement prepStmt = con.prepareStatement(insertStatement);
            prepStmt.setString(1, myFam.getFamilyname());
            prepStmt.setString(2, myFam.getFnric1());
            prepStmt.setString(3, myFam.getFnric2());
            prepStmt.setString(4, myFam.getFnric3());
            int status = prepStmt.executeUpdate();
            if (status != 0) {

                String selectStatement = "SELECT fid FROM familyok.familyaccount WHERE fnric1=?";
                PreparedStatement pStmt = con.prepareStatement(selectStatement);
                pStmt.setString(1, myFam.getFnric1());
                ResultSet rs = pStmt.executeQuery();
                if (rs.next()) {
                    System.out.println(rs.getInt("fid") + "\t");
                    fid = rs.getInt("fid");

                    String updateStatement = "update familyok.user set familyid=?, familyname1=? where nric in (?,?,?)";
                    PreparedStatement preparedStmt = con.prepareStatement(updateStatement);
                    preparedStmt.setInt(1, fid);
                    preparedStmt.setString(2, myFam.getFamilyname());
                    preparedStmt.setString(3, myFam.getFnric1());
                    preparedStmt.setString(4, myFam.getFnric2());
                    preparedStmt.setString(5, myFam.getFnric3());

                    int status2 = preparedStmt.executeUpdate();
                    System.out.println("update=" + preparedStmt.toString());

                    if (status2 != 0) {
                        System.out.println("Family Account Created");
                        return true;
                    }
                }
            }

        }

        else

        {

            System.out.println("Can't Register");
            return false;

        }

    } catch (Exception ex) {
        throw new Exception("Error: " + ex.getMessage());
    }
    return false;

}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source