Saturday, 21 January 2017

READING ARRAY DATA FROM DATABASE TABLE

ResultSet provide the following method
Array getArray(int columnindex) - Retrieves the value of the designated column in the current row of this ResultSet object as an Array object in the java programming language.


Array provide the following method
ResultSet getResultSet() - Retrieves a ResultSet that contains the elements of the SQL ARRAY value designated by this Array object.


import java.sql.Array;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test22 {
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 student");
ResultSet rs = ps.executeQuery();
rs.next();
int id=rs.getInt(1);
String name = rs.getString(2);
System.out.println(id);
System.out.println(name);
Array a = rs.getArray(3);
ResultSet r = a.getResultSet();
while(r.next())
{
System.out.println(r.getString(2));
}
}
}

Output - 
1
rakesh
jdbc
oracle

No comments:

Post a Comment