Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Avoid Sql Duplication jdbc

im trying to simply avoid sql duplication.i found this code here how to prevent duplication. Java,SQL which is successful. I know rs.nextwill move cursor but how it helps avoid duplication(dose it compare every value).what is dose 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.");   
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading