Skip to content

Repository files navigation

programming-in-java

Example programs in Java

What is ResultSet In Java?

  • ResultSet is an interface in Java that is used to get the database result in a table format.
  • The object of ResultSet maintains a cursor pointing to a row of a table. Initially, cursor points to before the first row.
  • But we can make this object to move forward and backward direction by passing either TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE in createStatement(int,int) method as well as we can make this object as updatable by:
Statementstmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); 
  • Example
importjava.sql.*; classFetchRecord{ publicstaticvoidmain(Stringargs[])throwsException{ Class.forName("oracle.jdbc.driver.OracleDriver"); Connectioncon=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle"); Statementstmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); ResultSetrs=stmt.executeQuery("select * from emp765"); //getting the record of 3rd row rs.absolute(3); System.out.println(rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)); con.close(); }} 
  • Another example
/STEP1.Importrequiredpackagesimportjava.sql.*;
publicclassMain{
// JDBC driver name and database URLstaticfinalStringJDBC_DRIVER = "com.mysql.jdbc.Driver"; staticfinalStringDB_URL = "jdbc:mysql://localhost/STUDENTS";
// Database credentialsstaticfinalStringUSER = "username";
staticfinalStringPASS = "password";
publicstaticvoidmain(String[] args) {
Connectionconn = null;
Statementstmt = null;
try{
//STEP 2: Register JDBC driverClass.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connectionSystem.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 4: Execute a querySystem.out.println("Creating statement...");
stmt = conn.createStatement();
Stringsql = "SELECT id, first, last, age FROM Registration";
ResultSetrs = stmt.executeQuery(sql);
//STEP 5: Extract data from result setwhile(rs.next()){
//Retrieve by column nameintid = rs.getInt("id");
intage = rs.getInt("age");
Stringfirst = rs.getString("first");
Stringlast = rs.getString("last");
//Display valuesSystem.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
rs.close();
}catch(SQLExceptionse){
//Handle errors for JDBCse.printStackTrace();
}catch(Exceptione){
//Handle errors for Class.forNamee.printStackTrace();
}finally{
//finally block used to close resourcestry{
if(stmt!=null)
conn.close();
}catch(SQLExceptionse){
}// do nothingtry{
if(conn!=null)
conn.close();
}catch(SQLExceptionse){
se.printStackTrace();
}//end finally try
}//end trySystem.out.println("Goodbye!");
}//end main
}//end Class

About

Example programs in Java

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages