
There are many occurrences when the Java application needs to manipulate data in the database. Data can be retrieved by using SELECT queries against the database. Java has provided a class ResultSet which is used to store the complete dataset coming to a java program and then we can retrieve the data as per our requirement from the ResultSet class using its methods.
The default behaviour of ResultSet objects are as follows:-
1. You can only retrieve data using appropriate getXXX() method.
2. You cannot go back in the ResultSet.means all are forward only resultsets.
The next example will demonstrate the use of ResultSet
package com.visualbuilder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class BasicResultSetExample {
public static void main(String[] args) {
try {
/** Loading the driver*/
Class.forName("com.mysql.jdbc.Driver");
/** Getting Connection*/
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
/** Creating Statement*/
Statement stmt = con.createStatement();
ResultSet rs=stmt.executeQuery("select * from visualbuilder");
while(rs.next()) {
System.out.print("Id is " + rs.getInt("id"));
System.out.println(" Name is "+ rs.getString("name"));
}
/** Closing the Connection*/
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:-
Id is 1 Name is Sun java
Id is 2 Name is Visual Builder
Id is 3 Name is Eclipse
Id is 4 Name is IBM
Id is 5 Name is Jakarta
Java Discussion
- - Java web application
- - Difference between BMT an
- - Replace getParameterValue
- - Interviewing Next week -
- - Sudoku solver




