dilbro
dilbro

Reputation: 45

Avoid SQL duplication in JDBC

Im trying to avoid SQL duplication. I found this code here how to prevent duplication. Java,SQL which is successful. I know rs.next will move the cursor, but how does it help avoid duplication (does it compare every value)? What is done is just checking is there another row and if there return true right?

Connection conn = // Connect to the database... 
PreparedStatement ps = 
    conn.prepareStatement("SELECT username FROM login where username = ?";
ps.setString(1, value1);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
    JOptionPane.showMessageDialog
        (null,"username is already existed! please create new username.");   
}

Upvotes: 0

Views: 165

Answers (1)

Renis1235
Renis1235

Reputation: 4710

Your code is quite straightforward. You make a query that fetches ALL rows of the table login where username = XXX. Then you check whether there is any data in the ResultSet by executing the rs.next() function. This function returns a boolean value that is true when there is yet more data in the rs and false when there is no more data.

As you said, it also moves the cursor.

Does it compare every value(row)?

Yes. Your query looks every row up and checks whether username = XXX

Upvotes: 1

Related Questions