0

i need to create a client-server connection (in JAVA), where the server is connected to a database and the client can send queries and get answers. I use the OCSF (by Dr. Timothy C. Lethbridge) , and JDBC driver for JAVA. at the server's side, i have the following code

ResultSetMetaData rsmd=rs.getMetaData();
int numOfCols=rsmd.getColumnCount();
String[] tuple=new String[numOfCols];
Vector result=new Vector();
while (rs.next()){
    for (int i=1;i<=numOfCols;i++)
            tuple[i-1]=rs.getString(i);
    result.addElement(tuple);
    isResultEmpty=0;
}
if (isResultEmpty==0)
    this.sendToAllClients(result);
else
        this.sendToAllClients("No appropriate results!");

so far, it seems to work fine, i made some test-prints if the for loop and it works fine.

for the client side, i have the following code:

public void handleMessageFromServer(Object msg) 
  {
    //clientUI.display(msg.toString());
    if (msg instanceof Vector){
        Vector result=(Vector)msg;
        System.out.println("The result of the query is:\n\n");
        //System.out.println((String)result.firstElement());
        System.out.println("result size is "+result.size());
        for (int i=1;i<=result.size();i++){
            System.out.println("here");
            System.out.println((String)result.get(i));
            System.out.println("here2");
        }
    }
    else
        clientUI.display(msg.toString());
  }

the problem starts when i try to print the results: the first "here" is printed, and nothing else happens, it never gets to the "here2", and seems to just terminate the printing and waits for new input.

is there any problem with the casting I make? i just can't figure out where is the exact problem.... thank you very much in advance for helping

Daniel Briskman
  • 399
  • 2
  • 3
  • 11
  • 1
    "and seems to just terminate the printing" are you sure that you don't have a somewhere at the bottom of the code a `catch` block where you completely ignore the exception? – BalusC Apr 23 '11 at 23:38

3 Answers3

1

You don't have a vector of String objects. You have a vector of string arrays (String[])

Edit in response to comments:

System.out.println("result size is "+result.size());
for (int i = 0;i < result.size();i++){
    System.out.println("here");
    String[] array = (String[])result.get(i));
    for (int j = 0; j < array.length; j++)
        System.out.println(array[j]); 
    System.out.println("here2");
}

This is of course assuming the server side works. I didn't look at it closely.

Edit for server side:

This is all kinds of broke:

while (rs.next()){
    for (int i=1;i<=numOfCols;i++)
        tuple[i-1]=rs.getString(i);
    result.addElement(tuple);
    isResultEmpty=0;
}

On each loop iteration, you're simply changing what your string array contains, and adding the same reference over and over. You need:

String[] tuple;
while (rs.next()){
    tuple = new String[numOfCols];
    for (int i=1;i<=numOfCols;i++)
        tuple[i-1]=rs.getString(i);

    result.addElement(tuple);
    isResultEmpty=0;

}
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • ok, i changed to 'Vector result=(Vector)msg;' how do i print it now? i can't pass the length of the array – Daniel Briskman Apr 23 '11 at 23:22
  • See edit, you would need to print the elements of the string arrays – Brian Roach Apr 23 '11 at 23:31
  • first, resultset starts from 1 and not from 0 , i tried to print the element rs.getString(0) and got an exception. about tour second remark: why do I need to allocate new tuple each time? I just write over the previous tuple, and then add it to the Vector – Daniel Briskman Apr 24 '11 at 07:56
  • first: How bizarre. Corrected. Second - because you're adding a *reference to the string array* to the vector. In your code, you're changing the contents of the `tuple` array on each iteration and re-adding the same array reference to the vector. You end up with a vector containing N elements, all exactly the same; the reference to your single String array. – Brian Roach Apr 24 '11 at 08:07
  • ok, understood the mistake. great, it actually works! thanks a lot! – Daniel Briskman Apr 24 '11 at 08:19
  • Np, glad I could help. My mistake on the ResultSet ... that really is bizarre, I obviously didn't remember that from the last time I used it. – Brian Roach Apr 24 '11 at 08:22
  • yea, it would be much easier if **everything** would start from 0, in all programming languages... :) – Daniel Briskman Apr 24 '11 at 08:35
0

First thing that I noticed is your iteration starts at one and goes through the size (inclusive). You must start at zero and go through the size (exclusive). i=0,i

In the client code, I don't understand the while loop. When you call rs.next() (I am assuming here rs is a ResultSet), you move your pointer one more, unless there are no more entries. Then it seems that you iterate through all the items again. I may be incorrect, as I am unfamiliar with the usage of ResultSet, though from browsing the JavaDoc, it seems problematic.

Try printing the raw msg in your client code, see what spits out. Also go ahead an do the same after the cast, see if the output makes sense.

Michael
  • 369
  • 2
  • 10
  • regarding the iteration, i just tried starting from 1, it didn't help actually i believe you were talking about the while in the server code. rs (which is the resultset) holds a line for each tuple in the answer. if the asnwer consists of several columns, then i have to read it one by one, that what the for loop does. the while loop iterates through all resultset. printing the raw msg? its type is Object.. how can i print it..? – Daniel Briskman Apr 23 '11 at 23:31
  • All Objects have a toString method, of which, if overridden will produce human readable output. If toString() is undefined, it will print out the 'pseudo' memory location. – Michael Apr 25 '11 at 13:09
0

You should not be passing a ResultSet around at all. It's an interface that does not extend Serializable. It's an abstraction for a database cursor. When you pass it from one machine to another, without any information about the connection, what do you expect to happen? I don't see how it can work.

A ResultSet should never be passed out of the method in which it was created. The right thing to is the load it into an object or data structure, close it in a finally block, and return the object or data structure.

Your code is riddled with problems, but I think this is the chief one.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • where did you see that i'm passing the resultset around? that what it is all about, i didn't manage to convert the resultset into the vector correctly.... – Daniel Briskman Apr 24 '11 at 07:59