Java by API/javax.sql/DataSource

Материал из Java эксперт
Перейти к: навигация, поиск

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();
    }
  }
}