Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Operation not allowed after ResultSet closed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been trying to update a row in a table using JSP and JDBC, but every time I try to run the code I am getting an exception

Operation not allowed after ResultSet closed wrote:

. Here is the code




This is the error I am getting


I don't know what am I doing wrong, any suggestions would be appreciated
 
Saloon Keeper
Posts: 12409
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

You are mixing presentation code and database/business code. NEVER do this.

The problem is caused because you are accessing the database as you are rending the view. After you have rendered each row you close the connection. By the time you press a button, the connection is already closed.

The solution is to access the database in a servlet, and when you've gotten all the information you want to display, you forward it to the JSP. The JSP should only use the data you've forwarded to it, it should not perform any logic by itself.
 
Sheriff
Posts: 22015
108
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The problem is caused because you are accessing the database as you are rending the view. After you have rendered each row you close the connection. By the time you press a button, the connection is already closed.


It's even worse. The closing of all the resources occurs within the while loop. So after the first row, everything is closed, and the second row cannot even be retrieved anymore. The clumsy indentation, probably partly caused by the mixing of code and formatting, doesn't help here.
 
Master Rancher
Posts: 4699
49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the joy of having fragments of Java in a JSP page like that.
The formatting can make it really difficult to follow.
 
Saloon Keeper
Posts: 22634
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:That's the joy of having fragments of Java in a JSP page like that.
The formatting can make it really difficult to follow.



And nearly impossible to debug!

If the database logic were in a class file and that class produced an enumerable object (collection) of returned values, then the JSP could use a useBean to find it and only need the JSTL iteration tags (forEach) to present the data.

In that case, if there was a problem with the database logic, an ordinary Java debugger could easily be used to track down any bugs by stepping through that class.
 
Mui Goku
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that this code is very clumsy, I am very new to JSP so I didn't know that you should not use business logic inside the JSP code. Thank you for the criticism it will help me grow
 
Stephan van Hulst
Saloon Keeper
Posts: 12409
269
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good luck! You can always share your progress if you want another code review.
 
Tim Holloway
Saloon Keeper
Posts: 22634
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mui Goku wrote:I understand that this code is very clumsy, I am very new to JSP so I didn't know that you should not use business logic inside the JSP code. Thank you for the criticism it will help me grow



There are three primary reasons why a casual Internet search on JSPs and for that matter in JSP books would show business logic in JSPs, I think.

1. The material is extremely ancient. Obsolete stuff has a remarkable longevity on the Internet, and far too many articles don't carry a date on them.

2. In the case of JSP tutorials, just showing off the power of JSPs. In the same way I can show off the power of electricity by sticking a fork in the wall socket, alas, but showing off the power.

3. Because the genius who published the article isn't as well-versed in JEE as they thought.

The general architecture of JEE webapps these days is almost always based on the Model/View/Controller (MVC) architecture. That generally means that a servlet acts as the Controller, JavaBeans provide Models, and the View Template is often - but not always - a JSP. Pure MVC doesn't actually define such a thing as Business Logic, but since it's a virtual necessity for almost any practical webapp, the biuniess logic usually hangs off the Controller via some sort of rathole. Except for JavaServer Faces (JSF), where it hangs off the Model (Backing Beans). JSF, incidentally does [b]not[/i] use JSP for its View Templates. It has its own templating language in xhtml form (speaking of obsolete documentation on the web, originally JSF did uses JSPs, but not for a long time now). Some frameworks use Velocity, some use other types of XML templates.

So let me back up a moment and point out that there are 2 ways of doing MVC using JEE. One is roll-your-own, where you write all the servlet, JSP and business code yourself. I think Bear is a fan of this. The other use to use a framework. JSF is such a framework, and it's part of the JEE standard and thus available on all full-stack JEE servers. Struts is another framework, and while it influenced the design of JSF, it's not a JEE standard. These frameworks generally come with a pre-supplied Master Controller Servlet and you provide application utility classes to handle the app-specific details. JSF uses POJOs. Struts trequires beans implementing certain interfaces and/or superclasses. There are quite a few other webapp frameworks, but I could spend all day.

There's also Spring Web, which may be halfway between the Master Controller and roll-your-own styles. It uses the Spring Framework to assemble components into an app.
 
Destroy anything that stands in your way. Except this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic