• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

returning resultset and question on structure

 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, I have my thread to return my connection:




But then I created a thread to return the resultset:





This is right after the other one. Would that cause a problem. I have read that you shouldn't close the rs before it's returned but I cant see why that or the statement can't be resolved. I have also heard that any method dealing with resultset should be private. Is that true?

Now, for the other question. I want to do a 3 tier structure for this application. The application is supposed to receive the data and hold basic commands for the data in the DAO. That is meant to be requested by the business tier which hold all of the methods to manipulate the data which is called by the servlet to deploy it to the server in a GUI created in the business tier. Is that correct? Tell me if I have made any big mistakes please. I hope to become a better software writer.




EDIT:

Also, if the method for resultset is private then how will i use the resultset for other methods elsewhere? I cant find anything for how i would do this. Some people talk about bean mapping instead. I have not come across this. Would you recommend I change everything and use this?
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Update:

I have now declared the statement and resultset outside of thread as null and encapsulated that and thread to get resultset in its own private method. That cleared a lot of errors but now the connection, declared in other thread, could not be resolved so I removed the new method and went back to them both being one method and the connection error left. However, the resultset is now unreachable apparently.

class and method:

 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right: line 27 returns no matter what, so all the code after it accomplishes nothing. Is that line a leftover from a previous code iteration that should be removed?

Line 44 should read "if (rs != null)", because otherwise you will get a NullPointerException in line 45. But the code hasn't done anything with the ResultSet at this point, so I suspect closing it would not actually be the right thing to do. Or maybe you left out some code between lines 36 and 38?
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou, I did actually have !=null in my code and the code i pasted was before that, sorry. Thanks, it does now make sense that I should have a finally and if statement on returning the connection as well. However, it is now saying on the method header that the method must return a result of type connection even though i say to return it. What does this mean?
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ive also added another catch to catch SQLExceptions for line 18 for which there was an error.
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have now created the Connection CS inside the method arguments bracket and there is no error there. Now, using what syntax do I set it to null. If I do it outside and write CS=null; it says there is an error again.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you post the code, we can take a look.
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code with issue:




 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 1 does not make sense. This method should create a Connection, right? It doesn't take a Connection as a parameter. In that line you're saying that it does both.

Line 22 still doesn't make sense. There is code after the line - should that be executed or should it not? If CS is null, then the code is executed- but it can't be executed, because with a null Connection, nothing will work.

You need to decide what this method should do. Should it create a Connection and return it? Then the later code is unnecessary. Or should it actually retrieve a ResultSet? In that case, returning the Connection does not make much sense; returning the data in some structured form -not as a ResultSet (which should be closed at the end of this method)- would make sense.
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, can I create another method to return the result set.
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, i've left the first part as its own method and it works to return the connection. Ive sperated the seond thread as its own getresultset method which passes the parameter connection CS. You say i should return a structured form. What do you mean here and would it go in the 'finally' section or would that just return the result set?
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, i have now changd t and put the second thread in the 'finally' part of the first thread after 'return CS;'. There are now no errors aside from wher i try to close rs at the end if its null. On the if, it says rs cannot be converted to a boolean.

code:

 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes; = is not the same as ==.
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes what
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul refers to line 35.

You should absolutely not put the code to retrieve data in the finally block, because that is executed now matter whether there was an exception or not. You're also still returning CS if it is non-null, thereby ensuring that in the one circumstance where CS is valid, the code to retrieve data is not executed.
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok , i did this:



Now it is saying at if(rs=null) it says cannot convert from resultset to boolean. At 'public connection makecon()' it sys must return result set.
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, the result set method is now this:



It is now only saying that void methods cant return resultsets.
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
makecon: you should get rid of the entire finally block, and just return CS no matter what. The calling code then needs to check whether CS is null or not, and act accordingly.

makeresultset: line 1 says that the return type is "void", so the method can't return anything. I think here, too, you should get rid of the entire finally block, and just return the ResultSet.
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That worked, thank you. Issue solved. Just for clarifiction, I should then just query whether the result set or connection is null in any method i want to use it?
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You pretty much have to, because otherwise you're going to have exceptions all over the place. But realistically, the connection would only need to be checked once, right after the call to makecon - because if for some reason it is null, nothing is going to work, so you might as well just quit the program.

Instead of doing a "System.out.println(o.getMessage())" in lines 12 and 17, you should print out the entire stack trace ("ex.printStackTrace()"), so that you have something to start debugging from.
 
Samuel Bird
Ranch Hand
Posts: 96
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sure, thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic