Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting up Datasource object

 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

I was going through some tutorials, I want to learn how to make a datasource object. Below is some code used in the tutorial:



I am alittle confused on how to configure it to work with mine current setup. I have the mysql-connector-java-5.1.6 driver in my lib folder and the link to the mysql db is mysql://localhost/faultsystem?user=myuser&password=mypassword. Can someone tell me what I need to edit in order to get it to work ?


Thanks
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It depends on the environment you're running on. That environment should be configured to provide you the datasource through JNDI.

Before giving the detailed answer, we need to know which environment you're running on.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, the given code doesn't compile and is wrong at several places. Where did you get it from?
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im not entirely sure what you mean by enviroment. Im using tomcat 5 if thats of any help.

I got it from here: http://java.sun.com/docs/books/tutorial/jdbc/basics/connecting.html
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shaf maff wrote:Im not entirely sure what you mean by enviroment. Im using tomcat 5 if thats of any help.

Then read this how to create a datasource in Tomcat so that you can use it in your application: http://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html It contains lot of information and examples.
I see. I've reported it by the feedback link. Hope they fix it soon.
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks - Sorry, I am using tomcat6. I found the tutorial for it: http://tomcat.apache.org/tomcat-6.0-doc/jndi-datasource-examples-howto.html

The problem is it shows how to implement it in a JSP but not a servlet.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a servlet (you should rather do it in the DAO class) just get it from the JNDI context as follows:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic