This week's book giveaways are in the Cloud/Virtualization and Go forums.
We're giving away four copies each of Cloud Native Transformation: Practical Patterns for Innovation and The Go Workshop and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native Transformation: Practical Patterns for InnovationE this week in the Cloud/Virtualization forum
or The Go Workshop in the Go 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
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Unable to retrieve values from mySql DB using JSP MVC application

 
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm testing a JSP MVC application on Netbeans 731. This is an admin application which simply queries a mySql DB and returns a list of users. The application starts with index.jsp where the user clicks the 'display' hyperlink. This then calls a displayUsers GET action which uses the DisplayUsersServlet. The DBUser class is used as a DAO that talks to the DB.

The application compiles and runs without any problems. I even tested the Connection pooling and manually queried the DB to make sure the values are there. But when I run the application I just see the columns displayed without the values. I'm thinking that I made some mistakes in coding either the UserDB class and/or the DisplayUsersServlet.

Would really appreciate any advice. My code is:






users.jsp
 
Marshal
Posts: 67334
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put in some logging statements (I like Log4J for that, but to get started you can simply uses System.out.println) to see where the data is going awry.
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry where exactly do you suggest I put the System.out.println and what should I try printing the seletUser array? Thanks, J
 
Bear Bibeault
Marshal
Posts: 67334
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At various points along the way in order to check that the data contains what you expect.

For example, in the servlet after the DAO call at line 112 to verify that the data is what you expect. If not, then you know that you need to dive into the DAO.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you checked that there is actual data in the User table, using a db query tool, not your code?
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I did a manual SELECT query using the mySql command line and successfully got all 4 records in the DB.

Following Bear's advice I also added a few System.out.println statements. Adding System.out.println(rs.getString("FirstName")); in the UserDB class I was able to see a print out of the DB records in the console. When restarting the application I noticed the following error in the Tomcat log. Does this suggest my application is corrupted (seeing as I opened this project in Netbeans didn't build it from scratch)?


java.lang.IllegalArgumentException: Document base C:\murach\extract\servlet_jsp\netbeans\ex_starts\ch07email\build\web does not exist or is not a readable directory
at org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:138)
at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:5002)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5182)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:656)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1635)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)


Any ideas if this error is a corruption of Netbeans, Tomcat or my JDK? I'd like to be on the safe side and re-install all but would appreciate any suggestions if anybody has come across this error before. I searched through the forum but didn't really find anything definitive on this error.

I also followed Bear's advice and added log4j, which doesn't output any errors so it looks like the database objects are working fine??
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I re-instaled Netbeans, JDK and Tomcat, so the prior error referring to the build/web directoy is gone. The following output from log4j suggests that the DAO is talking to the DB and the ArrayList objects are being correctly set. The only mistake is that the HTTP request object produces a NULL QueryString (line 23). Also I'm stiilll figuring out how to add loggers to the users.jsp file.

log4j output:


[/code]
 
Jay Tai
Ranch Hand
Posts: 222
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved this myself. The solution is to be consistent with the name of the 'user' object passed by the DAO. Instead of:



use:



The modification of the code will mean that the user object is consistently passed throughout the whole MVC structure.


 
Oh the stink of it! Smell my tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!