import java.io.*;
import java.net.*;
import java.util.*;
import java.sql.*;
public class ExcelReader
{
/**
* @param args
*/
public static void main(
String[] args)
{
// TODO Auto-generated method stub
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dsnName = "ID";
String str = "jdbc:odbc:"+dsnName;
Connection con = DriverManager.getConnection(str);
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery("Select * from [sheet1$]");
ResultSetMetaData rsmd = rs.getMetaData();
rs.last();
int row_count = rs.getRow();
int column_count = rsmd.getColumnCount();
//int row_count = rsmd.get
System.out.println("Column count" + column_count);
System.out.println("Row count" + row_count);
//String arr_val;
rs.beforeFirst();
//String arr_col[] = new String{"LastName","FirstName","ID"};
String arr_val[][] = new String[row_count][column_count];
//rs.next();
while(rs.next())
{
for(int i=1;i<=row_count;i++)
{
for(int j=1;j<=column_count;j++)
{
arr_val[i-1][j-1] = rs.getString(j);
System.out.print("\t"+arr_val[i-1][j-1]);
}
}
System.out.println();
}
rs.close();
stmt.close();
}
catch(Exception ee)
{
ee.printStackTrace();
}
/* finally
{
con.close();
}*/
}
}
This is my code, wherein I am trying to fetch data from excel sheet and store it in 2d array. I am able to read in first row of excel sheet but while reading 2nd row, code is giving me run time error of
Output:Column count3
Row count2
Oswal Rocky 101.0
java.sql.SQLException: No data found
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLGetDataString(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getDataString(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcResultSet.getString(Unknown Source)
at ExcelReader.main(ExcelReader.java:43)
Can anybody help me where I am going wrong, also, I have called rs.getString() only once then why this problem.
Please help me at the earliest.
Thanks in advance.