• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting up a database connection with Struts 2 and Tomcat 6.0

 
Alia Huss
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I've been doing roseindia.net's Struts 2 MySQL tutorial
http://www.roseindia.net/struts/struts2/struts-2-mysql.shtml

The application is suppsed to show you a form where you enter username and password.
When you press save the username and password are supposed to be saved to the Database.
But I'm getting the following error message when I press Save:

java.lang.NullPointerException
net.roseindia.insert.execute(insert.java:33)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:399)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:262)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:224)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:213)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:150)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:48)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.intercept(ParametersInterceptor.java:161)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:83)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:207)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:74)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:127)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:107)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:206)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:115)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:143)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.intercept(PrepareInterceptor.java:115)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:170)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:123)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:219)
com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:218)
com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:216)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:507)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:421)

Someone said that this was because there is no connection between MySQL and Tomcat, is that right?

This is the java class where the exception occurs



Thanks in advance for any tips on how to fix it.
Alia
 
Joe Ess
Bartender
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FYI, Roseindia has a poor reputation for quality.
For example, in the code you posted, on line 15 you register the JDBC driver. Something which only should be performed once, but the Roseindia code does it with every single browser access. Connections are expensive to produce so we normally pool connections. It may be that the author wants to be brief for the purpose of the tutorial. I hope he states as much in the tutorial, otherwise I'd look elsewhere for information.
As to your problem, there are many things that can go wrong.
Is your MySql database installed? Up and running? On port 3306? Can you connect with the command line? MySql Administrator?
Have you created a database named "taskproject?" Really? A question mark at the end?
Did you download a MySql JDBC driver? The version I've used is named "com.mysql.jdbc.Driver" See here.
Did you create a user and password with the credentials you list? A user name with an ampersand? Really?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The old org.gjt... is maintained for backwards compatibility--it still throws me for a loop when I see it in new code, though, since the com.mysql... switch happened a fairly long time ago.

FWIW, this line (from my version of their example):

is a Pretty Bad Idea, unless you've sanitized your data, and even then, I wouldn't recommend it.

Plus the connection/statement isn't closed.

As far as I can tell this isn't a Struts issue yet.

@Joe: Wow, hadn't run across that one, although I did have to deal with someone using a Swing file chooser to download a file from a web app. It almost worked locally (here, by "almost", I mean "a file chooser dialog popped up) but for some reason didn't work as well when deployed to the server.
 
Alia Huss
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, I'll just begin with thanking you for putting the first smile of the day on my lips. It's a wide one.

I know roseindia don't have the best tutorials around, and previous ones I've done always required some researching and fixing. Though sadly the otherones I've tried have been even worse. So I stuck to theirs.

And although the author did not mentioning anything about making the connection thus to be brief for the purpose of the tutorial, I do believe it is so. I am entirely new to webapps, and fairly new to java and MySQL so setting up a connection pool will most likely take me a week to do.


Is your MySql database installed? Up and running? On port 3306? Can you connect with the command line? MySql Administrator?
Yes, Yes, Yes, Yes, Yes

Have you created a database named "taskproject?" Really? A question mark at the end?
This is where the smile began spreading on my lips. No I have not created a database named "taskproject?" The "?" I put there because the only way I learned to make a connection to a database is by writing

And since what they did is the same thing but with several strings, I though that the ? and & had to be there.
And as I can see now , I forgot the "user=" and "password=". My memory equals that of a goldfish.

Did you download a MySql JDBC driver?

Yes, although I also usually use com.mysql.jdbc.Driver. I thought that perhaps it was important that it was the one they used.


Did you create a user and password with the credentials you list?

Yes, and I have used them to log in with MySQL Query Browser, all works just fine.

I have now changed the Driver, database name and password. And I still get the same exception.


Thanks in advance
Alia
 
Alia Huss
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello David.
Right so I've decided to go with my old friend the com.mysql.jdbc.Driver instead, the otherone did freak me out.
But what do you mean by: Plus the connection/statement isn't closed
How do I close it?

And I'm sorry for posting this where it didn't belong.
To be honest I have almost no idea about what I'm doing.

Thanks
Alia
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JDBC-related questions should go to the JDBC forum.

Closing JDBC connections is covered in JDBC documentation and tutorials. If you're having problems getting a connection there should be something showing up in your logs before the NPE.
 
Joe Ess
Bartender
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:@Joe: Wow, hadn't run across that one, although I did have to deal with someone using a Swing file chooser to download a file from a web app. It almost worked locally (here, by "almost", I mean "a file chooser dialog popped up) but for some reason didn't work as well when deployed to the server.


Somebody else posted that link but I can't find the original now.
I've never gotten good advice from roseindia. They pop up in a lot of Struts-related searches, but their code is nowhere near Best Practice.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one of them.
 
Joe Ess
Bartender
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alia Huss wrote:
I know roseindia don't have the best tutorials around, and previous ones I've done always required some researching and fixing. Though sadly the otherones I've tried have been even worse. So I stuck to theirs.


Start with our Struts 2 FAQ. It's got links to some free e-books and the Struts 2 website that are far more useful than roseindia.

Alia Huss wrote:
And since what they did is the same thing but with several strings, I though that the ? and & had to be there.


The ? and & are part of the URL as specified by RFC 1738. The ? means "a search parameter follows" and the & means "and (another search parameter)".

Alia Huss wrote:
I have now changed the Driver, database name and password. And I still get the same exception.


Can you show us the code you are using now, as well as printing out the actual URL you are trying to connect to? The URL is really easy to mess up (as you have seen) and printing it out before calling DriverManager.getConnection(someURL) is vital to debugging.
 
Joe Ess
Bartender
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Here's one of them.




newton.david wrote: *boggled*


 
Alia Huss
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually.. the only thing I had to do was put the mysql-connector-java-5.1.7.bin.jar in the lib folder. After that everything worked like a charm.

The code below was outprinting "mysql-connector-java" or something like that each time I pressed the "Save" button:


When I changed the syso to e.printStackTrace() I got a more helpful Exception.

Thanks anyhow for the help. And I will definately follow your advice David!
 
Dhruva Mistry
Ranch Hand
Posts: 67
Eclipse IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alia Huss wrote:Actually.. the only thing I had to do was put the mysql-connector-java-5.1.7.bin.jar in the lib folder. After that everything worked like a charm.

The code below was outprinting "mysql-connector-java" or something like that each time I pressed the "Save" button:


When I changed the syso to e.printStackTrace() I got a more helpful Exception.

Thanks anyhow for the help. And I will definately follow your advice David!


on roseindia site,
given
and you need to provide suitable DataBase name, table name that is in your database
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic