DataSource: getConnection() : DataSource « javax.sql « Java by API






DataSource: getConnection()

/* 
 * */
import java.sql.Connection;

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

public class Main {
  public static void main(String[] args) throws Exception {
    String dsn = args[0];
    System.out.println("Attempting to connect to " + dsn);

    try {
      System.out.println("Initializing the naming context...");
      InitialContext init = new InitialContext();

      System.out.println("Looking up " + dsn);
      DataSource source = (DataSource) init.lookup(dsn);

      System.out.println("Establishing a connection...");
      Connection connection = source.getConnection();

      System.out.println("Connect to " + connection.getCatalog() + " a success!");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

           
       








Related examples in the same category