File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to connect servlet to mysql database   Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to connect servlet to mysql database   " Watch "How to connect servlet to mysql database   " New topic
Author

How to connect servlet to mysql database

Suvojit Chakraborty
Ranch Hand

Joined: Nov 10, 2009
Posts: 62
    
    1
Hi all,

I am new to mysql. After many difficulty I have created a database with table in MySql. Now I want to connct it to a servlet. How do I do it?
Can anyone tell me the steps, including the step to create MySql datasource

thanks
Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
checkout http://download.oracle.com/javase/tutorial/jdbc/index.html

Create datasource configaration you will find in server documentation. So check your server documentation


Regards,
Sudipta Kumar Laha
Suvojit Chakraborty
Ranch Hand

Joined: Nov 10, 2009
Posts: 62
    
    1
Sudipta Laha wrote:checkout http://download.oracle.com/javase/tutorial/jdbc/index.html

Create datasource configaration you will find in server documentation. So check your server documentation


thanks for the reply. I know the basics of jdbc and can connect with sqlserver. Bt how to connect with Mysql? I mean what do I provide in dsn unless I create a MySql datasource. I cant find any tutorial which teaches how to create MySql datasource. Can you give me any link???

thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What's DSN?

Have you looked at any of the countless MySQL/JDBC tutorials available on the web?

http://www.vogella.de/articles/MySQLJava/article.html

(for example).

Moving to JDBC for now as it's not really related to servlets, unless you're trying to use JNDI.
Suvojit Chakraborty
Ranch Hand

Joined: Nov 10, 2009
Posts: 62
    
    1
David Newton wrote:What's DSN?

Have you looked at any of the countless MySQL/JDBC tutorials available on the web?

http://www.vogella.de/articles/MySQLJava/article.html

(for example).

Moving to JDBC for now as it's not really related to servlets, unless you're trying to use JNDI.


DSN: DATA SOURCE NAME or SUBNAME which comes in the jdbc url.
<protocol>:<subprotocol>:<subname/dsn>

By the way thanks for your link. I will read it and learn.

Anyways I found something useful in here

Now my servlet is picking up data from mysql....

Following happens to be the connectiong code in my program:


I downloaded the Connector/J driver from mysql's site and as per instructions I read in one of the websites I place the .jar in jre/lib/ext folder. Now I have read this in Kathy Sierra book that we use this folder structure to access jar only in development time. So whats the other alternative to this?

Also I found something very strange. After I placed the driver's jar in jre/lib/ext folder, servlet was still not connecting and through exception handling I found that it could not locate the driver. Then when I turned off the PC and restarted later, it was working fine. and now it is picking data from mysql. What could be the reason???

A special thanks to Sudipta Laha for suggesting to check mysql documentation

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You probably didn't restart the server.

If you're getting the connection (not data source, by the way--that's a different thing) through your app you should keep the MySQL driver in your application's WEB-INF/lib directory. If you're getting a data source through the container, it should be in the app container's classpath.
A special thanks to Sudipta Laha for suggesting to check mysql documentation

That should always be your first thought: read the documentation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to connect servlet to mysql database