• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Servlets + Databases using MVC model

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I've got to be missing something simple. I'm trying to use one servlet as a standard getter/setter (I'm assuming the problem isn't in that file, but one never knows). I use another servlet to prepare the data for viewing and/or for updating. I use yet another to call to the database and a controller servlet to tie it all together. Nothing is happening, but I've fiddled with it so many times that i can't figure out where the problem is. I think that either I'm not connecting to the database correctly (derby database) and/or I'm not calling items correctly. Can someone take a look? I'd greatly appreciate it! I never really learned servlets that well and have moved on to coding in other languages...and have yet to connect to a database in any of them (yes, most of them are educational exercises).

FYI, I'm using this example and trying to modify it: https://danielniko.wordpress.com/2012/04/17/simple-crud-using-jsp-servlet-and-mysql/

entity.Preferences.java


dbops.PreferencesDbops.java



util.DbUtil.java



db.properties


controller.PreferencesControler.java



index.jsp



listPreferences.jsp



web.xml
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats a large chunk of code.

You mentioned two specific bits: Database connection, and not calling items correctly.

Lets try and narrow it down to the important parts.

Your DB Connection code:



What I don't like about this is that if you get an error connecting to the database, it will just continue on as if nothing has happened.

At the least you should print out the error message. More likely you should just let it throw the exception, or throw a new exception at this point if you don't have a connection. If its gonna fail, you may as well fail fast.
Returning "null" from this method just means you will get a null pointer exception in some unrelated part of the code making it harder to debug.


And time for some questions:
How are you invoking this servlet?
What result do you get?
Any messages in the log?

I would suggest adding some debug logging into your code, so that you can see the flow of information/parameters around and get an idea of where things fall down.

 
Tony McManus
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean debug logging as in:



If so, I've tried that and it didn't throw an error. I have a page with table headers and nothing else.

I called it in dbops.PreferencesDbops.java

Not sure how to get logs in NetBeans, but if I'm looking at the correct thing, there aren't any errors displaying.
 
Stefan Evans
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, lets assume that the Database connection is successful then
Just to be 100% sure you can check that is to put a logging statement along the lines of "Data base connection successful!"

But lets assume that it is fine, and see what else could be wrong.
You have a page loaded and no data in the list.

Where does that data come from?

It is an EL Expression ${preferences}
which comes from your servlet:

request.setAttribute("preferences", dbops.getAllPreferences());

What do you get when you execute dbops.getAllPreferences() ?
Is that method working as you expect?
You could probably write a unit test that just tests this DAO method.

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic