Example usage for org.apache.commons.dbcp BasicDataSource setDriverClassName

List of usage examples for org.apache.commons.dbcp BasicDataSource setDriverClassName

Introduction

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

Prototype

public synchronized void setDriverClassName(String driverClassName) 

Source Link

Document

Sets the jdbc driver class name.

Note: this method currently has no effect once the pool has been initialized.

Usage

From source file:BasicDataSourceExample.java

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

    BasicDataSource bds = new BasicDataSource();
    bds.setDriverClassName("com.mysql.jdbc.Driver");
    bds.setUrl("jdbc:mysql://localhost/commons");
    bds.setUsername("root");
    bds.setPassword("");

    //      bds.setInitialSize(5);

    Connection connection = bds.getConnection();

    System.err.println(connection);
    connection.close();//from   w  ww  . ja v  a  2 s. c  om
}

From source file:com.javacreed.examples.app.Example1.java

public static void main(final String[] args) throws SQLException {
    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:target/db");
    ds.setUsername("sa");
    ds.setPassword("");

    try (Connection connection = ds.getConnection()) {
        System.out.println("Connected");
    } finally {/*from w  w  w .j ava 2 s . c om*/
        ds.close();
    }
}

From source file:com.javacreed.examples.app.Example2.java

public static void main(final String[] args) throws SQLException {
    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:target/db");
    ds.setUsername("sa");
    ds.setPassword("");

    try {/*from w  ww  .  j  a  va  2s .c  om*/
        final Flyway flyway = new Flyway();
        flyway.setDataSource(ds);
        flyway.migrate();

        try (Connection connection = ds.getConnection(); Statement stmt = connection.createStatement()) {
            stmt.executeUpdate(
                    "INSERT INTO contacts(name, contacts) VALUES ('Albert Attard', 'albert@javacreed.com')");

            System.out.println("Contacts");
            try (ResultSet rs = stmt.executeQuery("SELECT * FROM contacts")) {
                while (rs.next()) {
                    System.out.printf("  >> [%d] %s (%s)%n", rs.getInt("id"), rs.getString("name"),
                            rs.getString("contacts"));
                }
            }
        }
    } finally {
        ds.close();
    }
}

From source file:com.javacreed.examples.app.Main.java

public static void main(final String[] args) {

    Main.LOGGER.debug("Creating the data source");
    final BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("org.h2.Driver");
    ds.setUrl("jdbc:h2:target/db");
    ds.setUsername("sa");
    ds.setPassword("");

    try {//from  w w w.  j  a  va2  s.c o m
        Main.LOGGER.debug("Executing Flyway (database migration)");
        final Flyway flyway = new Flyway();
        flyway.setDataSource(ds);
        flyway.migrate();

        try (Connection connection = ds.getConnection(); Statement stmt = connection.createStatement()) {
            stmt.executeUpdate(
                    "INSERT INTO contacts(name, contacts) VALUES ('Albert Attard', 'albert@javacreed.com')");

            Main.LOGGER.debug("Contacts");
            try (ResultSet rs = stmt.executeQuery("SELECT * FROM contacts")) {
                while (rs.next()) {
                    Main.LOGGER.debug("  >> [{}] {} ({})", rs.getInt("id"), rs.getString("name"),
                            rs.getString("contacts"));
                }
            }
        } catch (final SQLException e) {
            Main.LOGGER.error("Failed", e);
        }
    } finally {
        try {
            ds.close();
        } catch (final SQLException e) {
            Main.LOGGER.error("Failed to close the data source", e);
        }
    }
}

From source file:mx.com.pixup.portal.demo.DemoDisqueraDelete.java

public static void main(String[] args) {
    System.out.println("BIENVENIDO A PIXUP");
    System.out.println("Mantenimiento catlogo disquera");
    System.out.println("Inserte el nombre de la disquera a eliminar: ");

    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    Connection connection = null;
    Statement statement = null;//from  ww  w . j av  a  2s . co m
    try {
        String nombreDisquera = br.readLine();
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("admin");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/pixup");

        connection = dataSource.getConnection();
        statement = connection.createStatement();

        String sql = "delete from disquera where nombre = '" + nombreDisquera + "'";

        statement.execute(sql);

        System.out.println("Disquera: " + nombreDisquera + " eliminada con xito");

    } catch (Exception e) {
        System.out.println("Error en el sistema, intente ms tarde!!");
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (Exception e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:mx.com.pixup.portal.demo.DemoDisqueraSelect.java

public static void main(String[] args) {
    System.out.println("BIENVENIDO A PIXUP");
    System.out.println("Mantenimiento catlogo disquera");
    System.out.println("Listado de disqueras");

    Connection connection = null;
    Statement statement = null;//from   w w  w  .  j a  v a  2  s  .  c  o  m
    ResultSet resultSet = null;
    try {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("admin");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/pixup");

        connection = dataSource.getConnection();
        statement = connection.createStatement();

        String sql = "select * from disquera order by nombre desc";

        resultSet = statement.executeQuery(sql);

        System.out.println("ID: \t NOMBRE:");
        while (resultSet.next()) {
            Integer id = resultSet.getInt(1);
            String nombre = resultSet.getString(2);
            System.out.println(id + " \t " + nombre);
        }

    } catch (Exception e) {
        System.out.println("Error en el sistema, intente ms tarde!!" + e.getMessage());
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (Exception e) {
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (Exception e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:com.gnizr.core.delicious.DeliciousImportApp.java

/**
 * @param args/*from w  w  w.ja  v a 2  s .  c o  m*/
 */
public static void main(String[] args) throws Exception {
    if (args.length != 1) {
        System.out.println("Missing required arguments.");
        System.exit(1);
    }
    Properties prpt = new Properties();
    prpt.load(new FileInputStream(args[0]));
    String dbdrv = prpt.getProperty("gnizr.db.driver");
    String dbUrl = prpt.getProperty("gnizr.db.url");
    String dbUser = prpt.getProperty("gnizr.db.username");
    String dbPass = prpt.getProperty("gnizr.db.password");
    String gnizrUser = prpt.getProperty("gnizr.import.user");
    String gnizrPassword = prpt.getProperty("gnizr.import.password");
    String gnizrEmail = prpt.getProperty("gnizr.import.email");
    String gnizrFullname = prpt.getProperty("gnizr.import.fullname");
    String deliUser = prpt.getProperty("gnizr.import.delicious.user");
    String deliPassword = prpt.getProperty("gnizr.import.delicious.password");

    BasicDataSource datasource = new BasicDataSource();
    datasource.setDriverClassName(dbdrv);
    datasource.setUrl(dbUrl);
    datasource.setUsername(dbUser);
    datasource.setPassword(dbPass);

    GnizrDao gnizrDao = GnizrDao.getInstance(datasource);

    UserManager userManager = new UserManager(gnizrDao);
    BookmarkManager bookmarkManager = new BookmarkManager(gnizrDao);
    FolderManager folderManager = new FolderManager(gnizrDao);

    User gUser = new User();
    gUser.setUsername(gnizrUser);
    gUser.setPassword(gnizrPassword);
    gUser.setFullname(gnizrFullname);
    gUser.setEmail(gnizrEmail);
    gUser.setAccountStatus(AccountStatus.ACTIVE);
    gUser.setCreatedOn(GnizrDaoUtil.getNow());

    DeliciousImport deliciousImport = new DeliciousImport(deliUser, deliPassword, gUser, userManager,
            bookmarkManager, folderManager, true);
    ImportStatus status = deliciousImport.doImport();
    System.out.println("Del.icio.us Import Status: ");
    System.out.println("- total number: " + status.getTotalNumber());
    System.out.println("- number added: " + status.getNumberAdded());
    System.out.println("- number updated: " + status.getNumberUpdated());
    System.out.println("- number failed: " + status.getNumberError());
}

From source file:edu.caltechUcla.sselCassel.projects.jMarkets.output.OutputWriter.java

public static void main(String[] args) {
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUsername("jmarkets");
    ds.setPassword("banana");

    String dbHost = "david.ssel.caltech.edu";
    String dbPort = "3306";
    String dbName = "jmarkets";

    ds.setUrl("jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName);
    //ds.setUrl("jdbc:mysql://localhost:3306/jmarkets");
    ds.setMaxActive(10);//from w  w  w. j  ava 2 s  . com

    OutputWriter writer = new OutputWriter();
    writer.outputSession(145, "c://output.csv");
}

From source file:mx.com.pixup.portal.demo.DemoDisqueraUpdate.java

public static void main(String[] args) {
    System.out.println("BIENVENIDO A PIXUP");
    System.out.println("Mantenimiento catlogo disquera");
    System.out.println("Actualizacin de Disquera");

    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    Connection connection = null;
    Statement statement = null;//from  w w w .ja v a2s.  com
    ResultSet resultSet = null;
    try {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("admin");
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/pixup");

        connection = dataSource.getConnection();
        statement = connection.createStatement();

        String sql = "select id, nombre from disquera order by nombre";

        resultSet = statement.executeQuery(sql);

        System.out.println("Id Disquera: \t Nombre Disquera");
        while (resultSet.next()) {
            System.out.println(resultSet.getInt("id") + " \t " + resultSet.getString("nombre"));
        }

        System.out.println("Proporcione el id de la disquera a actualizar: ");
        String idDisquera = br.readLine();

        System.out.println("Proporcione el nuevo nombre de la disquera: ");
        String nombreDisquera = br.readLine();

        sql = "update disquera set nombre = '" + nombreDisquera + "' where id = " + idDisquera;

        statement.execute(sql);

        System.out.println("Disqueras Actualizadas:");

        sql = "select id, nombre from disquera order by nombre desc";

        resultSet = statement.executeQuery(sql);

        System.out.println("Id Disquera: \t Nombre Disquera");
        while (resultSet.next()) {
            System.out.println(resultSet.getInt("id") + " \t " + resultSet.getString("nombre"));
        }

    } catch (Exception e) {
        System.out.println("Error en el sistema, intente ms tarde!!");
    } finally {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (Exception e) {
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (Exception e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:com.alibaba.cobar.manager.test.ConnectionTest.java

/**
 * @param args//  ww  w  .  j av  a  2  s . com
 */
public static void main(String[] args) {
    try {
        BasicDataSource ds = new BasicDataSource();
        ds.setUsername("test");
        ds.setPassword("");
        ds.setUrl("jdbc:mysql://10.20.153.178:9066/");
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        ds.setMaxActive(-1);
        ds.setMinIdle(0);
        ds.setTimeBetweenEvictionRunsMillis(600000);
        ds.setNumTestsPerEvictionRun(Integer.MAX_VALUE);
        ds.setMinEvictableIdleTimeMillis(GenericObjectPool.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS);
        Connection conn = ds.getConnection();

        Statement stm = conn.createStatement();
        stm.execute("show @@version");

        ResultSet rst = stm.getResultSet();
        rst.next();
        String version = rst.getString("VERSION");

        System.out.println(version);

    } catch (Exception exception) {
        System.out.println("10.20.153.178:9066   " + exception.getMessage() + exception);
    }
}