1

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;
    }
}
1
  • 2
    catch(Exception ex){} and catch (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 ;) Commented Nov 16, 2013 at 17:04

2 Answers 2

3

it is your problem java resultset starts from 1.. so change your getString method like this. And remain all codes fine.

        while(rslt.next() && i < rowSize)
        {
            for(int j=0;j<columnSize;j++){
                arr[i][j] = rslt.getString(j+1);
            }
            i++;                    
        }
Sign up to request clarification or add additional context in comments.

Comments

1

A simple way to get the row count is to use COUNT(*) for your case...

 java.sql.ResultSet rslt = stmt.executeQuery("SELECT COUNT(*) FROM S_USERS");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.