File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes How to connect MySQL DB from JSP? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to connect MySQL DB from JSP? " Watch "How to connect MySQL DB from JSP? " New topic
Author

How to connect MySQL DB from JSP?

Niranjan Mysore
Greenhorn

Joined: Dec 14, 2006
Posts: 6
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...


Thanks & Regards<br /> Niranjan
Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
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

Joined: Mar 03, 2005
Posts: 18
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

Joined: Jan 05, 2007
Posts: 100
Try this driver:

Class.forName("org.gjt.mm.mysql.Driver")


RAGHU<br /> <br />"When the going gets tough, the tough get going"
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

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..


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

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

Joined: Apr 27, 2006
Posts: 95
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.


truehh@hotmail.com
Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to connect MySQL DB from JSP?
 
Similar Threads
how to connect with mysql?
authentication in jboss
Reading data from mysql database and converting the data into xml format
bean returning error on sql
reporting tool for java webapplications.