Copying from Result Set to Array List

2,168 views
Skip to first unread message

Rahul

unread,
Jul 15, 2009, 10:58:33 AM7/15/09
to Google Web Toolkit
Hi,
I apologize because this topic is again n again asked on the forum,
but after reading the topics I was not able to get an answer to my
problem.

I am connecting to a server and trying to display an sql query on the
browser. Here is my server side code:

public class GreetingServiceImpl extends RemoteServiceServlet
implements
GreetingService {
private Connection conn = null;
private String status;
private String connString = "Initial value";
private String url = "jdbc:sqlserver:;database";
private String user = "";
private String pass = "";
public ResultSet rs;
public Statement stm;
ArrayList rowArray = new ArrayList();
String name;
public ArrayList greetServer(String input) {
String serverInfo = getServletContext().getServerInfo();
String userAgent = getThreadLocalRequest().getHeader("User-Agent");
// String s1 = "Select * from Patient";
try {
Class.forName
("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
// conn = DriverManager.getConnection(url,user,pass);
conn = DriverManager.getConnection
("jdbc:sqlserver:;database","username","password");
connString = "We connected";
stm = conn.createStatement();
String query1;
rs = stm.executeQuery("select SourceTableName from SourceTables");



while (rs.next()) {
rowArray.add(String.valueOf(rs.getInt(1)));

}


} catch (Exception e) { connString = e.getMessage();}

// if (conn != null)
// connString = "We connected";
// else
// connString = "We failed to connect";

return rowArray;
}


}

I am confused in the following areas:
1) My sql query contains 4 values (string) is while(rs.next()) loop
the correct way to copy into from ResultSet to Arraylist?
2) I have made my return type to Arraylist, so in the default gwt
application when the user clicks the send button would it show the
four values of my sql query or not?

If anyone has some tutorials over how to do this plz tell the links
that would be very helpful
thanks

Chad

unread,
Jul 15, 2009, 11:44:19 AM7/15/09
to Google Web Toolkit
Rahul,

Based on your query (select SourceTableName from SourceTables), I
would guess that your result set would contain a single column with
string data (char or varchar in SQL). If that's correct, then your
loop should look more like this:

while (rs.next()) {
rowArray.add(rs.getString(1));
}

You can also use the column name, as in:

while (rs.next()) {
rowArray.add(rs.getString("SourceTableName"));
}

Your code would only work if the SourceTableName column in the
SourceTables table is of an integer type.

HTH,
Chad

Sean

unread,
Jul 15, 2009, 11:48:54 AM7/15/09
to Google Web Toolkit
while(rs.next()) is fine and pretty standard way to go through.

I'm confused by how you are adding the data to the ArrayList though.
You get your resultSet which judging by your query should be
TableNames, which I assume are Strings?. But you're getting it out as
an INT then converting to a String. You could just do rs.getString
(1) . That might be a problem. I'm not sure what happens when you
say .getInt() on a String Field. Also, since you know the column name,
"SourceTableName", you could do rs.getString("SourceTableName") as
well.

Hope that's of some help.

Rahul

unread,
Jul 15, 2009, 11:49:42 AM7/15/09
to Google Web Toolkit
Hi,
Thanks for replying
I am getting the following error

[ERROR] Uncaught exception escaped
java.lang.ClassCastException: java.util.ArrayList cannot be cast to
java.lang.String
at com.example.test7.client.Test7$1MyHandler$1.onSuccess(Test7.java:
1)
at
com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived
(RequestCallbackAdapter.java:215)
at com.google.gwt.http.client.Request.fireOnResponseReceivedImpl
(Request.java:264)
at com.google.gwt.http.client.Request.fireOnResponseReceivedAndCatch
(Request.java:236)
at com.google.gwt.http.client.Request.fireOnResponseReceived
(Request.java:227)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:
103)
at com.google.gwt.dev.shell.ie.IDispatchImpl.callMethod
(IDispatchImpl.java:126)
at com.google.gwt.dev.shell.ie.IDispatchProxy.invoke
(IDispatchProxy.java:155)
at com.google.gwt.dev.shell.ie.IDispatchImpl.Invoke
(IDispatchImpl.java:294)
at com.google.gwt.dev.shell.ie.IDispatchImpl.method6
(IDispatchImpl.java:194)
at org.eclipse.swt.internal.ole.win32.COMObject.callback6
(COMObject.java:117)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:1925)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2966)
at com.google.gwt.dev.SwtHostedModeBase.processEvents
(SwtHostedModeBase.java:235)
at com.google.gwt.dev.HostedModeBase.pumpEventLoop
(HostedModeBase.java:558)
at com.google.gwt.dev.HostedModeBase.run(HostedModeBase.java:405)
at com.google.gwt.dev.HostedMode.main(HostedMode.java:232)


any suggestions where am i going wrong ?

Rahul

unread,
Jul 15, 2009, 11:54:08 AM7/15/09
to Google Web Toolkit
Hi Sean,
Thanks for replying.
I understood what you are trying to say.

@Chad can you elborate on "Your code would only work if the
SourceTableName column in the
SourceTables table is of an integer type. " My column name is
presently an varchar type

Chad

unread,
Jul 15, 2009, 6:52:19 PM7/15/09
to Google Web Toolkit
Well, just that. The code you originally posted showed you trying to
retrieve integer data from your result set, but your table contains
string type data so your code wouldn't have worked. Now, the error you
posted looks like you are trying to cast your result to a String in
your onSuccess method. Your result will be an ArrayList. You need to
iterate it and read each string in it.

HTH,
Chad

Sean

unread,
Jul 16, 2009, 8:42:09 AM7/16/09
to Google Web Toolkit
In all places you reference your callback, including the Async
Interface, make it's

AsyncCallback<ArrayList<String>> callback

and NOT:

AsyncCallback<<String> callback

Rahul

unread,
Jul 16, 2009, 10:05:57 AM7/16/09
to Google Web Toolkit
Hi,
Thanks for the reply. I have changed the following things you asked me
to do.

This is the code of my onSuceess Method

public void onSuccess(ArrayList<String> result) {
// TODO Auto-generated method stub
// String str [] = (String []) result.toArray (new String
[result.size ()]);
// String h1 = Integer.toString(result.get(0));
String str [] = new String [result.size ()];
//copying arraylist to string array

str = (String[]) result.toArray (str);
dialogBox.setText("Remote Procedure Call");
serverResponseLabel
.removeStyleName("serverResponseLabelError");

// I want to display the first value of the array

serverResponseLabel.setHTML(str[0]);
dialogBox.center();
closeButton.setFocus(true);

}

I am getting the following error
[ERROR] Uncaught exception escaped
java.lang.ArrayIndexOutOfBoundsException: 0
at com.example.test9.client.Test9$1MyHandler$1.onSuccess(Test9.java:
166)
at com.example.test9.client.Test9$1MyHandler$1.onSuccess(Test9.java:
1)
at
com.google.gwt.user.client.rpc.impl.RequestCallbackAdapter.onResponseReceived
i dunno how can be arrayindex out of bound, i am copying it correctly
i belive.
Thanks a lot

Sean

unread,
Jul 16, 2009, 10:42:55 AM7/16/09
to Google Web Toolkit
You can simplify that a lot by just doing:
String firstResult = result.get(0);

You have a lot of conversions that are uncessceary.

But judging by the error you're str has no elements in it. What you
want to check is
result.size() > 0
> ...
>
> read more »

Rahul

unread,
Jul 16, 2009, 11:26:52 AM7/16/09
to Google Web Toolkit
hi
thanks a lot you guys
its started working
:)
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages