I am trying to convert ResultSet to String[][] in Java webservice. It sounds very stupid to convert the Resultset to Strin[][]. The reason I am trying to do this is because Java webservice is called from .Net and it doesnt understand the ResultSet. I hoped it would had accepted the ResultSet as a DataTable but it didnt work out.
One of my problems is that the server I am trying to test my Java webservice is terrible and I cannot work very efficiently, so I cannot test everything.
this is my java webservice. I am trying to convert it to String[][] but it returns null. I think I am doing something wrong when I try to fill the String[][].
Is it correct way to get the rowCount and columnCount of ResultSet? what am I doing wrong?
public String[][] getValues()
{
String[][] arr;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433/MLS_J", "sa", "12345");
java.sql.Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
java.sql.ResultSet rslt = stmt.executeQuery("SELECT * FROM S_USERS");
// to get row count and column count
/////////////////////////////////////
int rowSize = 0;
try {
rslt.last();
rowSize = rslt.getRow();
rslt.beforeFirst();
}
catch(Exception ex) {
}
ResultSetMetaData rsmd = rslt.getMetaData();
int columnSize = rsmd.getColumnCount();
/////////////////////////////////////
arr = new String[rowSize][columnSize];
int i =0;
while(rslt.next() && i < rowSize)
{
for(int j=0;j<columnSize;j++){
arr[i][j] = rslt.getString(j);
}
i++;
}
rslt.close();
stmt.close();
conn.close();
return arr;
} catch (Exception e) {
return null;
}
}
catch(Exception ex){}andcatch (Exception e) { return null; }, you should avoid these practices. If an exception is being thrown, how would you know? Exceptions are very useful for programming, don't just swallow them into the void. You're missing important information here. Don't be afraid of exceptions, they're your friends ;)