[Solved] Verification for register does not work java


You are obviously not showing all the pertinent code or you simply have neglected to place the needed code into your method that should make this work.

A ResultSet is basically a collection of results from your query which you need to iterate through to access all results from that query. A while loop is widely used as the means to iterate through all the results contained within a result set object. I see nowhere in your code where you have declared a ResultSet Object but yet you are trying to utilize one. Perhaps try something like this:

String email = email_register_txt.getText();
String username = username_register_txt.getText();

try {
    Connection conn = DriverManager.getConnection("...your jdbc connection string...");
    conn.setAutoCommit(false);

    String sql = "SELECT * FROM user_profile WHERE username = ? OR user_email = ?";
    PreparedStatement stmt = conn.prepareStatement(sql);
    stmt.setString(1, username);
    stmt.setString(2, email);

    ResultSet res = stmt.executeQuery();

    // Utilize a boolean Flag to indicate whether 
    // or not the User is registered.
    boolean registered = false; 
    while (res.next()) {
        if(res.getString("username").equalsIgnoreCase(username)) {
            JOptionPane.showMessageDialog(registerPanel, 
                    "The username has already been already registered!");
            registered = true;
            break; // Get out of the loop. No more need for it.
        }
        else if(res.getString("user_email").equalsIgnoreCase(email)) {
            JOptionPane.showMessageDialog(registerPanel, 
                    "This email address has already been already registered!");
            registered = true;
            break; // Get out of the loop. No more need for it.
        }
    }

    // Do what needs to be done if User is NOT registered.
    if (!registered) {
        ...............................
        ...............................
    }
    res.close();
    stmt.close();
    conn.close(); //Close the DB connection
}
catch (SQLException ex) {
    ex.printStackTrace();
}

You will notice the use of the PreparedStatement Class. There are a number of benefits for using this class:

1) PreparedStatement allows you to write dynamic and parametric queries.

2) PreparedStatement is faster than Statement in Java.

3) PreparedStatement prevents SQL Injection Attacks in Java

Read more about Why to use Prepared Statements in Java here.

1

solved Verification for register does not work java