Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to connect MySQL DB from JSP?

 
Niranjan Mysore
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all ..

Iam finding difficulty in giving connections to MySQL database from JSP file...Anyone please provide this class.
Class.forName("com.mysql.jdbc.Driver")..

This is not working .. Any alternate solutions? Please...
 
Martin Simons
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Download the Driver from http://www.mysql.org/

Also, if you are using the provided line of code in a JSP, you are probably doing domething that is now discouraged in a JSP, and the operation should be moved to a Servlet, a Context asset (whether servlet or session depends on its purpose), a Bean, or a custom JSTL tag.
 
prashant kond
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
my suggetion is better keep DB related code in servlet. Because it will be executed everytime whenever page gets loaded. Getter connection every time it is bit heavy process and also it is not a good practice.
 
Rao Raghu
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this driver:

Class.forName("org.gjt.mm.mysql.Driver")
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Niranjana Nagaraju:
I am finding difficulty in giving connections to MySQL database from JSP file...


your provided information is not enough to understand. Please provide me some details where you find difficulty in your jsp code in order to help and solve your problem..
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Niranjana Nagaraju:

This is not working


are you getting any Exception or compile-time error ?
[ January 24, 2007: Message edited by: Saif uddin ]
 
Joshua Cloch
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, please avoid using the scriptlet directly in jsp pages.

As other people menthioned, it is a good idea to wrap the database processing in servlet. However,it really depends on your application.You can use JSTL in jsp pages for small-scale application.You may find "connection pool" is very useful.Do a google search please.
 
Martin Simons
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ravinder kopparam:
Hi,
my suggetion is better keep DB related code in servlet. Because it will be executed everytime whenever page gets loaded. Getter connection every time it is bit heavy process and also it is not a good practice.


I need to ask, why is it better to put the DB initialization info in a servlet? You do not need to initialise the Driver every time a servlet is accessed. If you are not capable of using the Application/Web servers connection pooling, then you would be much better off putting the initialization code into a class that will be executed by a listener when the application starts, and then save the initialized driver (probably in a class with methods for creating connections and doing other DB related tasks) in the application context.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic