Example usage for javax.sql DataSource getConnection

List of usage examples for javax.sql DataSource getConnection

Introduction

In this page you can find the example usage for javax.sql DataSource getConnection.

Prototype

Connection getConnection() throws SQLException;

Source Link

Document

Attempts to establish a connection with the data source that this DataSource object represents.

Usage

From source file:Main.java

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!");
}

From source file:TestDSLookUp.java

public static void main(String[] args) throws SQLException, NamingException {

    Context ctx = null;//from   w w w.j a va2  s  . c o m
    try {
        Properties prop = new Properties();
        prop.setProperty(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
        prop.setProperty(Context.PROVIDER_URL, "file:/JNDI/JDBC");
        ctx = new InitialContext(prop);
    } catch (NamingException ne) {
        System.err.println(ne.getMessage());
    }

    DataSource ds = (DataSource) ctx.lookup("joe");
    Connection conn = ds.getConnection();
    Statement stmt = conn.createStatement();
    ResultSet rset = stmt.executeQuery(
            "select 'Hello Thin driver data source tester '||" + "initcap(USER)||'!' result from dual");
    if (rset.next())
        System.out.println(rset.getString(1));
    rset.close();
    stmt.close();
    conn.close();
}

From source file:com.dbconnection.DataSourceConnection.java

public static void main(String[] args) throws Exception {

    ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
    DataSource dataSource = (DataSource) appContext.getBean("dataSource");

    Connection connection = dataSource.getConnection();
    System.out.println("Database with data source connected!");
    String result = query(connection);
    System.out.println(result);/*from www.j  av  a2s  .  c om*/

}

From source file:net.jcreate.home.util.HomeBeanFactory.java

public static void main(String[] args) {
    DataSource ds = (DataSource) HomeBeanFactory.getBean("dataSource");
    try {/*from   w  w w .  j  a va 2s . co m*/
        System.out.println(ds.getConnection().getCatalog());
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:net.jcreate.store.util.StoreBeanFactory.java

public static void main(String[] args) {
    DataSource ds = (DataSource) StoreBeanFactory.getBean("dataSource");
    try {/* w w  w  . ja v  a 2  s  .  co m*/
        System.out.println(ds.getConnection().getCatalog());
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String dsn = args[0];/*w w w .j a va 2 s. com*/
    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();
    }
}

From source file:JndiDataSource.java

public static void main(String args[]) throws Exception {
    String sp = "com.sun.jndi.fscontext.RefFSContextFactory";
    String file = "file:/e:/JNDI";
    String dataSourceName = "jdbc/myDatabase";

    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, sp);
    env.put(Context.PROVIDER_URL, file);
    ctx = new InitialContext(env);

    bindDataSource(ctx, dataSourceName);

    DataSource ds = null;
    ds = (DataSource) ctx.lookup(dataSourceName);

    Connection conn = ds.getConnection();

    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT Name FROM Employees");

    while (rs.next())
        System.out.println(rs.getString("name"));

    ctx.close();/*from w w w . j  ava  2 s.c  o  m*/
    rs.close();
    stmt.close();
    conn.close();

    ctx.close();
    conn.close();
}

From source file:org.activiti.dmn.engine.impl.db.DbSchemaDrop.java

public static void main(String[] args) {
    try {/*from   ww w.  java 2 s . c om*/
        DmnEngine dmnEngine = DmnEngines.getDefaultDmnEngine();
        DataSource dataSource = dmnEngine.getDmnEngineConfiguration().getDataSource();

        DatabaseConnection connection = new JdbcConnection(dataSource.getConnection());
        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
        database.setDatabaseChangeLogTableName(
                DmnEngineConfiguration.LIQUIBASE_CHANGELOG_PREFIX + database.getDatabaseChangeLogTableName());
        database.setDatabaseChangeLogLockTableName(DmnEngineConfiguration.LIQUIBASE_CHANGELOG_PREFIX
                + database.getDatabaseChangeLogLockTableName());

        if (StringUtils.isNotEmpty(dmnEngine.getDmnEngineConfiguration().getDatabaseSchema())) {
            database.setDefaultSchemaName(dmnEngine.getDmnEngineConfiguration().getDatabaseSchema());
            database.setLiquibaseSchemaName(dmnEngine.getDmnEngineConfiguration().getDatabaseSchema());
        }

        if (StringUtils.isNotEmpty(dmnEngine.getDmnEngineConfiguration().getDatabaseCatalog())) {
            database.setDefaultCatalogName(dmnEngine.getDmnEngineConfiguration().getDatabaseCatalog());
            database.setLiquibaseCatalogName(dmnEngine.getDmnEngineConfiguration().getDatabaseCatalog());
        }

        Liquibase liquibase = new Liquibase("org/activiti/dmn/db/liquibase/activiti-dmn-db-changelog.xml",
                new ClassLoaderResourceAccessor(), database);
        liquibase.dropAll();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.activiti.form.engine.impl.db.DbSchemaDrop.java

public static void main(String[] args) {
    try {/*from   www  . j  a  v  a  2s .c  om*/
        FormEngine formEngine = FormEngines.getDefaultFormEngine();
        DataSource dataSource = formEngine.getFormEngineConfiguration().getDataSource();

        DatabaseConnection connection = new JdbcConnection(dataSource.getConnection());
        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
        database.setDatabaseChangeLogTableName(
                FormEngineConfiguration.LIQUIBASE_CHANGELOG_PREFIX + database.getDatabaseChangeLogTableName());
        database.setDatabaseChangeLogLockTableName(FormEngineConfiguration.LIQUIBASE_CHANGELOG_PREFIX
                + database.getDatabaseChangeLogLockTableName());

        if (StringUtils.isNotEmpty(formEngine.getFormEngineConfiguration().getDatabaseSchema())) {
            database.setDefaultSchemaName(formEngine.getFormEngineConfiguration().getDatabaseSchema());
            database.setLiquibaseSchemaName(formEngine.getFormEngineConfiguration().getDatabaseSchema());
        }

        if (StringUtils.isNotEmpty(formEngine.getFormEngineConfiguration().getDatabaseCatalog())) {
            database.setDefaultCatalogName(formEngine.getFormEngineConfiguration().getDatabaseCatalog());
            database.setLiquibaseCatalogName(formEngine.getFormEngineConfiguration().getDatabaseCatalog());
        }

        Liquibase liquibase = new Liquibase("org/activiti/form/db/liquibase/activiti-form-db-changelog.xml",
                new ClassLoaderResourceAccessor(), database);
        liquibase.dropAll();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] argv) {
    Connection connection = null;
    Statement statement;/*from  ww  w .j  av  a 2 s .  c  om*/
    ResultSet rs;
    try {
        Context ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/AccountsDB");
        connection = ds.getConnection();

        DatabaseMetaData md = connection.getMetaData();
        statement = connection.createStatement();

        System.out.println("getURL() - " + md.getURL());
        System.out.println("getUserName() - " + md.getUserName());
        System.out.println("getDatabaseProductVersion - " + md.getDatabaseProductVersion());
        System.out.println("getDriverMajorVersion - " + md.getDriverMajorVersion());
        System.out.println("getDriverMinorVersion - " + md.getDriverMinorVersion());
        System.out.println("nullAreSortedHigh - " + md.nullsAreSortedHigh());

        System.out.println("<H1>Feature Support</H1>");
        System.out.println(
                "supportsAlterTableWithDropColumn - " + md.supportsAlterTableWithDropColumn() + "<BR>");
        System.out.println("supportsBatchUpdates - " + md.supportsBatchUpdates());
        System.out.println("supportsTableCorrelationNames - " + md.supportsTableCorrelationNames());
        System.out.println("supportsPositionedDelete - " + md.supportsPositionedDelete());
        System.out.println("supportsFullOuterJoins - " + md.supportsFullOuterJoins());
        System.out.println("supportsStoredProcedures - " + md.supportsStoredProcedures());
        System.out.println("supportsMixedCaseQuotedIdentifiers - " + md.supportsMixedCaseQuotedIdentifiers());
        System.out.println("supportsANSI92EntryLevelSQL - " + md.supportsANSI92EntryLevelSQL());
        System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());
        System.out.println("getMaxRowSize - " + md.getMaxRowSize());
        System.out.println("getMaxStatementLength - " + md.getMaxStatementLength());
        System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
        System.out.println("getMaxConnections - " + md.getMaxConnections());
        System.out.println("getMaxCharLiteralLength - " + md.getMaxCharLiteralLength());

        System.out.println("getTableTypes()");
        rs = md.getTableTypes();
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
        System.out.println("getTables()");
        rs = md.getTables("accounts", "", "%", new String[0]);
        while (rs.next()) {
            System.out.println(rs.getString("TABLE_NAME"));
        }
        System.out.println("Transaction Support");
        System.out.println("getDefaultTransactionIsolation() - " + md.getDefaultTransactionIsolation());
        System.out
                .println("dataDefinitionIgnoredInTransactions() - " + md.dataDefinitionIgnoredInTransactions());

        System.out.println("General Source Information");
        System.out.println("getMaxTablesInSelect - " + md.getMaxTablesInSelect());
        System.out.println("getMaxColumnsInTable - " + md.getMaxColumnsInTable());
        System.out.println("getTimeDateFunctions - " + md.getTimeDateFunctions());
        System.out.println("supportsCoreSQLGrammar - " + md.supportsCoreSQLGrammar());

        System.out.println("getTypeInfo()");
        rs = md.getTypeInfo();
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}