Java DataSource createDerbyEmbeddedDataSource(Properties props)

Here you can find the source of createDerbyEmbeddedDataSource(Properties props)

Description

create Derby Embedded Data Source

License

Apache License

Declaration

private static DataSource createDerbyEmbeddedDataSource(Properties props) throws Exception 

Method Source Code


//package com.java2s;
/*/*from   w w w. jav a  2s .c o  m*/
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at
    
  http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
*/

import java.lang.reflect.Method;

import java.util.Properties;
import javax.sql.DataSource;

public class Main {
    private static DataSource createDerbyEmbeddedDataSource(Properties props) throws Exception {
        String dbName = props.getProperty("db.name", "JdbcConnectorSampleDb");

        // For our sample, avoid a compile-time dependency to the jdbc driver.
        // At runtime, require that the classpath can find it.

        String DERBY_DATA_SOURCE = "org.apache.derby.jdbc.EmbeddedDataSource";

        Class<?> nsDataSource = null;
        try {
            nsDataSource = Class.forName(DERBY_DATA_SOURCE);
        } catch (ClassNotFoundException e) {
            String msg = "Fix the test classpath. ";
            if (System.getenv("DERBY_HOME") == null) {
                msg += "DERBY_HOME not set. ";
            }
            msg += "Class not found: " + e.getLocalizedMessage();
            System.err.println(msg);
            throw new IllegalStateException(msg);
        }
        DataSource ds = (DataSource) nsDataSource.newInstance();

        @SuppressWarnings("rawtypes")
        Class[] methodParams = new Class[] { String.class };
        Method dbname = nsDataSource.getMethod("setDatabaseName", methodParams);
        Object[] args = new Object[] { dbName };
        dbname.invoke(ds, args);

        // create the db if necessary
        Method create = nsDataSource.getMethod("setCreateDatabase", methodParams);
        args = new Object[] { "create" };
        create.invoke(ds, args);

        // set the user
        Method setuser = nsDataSource.getMethod("setUser", methodParams);
        args = new Object[] { props.getProperty("db.user", System.getProperty("user.name")) };
        setuser.invoke(ds, args);

        // optionally set the pw
        Method setpw = nsDataSource.getMethod("setPassword", methodParams);
        args = new Object[] { props.getProperty("db.password") };
        if (args[0] != null)
            setpw.invoke(ds, args);

        return ds;
    }
}

Related

  1. checkTableExists(DataSource dataSource, String tableName)
  2. cleanDB(String ip, DataSource dataSource)
  3. countTable(DataSource dataSource, String tableName)
  4. createDataSource()
  5. createDataTable(DataSource dataSource, String tableName)
  6. createTable(DataSource ds, String ddl)
  7. createTable(DataSource ds, String tableName)
  8. createTableRecommendation(DataSource dataSource)
  9. createTables(DataSource ds)