how to know when a select finds 0 rows mysql, getFetchSize() not working

August 20, 2017, at 2:13 PM

I am trying to make a username only register if that name is not taken, using JDBC connection and checking on SQL Database. I have the code that checks for the SELECT * FROM user WHERE username = 'jessica';

and it finds 2 rows;

Searched a lot and found that with getFetchSize() it would give me the number of rows, and if it finds null it would return 0. It is always returning 0, I don't know why, because I have the usernames taken twice, it lets me add me always...

 public int nameAvailable(MyUserApp app, String name) throws SQLException{
    String sql = "SELECT * FROM user \n WHERE username = '"+ name +"';";
    Statement st = app.getCon().createStatement();
    ResultSet rs =  st.executeQuery(sql);
    int numResults = rs.getFetchSize();
    return numResults;

This is the register code:

private void RegisterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                               
    String username, password, address, dob;
    boolean status;
    String u;
    try {
        username = newUsernameField.getText();
        password = passwordField2.getText();
        address = addressField.getText();
        dob = dateofbField.getText();
        int no= 5;
        if( username.isEmpty() || password.isEmpty() || password.length() < 6 ){
            jLabel6.setText("The information you typed in is not valid. ");
            status = false;
                no =, username);
                if(no == 0){
                    jLabel6.setText("Registered your account, "+username+"!" + no);
                    status =, username, password, dob, address);
                    u =, username);
                    no =, username);
                    showTableDB.setText("That username is token. Please choose a different one.");


    } catch (SQLException ex) {
        Logger.getLogger(UserAppUI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InterruptedException ex) {
        Logger.getLogger(UserAppUI.class.getName()).log(Level.SEVERE, null, ex);
Answer 1

The fetch size is not the same thing as the number of rows. The fetch size is just a way of limiting how many rows at a time will be fetched from the database.

There's no easy way to check the number of rows returned by a select statement. If you really need to know how many rows there are, in the case there's more than one, then one approach would be to iterate through the result set, copying the information that you need from each row into memory; then check the amount of data that you copied at the end.

Alternatively, if you don't actually need any data from the rows themselves, you could try a statement like SELECT count(*) FROM user WHERE username = ?.

One more thing - you need to read about SQL injection attacks. This is where a hacker uses your code to run SQL that they shouldn't. The code you've shown here is vulnerable to an SQL injection attack. But that's another question entirely.

Rent Charter Buses Company
java getting stack overflow-program is not recursive

java getting stack overflow-program is not recursive

I have been working on my "Random Trivia" program and I keep getting stack overflow with NO recursiveness, at least I cannot find any, here is the code to Guijava, the main class(and only class)

How can you read a boolean array?

How can you read a boolean array?

I have this boolean array that I am using in my app, but I have no idea how to save this array that I am usingHere is how I am saving the array:

JScrollPane doesn&#39;t seem to scroll

JScrollPane doesn't seem to scroll

I'm pretty new to Java SwingCan someone help me figure out what I am doing wrong? Please correct me anywhere necessary