• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in comparing the results JDBC

 
Jack Tauson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please consider the following code:


I am naming database located at xx.xx.x.xxx as Maindb for our discussion purpose. I have a table in the Maindb and column with name "IPStatus".
So, basically for each IP addresses (starting from aa.aa.a.aaa to gg.gg.g.ggg), I have a value 0 or 1 set in the IPStatus column in the database.
I want to basically establish connection to the above 7 databases only if the value of the IP Status is 1. So for example, if the value of IPStatus field is 1 for the IP addresses , namely, aa.aa.a.aaa,
bb.bb.b.bbb and dd.dd.d.ddd and for others it's zero, so I would like to establish connection to only first three. I am doing Something like the following inside the aforementioned try-block:



** Problem Description:**

The problem I notices is now with my SQL Query which doesn't makes sense with the following comparision I am doing in my code. Because when I checked the Query
SELECT IPStatus FROM Maindb.TableIPStatus , I just gor 7 rows and one columns with values 0,0,0,0,0,1,1. And when I am comparing it with 1 in my code, it doesn't make any sense.
Am I doing something wrong here? Thanks

**My database Schema is as follows:**

http://sqlfiddle.com/#!2/8cce4


Here is the out put of the print statement I am getting:

 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Jack!

First of all that code won't compile because you declare MainConnection twice, on two consecutive lines. But assuming your real code doesn't have that duplication, it doesn't make sense to me to create a new array each time through the loop. That just throws away the data which earlier iterations through the loop put into the array. You should declare both arrays outside the loop, and you should fill in the remote IP addresses outside the loop as well.

It doesn't do any harm to repeatedly set up the remote IP addresses, but it's kind of confusing and it's better to do things only once. However it does do harm to repeatedly create the MainConnection array.
 
Jack Tauson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for Welcoming me. I have updated my code and removed the duplication. I didn't have duplicate Main Connection in my real code. Do you see something wrong with the way I am pulling the records and trying to compare? Please let me know.
Paul Clapham wrote:Welcome to the Ranch, Jack!

First of all that code won't compile because you declare MainConnection twice, on two consecutive lines. But assuming your real code doesn't have that duplication, it doesn't make sense to me to create a new array each time through the loop. That just throws away the data which earlier iterations through the loop put into the array. You should declare both arrays outside the loop, and you should fill in the remote IP addresses outside the loop as well.

It doesn't do any harm to repeatedly set up the remote IP addresses, but it's kind of confusing and it's better to do things only once. However it does do harm to repeatedly create the MainConnection array.
 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jack Tauson wrote:Do you see something wrong with the way I am pulling the records and trying to compare? Please let me know.


No, I don't see anything wrong with those two things. I already pointed out the problem I did see, so why not work on that one?
 
Jack Tauson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made the changes but still it's looping over the IP Address for which the value is 0. Ideally it should loop over only two IP addresses as there are only two values for which the value is 1.
Paul Clapham wrote:
Jack Tauson wrote:Do you see something wrong with the way I am pulling the records and trying to compare? Please let me know.


No, I don't see anything wrong with those two things. I already pointed out the problem I did see, so why not work on that one?
 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, no, the code you posted will look at all rows of that table every time through the loop. When it does that it's bound to find a row for which ipStatus is 1, since some of the rows are like that, and therefore it will open a new connection and assign it to that array entry for every one of the remote IP addresses.

I'm kind of baffled about what you're trying to do there. What's the point of having a table with 1's and 0's in it when it doesn't tell you which IP address the 1's and 0's belong to?
 
Jack Tauson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can understand your confusion and I am also not satisfied with what I am doing. Please feel free to suggest any changes if you feel like.

What I want is:

I only want to loop through(more specifically establish connections) to only those databases for which the IPStatus value is set to 1. Actually, my application will be running again and again and I have plenty of databases to connnect to rather than 7 I have mentioned above, so I will be manually setting the values to 1 or 0 based on my requirement for some databases by editing the MySQL table. So that's the reason I am looking for a mechanism where I can take some databases out of the loop quickly. I hope it's clear to you know.

So, if I write a query like the following



Then I would have those IP addresses for which the value is 1. Then I am wonderign how should I proceed with if condition so that I loop over only these IP address ? Thanks

Paul Clapham wrote:Well, no, the code you posted will look at all rows of that table every time through the loop. When it does that it's bound to find a row for which ipStatus is 1, since some of the rows are like that, and therefore it will open a new connection and assign it to that array entry for every one of the remote IP addresses.

I'm kind of baffled about what you're trying to do there. What's the point of having a table with 1's and 0's in it when it doesn't tell you which IP address the 1's and 0's belong to?
 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your idea to produce a ResultSet which only contains the remote IP addresses which have 1 in that column is much better than the original code.

But let me ask something else. Is there a particular reason you chose an array to contain the working connections? An array is not very suitable in this case because you aren't going to know how long it should be. If possible you should modify your code to use a List. Then you can just go through your ResultSet, create a connection for each row, and add those connections to the list. No need to hard-code IP addresses, no need for an outer loop, just a simple loop through the ResultSet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic