Web Article - Tomcat DataSource JNDI Example


APACHE_TOMCAT_DIR/conf/context.xml:

< Resource auth="Container" 
driverClassName="com.mysql.jdbc.Driver" 
maxActive="100"  maxIdle="30" maxWait="10000"
name="jdbc/TestDB" type="javax.sql.DataSource" 
url="jdbc:mysql://localhost:3306/jcg?rewriteBatchedStatements=true" 
username="username"  password="password" />
web.xml:

MySQL Datasource example 
jdbc/TestDB 
javax.sql.DataSource 
Container 


DBConnection.java:
package com.javacodegeeks.examples;


import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


/**
 * @author Chandan Singh
 */
public class DBConnection
{
 public static Connection getConnection() throws SQLException, ClassNotFoundException, NamingException
 {
  Context ctx = new InitialContext();
  DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/TestDB");
  Connection connection = ds.getConnection();
                System.out.println(connection);

  return connection;
 }
}




Source: javacodegeeks.com

0 Respostas em "Web Article - Tomcat DataSource JNDI Example"

Powered by Blogger