This ResultSet method set cursor to before first record. It sets the cursor
position to 0. It indicates whether the cursor is before the first row in this
ResultSet object.
Syntax:
ResultSet rs;
void rs.beforeFirst();
Now we will create example using this method. In this example, the beforeFirst() point the 0 position in the resultset object.
Example:
|
package ResultSet; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class BeforeFirstMethodExample { public static void main(String[] args) { Connection connection = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "roseindia_jdbc_tutorials"; String driverName = "com.mysql.jdbc.Driver"; String userName = "root"; String password = "root"; try{ Class.forName(driverName).newInstance(); connection = DriverManager.getConnection(url+dbName, userName, password); try{ Statement stmt = connection.createStatement(); String selectquery = "select user_id, user_name from user"; ResultSet rs = stmt.executeQuery(selectquery); rs.beforeFirst(); while (rs.next()) { System.out.print("User id :" + rs.getInt("user_id") + " "); System.out.println("User Name :" + rs.getString("user_name")); } } catch(SQLException s){ System.out.println(s); } connection.close(); } catch (Exception e){ e.printStackTrace(); } } } |
Now run this example using eclipse IDE and see the output.
Program output:
Database table is:

The eclipse console output is:

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.