Look up DataSource from InitialContext : DataSource « Database « Java Tutorial






import java.sql.Connection;

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

public class Main {
  public static void main(String[] argv) throws Exception {
    InitialContext init = new InitialContext();
    DataSource source = (DataSource) init.lookup("dsn");
    Connection connection = source.getConnection();
    System.out.println("Connect to " + connection.getCatalog() + " a success!");
  }
}








20.4.DataSource
20.4.1.Look up DataSource from InitialContext
20.4.2.Use JNDI to get database connection or data source