Example usage for java.sql DriverManager getConnection

List of usage examples for java.sql DriverManager getConnection

Introduction

In this page you can find the example usage for java.sql DriverManager getConnection.

Prototype

private static Connection getConnection(String url, java.util.Properties info, Class<?> caller)
            throws SQLException 

Source Link

Usage

From source file:Main.java

/**
 * Create a JDBC connection to the test database.
 * // w  w  w. j  a  v a 2  s. c o m
 * @return
 */
public static Connection getTestConnection() {
    Connection conn = null;
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "fantasy_yahoo_test";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "fantasy";
    String password = "fantasy";

    try {
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url + dbName, userName, password);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return conn;
}

From source file:CreateTableAllTypesInOracle.java

public static Connection getConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
    String username = "scott";
    String password = "tiger";
    Class.forName(driver); // load Oracle driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;/*  w w  w .  j a va  2  s . c  om*/
}

From source file:MainClass.java

public static Connection getConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/mydatabase";
    String username = "root";
    String password = "root";
    Class.forName(driver);/*from  w ww.ja v  a2 s  .  c o  m*/
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
}

From source file:InsertCustomType_Oracle.java

public static Connection getConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
    String username = "userName";
    String password = "pass";
    Class.forName(driver); // load Oracle driver
    return DriverManager.getConnection(url, username, password);
}

From source file:MainClass.java

public static Connection getConnection() throws ClassNotFoundException, SQLException {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:database";
    String username = "name";
    String password = "pass";
    Class.forName(driver);//w  w w.  j a  va  2s . c o  m
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
}

From source file:com.mum.edu.cs472.DbConnection.java

public static Connection getConnection() {

    ///* w  ww. ja v a2s . c om*/
    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/entries", "root", "root");

        return con;
    } catch (Exception e) {
        System.out.println(e);
    }
    return null;
}

From source file:Main.java

public static void setup() {
    try {/*from   w  w w  . jav  a2  s .c om*/
        // Step 1: Load the JDBC driver.
        Class.forName("org.hsqldb.jdbcDriver");
        System.out.println("Driver Loaded.");
        // Step 2: Establish the connection to the database.
        String url = "jdbc:hsqldb:data/tutorial";

        conn = DriverManager.getConnection(url, "sa", "");
        System.out.println("Got Connection.");

        st = conn.createStatement();
    } catch (Exception e) {
        System.err.println("Got an exception! ");
        e.printStackTrace();
        System.exit(0);
    }
}

From source file:TestConnectToMoreThanOneDatabase.java

public static Connection getMySqlConnection() throws Exception {
    String driver = "org.gjt.mm.mysql.Driver";
    String url = "jdbc:mysql://localhost/tiger";
    String username = "root";
    String password = "root";
    Class.forName(driver); // load MySQL driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;//from  www  .j  a  v a 2s. c  o m
}

From source file:Main.java

private static Connection getConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:scorpian";
    String username = "username";
    String password = "password";
    Class.forName(driver);/*ww w.  ja v  a2  s . c o  m*/
    return DriverManager.getConnection(url, username, password);
}

From source file:com.fileanalyzer.dbconnector.DBConnector.java

public static Connection getConnection() {
    try {/*w w  w .  ja  v a 2 s .  c  o  m*/
        Class.forName(conf.getDriverName());
        try {
            con = DriverManager.getConnection(conf.getUrl(), conf.getUsernsme(), conf.getPasword());
        } catch (SQLException ex) {
            log.error("Failed to create the database connection.", ex);
        }
    } catch (ClassNotFoundException ex) {
        log.error("Driver not found.", ex);
    }
    return con;
}