Saturday, 21 January 2017

READING OBJECT FROM DATABASE

ResultSet provide the following method -

  1. Object getObject(int columnindex) - Gets the value of the designated column in the current row of this ResultSet object as an Object in the java programming language.
  2. Object getObject(int columnindex, Map<String,class<?>>map) - Retrieves the value of the designated column in the current row of this ResultSet object as an Object in the java programming language.


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Struct;
public class Test23 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "rakesh");
PreparedStatement ps = con.prepareStatement("select * from person");
ResultSet rs = ps.executeQuery();
rs.next();
String name = rs.getString(1);
Object o = rs.getObject(2);
Struct s = (Struct)o;
Object a[]=s.getAttributes();
System.out.println(name);
System.out.println(a[0]);
System.out.println(a[1]);
}
}

Output -
rakesh
Bamunimaidam
Guwahati

No comments:

Post a Comment