Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to connect a database from Servlet/Jsp, EJB

 
AMAL KANTI SEAL
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell me giving concrete example code how I can connect to a database ( say SQL Server or MS Access) and interact with it {store/retrieve data).
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
 
Kai Witte
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

the only thing you have to know additionally to what is said in that tutorial is how to configure and declare a DataSource. This is done using the <resource-ref> tag in the deployment descriptor, for example:

In your program you can then look it up using JNDI. Note that res-ref-name is relative to java:comp/env.

The rest of the configuration depends on the web container or application server.

Note that it is common to use a more abstract persistence API rather than using JNDI directly. Common choices are BMP, Hibernate or a custom DAO implementation.

Kai
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic