• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to connect servlet to mysql database

 
Ranch Hand
Posts: 66
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
checkout http://download.oracle.com/javase/tutorial/jdbc/index.html

Create datasource configaration you will find in server documentation. So check your server documentation
 
Suvojit Chakraborty
Ranch Hand
Posts: 66
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 66
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic