• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP and JDBC

 
Chandaka Fernando
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am very new to JSP/Servlets and trying to do simple stuff using tomcat 4.1
In my simple project there are few pages that access the database using JDBC. At the moment, everypage has the same code to load drivers, get connections and stuff. If I change the database or password, I have to change every page.
Can I create a function (lets say "getConn(String db, String psswd)" ) which is stored in another file. so I only have to update this if I change the password or database.
Thanks
Chandaka
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best way is not to get a connection in a JSP at all -- JSPs are best used for rendering views in an MVC (Model 2) design. But if you really have to, use the JSP Standard Tag Library (JSTL) for your database access, instead of writing Java code to do it. Instead of using a driver directly, consider using your application server to give you a connection-pooled DataSource.
- Peter
 
Java Brewer
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chandaka Fernando,

Its not a good idea if you are opening Connection in every page as said by one of the friend in the post. If you are using Tomcat 4 or higher, and using Oracle Database then go to http://localhost:8080/tomcat-docs/jndi-datasource-examples-howto.html in your local machine, this page tells you how to create the DATASOURCE and how to use it in your JSP/Servlet/EJB. With this you dont have to worry about creating the connection in each and every page. If you are using some other App. Server then refer the documentation about creating the DATASOURCE for that server.
Regards
Goodieguy
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Brewer
Welcome to the Java Ranch, we hope you�ll enjoy visiting as a regular however, your name is not in keeping with our naming policy here at the ranch. Please change your display name to an appropriate name as shown in the policy.
Thanks again and we hope to see you around the ranch!!
 
zhu a ming
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes Chandaka,getConn()is very good!!!
 
zhu a ming
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
create two bean
1 useful: load driver
2 useful: change user password
 
Chandaka Fernando
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Thanks for all responces... I am new to the whole JSP stuff soo terms like "creating a bean", still sound wierd. I guess that's why we have google.
Will look up all the suggestion...
thanks again
Chandaka
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!