Skip to content

How to connect to DB using JNDI

by Marina Sprava on October 14, 2011

This is a step-by-step tutorial on connection to database using Java API for a directory service Java Naming and Directory Interface (JNDI) in Jelastic. JNDI allows Java software clients to discover and look up data and objects via a name.

(Jelastic can natively run any Java application with no code changes required so the procedure below is applicable to most Java applications.)

 1. Create the environment in Jelastic (with MySQL database in our case)   java jndi, jndi java

 2. Create a new user in a database:

How to create new user you can see - here (Database name: jelasticDb; User_name : jelastic; Password : jelastic)

3. Modify configuration files in your web-application:

context.xml:

 <Context antiJARLocking="true" path="/JNDI">
    <Resource name="jdbc/jelasticDb" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="jelastic" password="jelastic" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://mysql.jndi-example.jelastic.com/jelasticDb"/>
</Context>

web.xml:

<resource-ref>
 <description>MySQL Datasource example</description>
 <res-ref-name>jdbc/jelasticDb</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
</resource-ref>

4. Finally create a connection in a java-class:

 
public class MyConnection{
private DataSource dataSource;

public MyConnection() {
try {

InitialContext context = new InitialContext();
dataSource = (DataSource) context.lookup("java:comp/env/jdbc/jelasticDb");

} catch (NamingException ex) {
Logger.getLogger(MyConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}

public Connection getConnection() {
Connection conn = null;
try {
conn = dataSource.getConnection();
} catch (SQLException ex) {
Logger.getLogger(MyConnection.class.getName()).log(Level.SEVERE, null, ex);
}
return conn;
}

From → HowTo

5 Comments Leave one →
  1. Thanks for sharing. Nicely explained.

  2. Friend…………i want to connect a database using datasource (pooling)…………..rather than using DriverManager.getConnection(url),

    the database i m using is called dragon and username is root and password is ************
    now the problem is , what should i provide in the following…….

    cnt=new InitialContext();
    cnt.bind(???????, ds);
    ds is DataSource instance…………..how do i connect…

    plz help…………

  3. “How to connect to DB using JNDI « Jelastic — Rock-Solid Java in the Cloud, Java
    Server Hosting, Java Cloud Computing” genuinely enables me
    ponder a small bit more. I adored every single part of this blog post.
    Many thanks ,Terrell

Trackbacks and Pingbacks

  1. Jelastic Newsletter – Nov 4, 2011 « Elastic Java Cloud – Platform as a Service

Account registration failed

We suspect that this is due to network issues - so please try again in a minute. If your second attempt fails please email us at info@jelastic.com and we will get you in.

%d bloggers like this: