Example usage for org.apache.commons.dbcp2 BasicDataSource BasicDataSource

List of usage examples for org.apache.commons.dbcp2 BasicDataSource BasicDataSource

Introduction

In this page you can find the example usage for org.apache.commons.dbcp2 BasicDataSource BasicDataSource.

Prototype

BasicDataSource

Source Link

Usage

From source file:GuestManagerImplTest.java

@Before
public void setUp() throws SQLException {
    BasicDataSource bds = new BasicDataSource();
    bds.setUrl("jdbc:derby:memory:GuestManagerTest;create=true");
    this.dataSource = bds;
    //create new empty table before every test
    try (Connection conn = bds.getConnection()) {
        conn.prepareStatement("CREATE TABLE GUEST (" + "ID INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,"
                + "NAME VARCHAR(50)," + "CREDITCARD VARCHAR(50))").executeUpdate();
    }/*from   www.  j  a v  a2 s . co m*/
    manager = new GuestManagerImpl(bds);
}

From source file:f1db.configuration.ProductionProfile.java

@Bean
public BasicDataSource dataSource() throws URISyntaxException {
    URI dbUri = new URI(System.getenv("DATABASE_URL"));
    String username = dbUri.getUserInfo().split(":")[0];
    String password = dbUri.getUserInfo().split(":")[1];
    String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();
    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setUrl(dbUrl);/*  ww w.  ja  v  a2s.  c o m*/
    basicDataSource.setUsername(username);
    basicDataSource.setPassword(password);
    return basicDataSource;
}

From source file:bibibi.configs.ProductionProfile.java

@Bean
public BasicDataSource dataSource() throws URISyntaxException {
    URI dbUri = new URI(System.getenv("DATABASE_URL"));

    String username = dbUri.getUserInfo().split(":")[0];
    String password = dbUri.getUserInfo().split(":")[1];
    String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ':' + dbUri.getPort() + dbUri.getPath();

    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setUrl(dbUrl);//from   ww w.j a va  2 s.c om
    basicDataSource.setUsername(username);
    basicDataSource.setPassword(password);

    return basicDataSource;
}

From source file:dragonrental.backend.DbConfig.java

public DataSource dataSource() {

    Properties conf = new Properties();
    BasicDataSource ds = new BasicDataSource();
    try {/* w w w.  j  ava2 s.c om*/
        conf.load(DbConfig.class.getResourceAsStream("/Properties.properties"));
    } catch (IOException ex) {
        //log.error("Loading properties has failed!");
    }

    ds.setUrl(conf.getProperty("db.url"));
    ds.setDriverClassName(conf.getProperty("db.driver"));
    ds.setUsername(conf.getProperty("db.user"));
    ds.setPassword(conf.getProperty("db.password"));

    DatabaseMetaData metaData;
    ResultSet tables;
    try (Connection connection = ds.getConnection()) {
        metaData = connection.getMetaData();
        tables = metaData.getTables(null, null, "%", new String[] { "TABLE" });

        //checks wheter there is any tables (will not create new tables if it finds ANY table)
        if (!tables.next()) {
            new ResourceDatabasePopulator(new ClassPathResource("schema-javadb.sql"),
                    new ClassPathResource("test-data.sql")).execute(ds);
        }
    } catch (SQLException ex) {
        System.out.println("SQL Ex when checking for tables");
        System.out.println(ex.getMessage());
    }

    return ds;
}

From source file:guiPackage.AllAccomTableModel.java

private List<Accomodation> getValues() {
    mySource = new BasicDataSource();
    mySource.setUrl(ResourceBundle.getBundle("databaseConnection").getString("connection"));
    mySource.setUsername(ResourceBundle.getBundle("databaseConnection").getString("username"));
    mySource.setPassword(ResourceBundle.getBundle("databaseConnection").getString("password"));
    AccomManager mngr = new AccomManagerImpl(mySource);
    return mngr.getAllAccoms();
}

From source file:com.tukaloff.customers.Config.java

@Bean
public BasicDataSource dataSource() {
    BasicDataSource dbcp = new BasicDataSource();
    dbcp.setDriverClassName("com.mysql.jdbc.Driver");
    dbcp.setInitialSize(5);//from   ww  w.ja v  a2  s .  c  om
    dbcp.setMaxIdle(10);
    return dbcp;
}

From source file:cz.muni.fi.pv168.AgentManagerImplTest.java

@Before
public void setUp() throws Exception {
    BasicDataSource bds = new BasicDataSource();
    bds.setUrl("jdbc:derby:memory:AgentManagerTest;create=true");
    this.dataSource = bds;
    //create new empty table before every test
    try (Connection conn = bds.getConnection()) {
        conn.prepareStatement("CREATE TABLE agent (" + "id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,"
                + "name VARCHAR(255)," + "born DATE)").executeUpdate();
    }/*from  w ww  .j av  a2 s . com*/
    manager = new AgentManagerImpl(bds);
}

From source file:lk.ijse.thogakade.dao.ConnectionFactory.java

private ConnectionFactory() {

    try {/*  ww w .  ja  v  a2  s  .c  om*/
        Properties dbproperties = new Properties();
        FileReader reader = null;

        File file = new File("settings/dbsettings.properties");

        reader = new FileReader(file);
        dbproperties.load(reader);

        bds = new BasicDataSource();

        bds.setUsername((String) dbproperties.getProperty("username"));
        bds.setPassword((String) dbproperties.getProperty("password"));
        System.out.println((String) dbproperties.getProperty("url"));
        bds.setUrl((String) dbproperties.getProperty("url"));
        bds.setDriverClassName((String) dbproperties.getProperty("driver"));

        bds.setInitialSize(10);
        bds.setMaxTotal(10);
        bds.setMaxIdle(10);
        bds.setMinIdle(10);

    } catch (FileNotFoundException ex) {
        Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ConnectionFactory.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:cz.muni.fi.BicycleRental.SpringConfig.java

@Bean
public DataSource dataSource() {
    BasicDataSource bds = new BasicDataSource();
    bds.setDriverClassName("org.apache.derby.jdbc.ClientDriver");
    bds.setUrl(dbUrl);/*from  ww  w  .  ja  va 2s.com*/
    return bds;
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.util.DBTestUtil.java

public static void setDataSourceFromPropertyFile() throws Exception {
    InputStream inputStream = DeviceDAOImplTest.class.getResourceAsStream("/test.properties");

    Properties testProperties = new Properties();
    testProperties.load(inputStream);//ww w .j  av  a  2s.c  o  m

    String host = testProperties.getProperty("db.host");
    String port = testProperties.getProperty("db.port");
    String user = testProperties.getProperty("db.user");
    String password = testProperties.getProperty("db.password");
    String driver = testProperties.getProperty("db.driver");
    String schema = testProperties.getProperty("db.schema");

    String url = "jdbc:mysql://" + host + ":" + port + "/" + schema;

    ds = new BasicDataSource();
    ds.setDriverClassName(driver);
    ds.setUsername(user);
    ds.setPassword(password);
    ds.setUrl(url);
}