简体   繁体   中英

how can i check there is a record and get record values from the database using single result set in java?

how can i check there is a record and get value from the database using single resultset in java...

i'm using this code to check there is record in database or not..

then i'm get the record set values but it's not fetch the second row values... It display one row values... anyone give me the solution....

try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager.getConnection("jdbc:odbc:mobile","","");
    String qry = "SELECT * from login";
    ResultSet rs = stmt.executeQuery(qry); 
    if (rs.next()) {
        while(rs.next()) { 
            System.out.println(rs.getString("value1");
            System.out.println(rs.getString("value2");
        }     
    } else {
        System.out.println("No Records");
    }
} catch(Exception e) {  
    System.out.println(e);
}

hi your problem is that you are using rs.next() in if condition as well as in while condition. when you are using while loop you dont need to check if(rs.next())

using this causes you recordset's cursor to move two position further that lead you to skip the first record.

should use

if(!rs.isLast()){
    while(rs.next()){
        System.out.println(rs.getString("value1"));
        System.out.println(rs.getString("value2"));
    }
}

Try this:

If you want to trigger whether there are records exist in database or not:

if (!rs.next()) 
{
    System.out.println("No Records Found");
}
else
{
    while(rs.next())
    { 
        System.out.println(rs.getString("value1");
        System.out.println(rs.getString("value2");
    }
}

If you don't want to trigger whether there are records exist in database or not then following will be enough:

while(rs.next())
{ 
    System.out.println(rs.getString("value1");
    System.out.println(rs.getString("value2");
}

Update1 Try this:

try
{
    pstmt = con.prepareStatement("Select * from login", ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
    rs = pstmt.executeQuery();
    if (!rs.next()) 
    {
        System.out.println("No Records Found");
    }
    else
    {
        rs.beforeFirst();
        while(rs.next())
        { 
            System.out.println(rs.getString("value1");
            System.out.println(rs.getString("value2");
        }
    }
}

The rs.next() method moves the cursor forward one row at a time. After you execute your query, you can immediately start iterating over the ResultSet ; if there is no single record in your ResultSet , the first call to next() will return false and your while block is never executed.

Your current code always skips the first record in the ResultSet , which is most likely not what you want.

Why are you checking if row exists?

while(rs.next()) will automatically check and do if next row exist, but if you want to say something when row not exist, you should use if(rs.isLast())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM