Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

INVALID CURSOR STATE (attempt in making a forum)

 
Kenneth Lin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's what i tried to do


I need to access how many posts on a thread, therefore my algorithm would be something like this

postThread(post inside thread X)

while(!EOF)
if(threadName==postThread)
post++;

and i get this
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Invalid cursor state

i think rsPost and rsThread can't be run simultaneously, i wonder if there are any available solutions for me to solve this.

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

First of all I would advise you not to put this sort of code in a JSP. It should be in a servlet, or perhaps in some class accessed by a servlet. The purpose of a JSP should be strictly to generate HTML. But if you are going to stick to worst practices, then at least write a JSP in which the <% and %> delimiters come in matching pairs.

As for the JDBC code:



I also think your strategy of reading through the entire database table and using Java code to select the rows you need is a bad strategy. Let the database do that selecting via a suitable WHERE clause.
 
Kenneth Lin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the kind reply Paul!

you are definitely right, i just realized that when i'm half way in doing this, so i guess ill try to get this off first,then i'll fix which one should be in servlet etc..



i quite not understand fully on this, please enlighten me a little , you are saying i read through the rsThread, while its having another result set(rsPost) inside of it? and then re-read the rsThread again?
it wont let me cross connection for some reason, i can't get it to validate the posts whether its in thread X or not.


thanks a lot!!
 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's your code. Can you explain why you read through one result set and then closed a different result set? And then why did you try to read through the first result set again?
 
Kenneth Lin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey sorry for being unclear.

i figured i cant have two result set at the same time, so thats why i open rsthread then i open rspost to compare them, and close them when im finished comparing.

im currently experimenting with the query using join/union but still stuck at the moment.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Such code should not even be in a servlet, let alone in a JSP.

Database code should be in the model layer and have nothing at all to do with the presentation layer. You should be reading data out of the result sets and copying it to normal Java collections, then closing the results sets and releasing connections as quickly as possible.

Putting this code in a JSP is nothing short of irresponsible.
 
Kenneth Lin
Greenhorn
Posts: 12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pardon my irresponsibility, im still learning how this system works. so are you saying that all jsp code such shouldnt be merged with html tags? correct me if im wrong. thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry -- that probably come out sounding more critical than intended. But that is completely correct. Modern JSP pages should use the JSTL and EL.

You might find this article helpful in structuring modern web apps.

Moving the code to a proper model class may also help iron out some of the kinks as your code will be focused on gathering data and nothing else.
 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, I see. In this code:



I didn't notice that you were comparing data from two different result sets. But you haven't read any records from rsPost (i.e. you didn't call rsPost.next()) and so you get that message. Whatever you were trying to do there, it isn't going to work.

At this point I don't have any suggestions about how you should fix that problem, since your code is so far wrong that I don't think it can be salvaged. All I can suggest is to delete it all and start again. This time you should work out the SQL queries which access the database and return only the data you need.
 
Kenneth Lin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot Bear & Paul!

I am seriously thinking of deleting all and start again using the correct approach, though still have many "fill in the blanks" moment with some of the terms in Bear's article.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you know where to ask any questions!
 
Kenneth Lin
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


there it is Bear, I fear this will be off topic for a while.

Is there a simple example of how this works?

client = presentation such as html etc right?
jsp pages = data getter, response team right?
java beans = what is this?
page controller = is it the same as jsp pages?
database server = where you store the dbs right?

thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kenneth Lin wrote:

client = presentation such as html etc right?

After a JSP is processed on the server, the generated response is sent to the client as an HTML page. (See this article.)

jsp pages = data getter, response team right?

No. JSP is view only. No data processing, no gathering, no doing anything but serving as a template for the HTML page to be displayed. (See article linked above.)

java beans = what is this?

Java classes that carry the data. JSP, JSTL and EL are heavily dependent on the Javabean pattern. It's something you must learn and use. (Do not be confused by EJB -- Enterprise Javabeans -- which are something completely different and not something you should concern yourself with.)

page controller = is it the same as jsp pages?

No. The page controller is a servlet that fetches the data needed by the JSP. It is executed as the first resources in the request, then forwards to the JSP once the data is ready.

database server = where you store the dbs right?

Yes, this is where the model get its data from.

thanks!
 
Kenneth Lin
Greenhorn
Posts: 12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for explaining, ive got tons of reading to do eh? haha
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like your positive attitude.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic