Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic