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

public static void main(String[] argv) throws Exception {
    String driverName = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName);/*from   w w w .j  a  va  2 s. c  om*/

    String serverName = "127.0.0.1";
    String portNumber = "1521";
    String sid = "mydatabase";
    String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
    String username = "username";
    String password = "password";
    Connection connection = DriverManager.getConnection(url, username, password);

    Statement stmt = connection.createStatement();

    stmt.execute("CREATE TYPE number_varray AS VARRAY(10) OF NUMBER(12, 2)");

    stmt.execute("CREATE TYPE my_object AS OBJECT(col_string2 VARCHAR(30), col_int2 INTEGER)");

    //    Column Name          Oracle Type             Java Type
    String sql = "CREATE TABLE oracle_all_table(" + "col_short           SMALLINT, " // short
            + "col_int             INTEGER, " // int
            + "col_float           REAL, " // float; can also be NUMBER
            + "col_double          DOUBLE PRECISION, " // double; can also be FLOAT or NUMBER
            + "col_bigdecimal      DECIMAL(13,0), " // BigDecimal
            + "col_string          VARCHAR2(254), " // String; can also be CHAR(n)
            + "col_characterstream LONG, " // CharacterStream or AsciiStream
            + "col_bytes           RAW(2000), " // byte[]; can also be LONG RAW(n)
            + "col_binarystream    RAW(2000), " // BinaryStream; can also be LONG RAW(n)
            + "col_timestamp       DATE, " // Timestamp
            + "col_clob            CLOB, " // Clob
            + "col_blob            BLOB, " // Blob; can also be BFILE
            + "col_array           number_varray, " // oracle.sql.ARRAY
            + "col_object          my_object)"; // oracle.sql.OBJECT

    stmt.executeUpdate(sql);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//w ww .  j a va2 s . co m

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);
    CallableStatement cs = connection.prepareCall("{? = call myfuncin(?)}");

    cs.registerOutParameter(1, Types.VARCHAR);

    // Set the value for the IN parameter
    cs.setString(2, "a string");

    // Execute and retrieve the returned value
    cs.execute();
    String retValue = cs.getString(1);

}

From source file:Main.java

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

    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*from  w w w .j a  va 2 s.c o m*/

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);

    Statement stmt = connection.createStatement();
    int fetchSize = stmt.getFetchSize();

    // Set the fetch size on the statement
    stmt.setFetchSize(100);

    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    // Change the fetch size on the result set
    resultSet.setFetchSize(100);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//from   www .j  a  v a  2 s  .c om

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);
    DatabaseMetaData dbmd = connection.getMetaData();
    ResultSet resultSet = dbmd.getTypeInfo();

    while (resultSet.next()) {
        // Get the database-specific type name
        String typeName = resultSet.getString("TYPE_NAME");

        // Get the java.sql.Types type to which this database-specific type is mapped
        short dataType = resultSet.getShort("DATA_TYPE");
    }
}

From source file:TestOCINet8App.java

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

    Class.forName("oracle.jdbc.driver.OracleDriver");
    // or you can use:
    //  DriverManager.registerDriver(
    //   new oracle.jdbc.driver.OracleDriver());
    Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@(DESCRIPTION = "
            + "(ADDRESS_LIST = (ADDRESS = " + "(PROTOCOL = TCP)(HOST = dssw2k01)(PORT = 1521)))"
            + "(CONNECT_DATA = (SERVICE_NAME = dssw2k01)))", "scott", "tiger");

    Statement stmt = conn.createStatement();
    ResultSet rset = stmt.executeQuery("select 'Hello OCI driver tester '||USER||'!' result from dual");
    while (rset.next())
        System.out.println(rset.getString(1));
    rset.close();/*from  w  w  w  .java2 s  . com*/
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driverName);//from www  .  ja va  2 s  .  c o m

    String serverName = "127.0.0.1";
    String portNumber = "1521";
    String sid = "mydatabase";
    String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
    String username = "username";
    String password = "password";
    Connection connection = DriverManager.getConnection(url, username, password);
    CallableStatement cs = connection.prepareCall("{? = call myfuncinout(?)}");

    Statement stmt = connection.createStatement();

    // Create procedure myproc with no parameters
    String procedure = "CREATE OR REPLACE PROCEDURE myproc IS " + "BEGIN "
            + "INSERT INTO oracle_table VALUES('string 1'); " + "END;";
    stmt.executeUpdate(procedure);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection dbConnection = null;
    PreparedStatement preparedStatement = null;
    Class.forName(DB_DRIVER);/*ww w.  ja  va 2  s.  c o m*/
    dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
    String updateTableSQL = "UPDATE Person SET USERNAME = ? " + " WHERE USER_ID = ?";
    preparedStatement = dbConnection.prepareStatement(updateTableSQL);

    preparedStatement.setString(1, "newValue");
    preparedStatement.setInt(2, 1001);

    preparedStatement.executeUpdate();

    preparedStatement.executeUpdate();
    preparedStatement.close();
    dbConnection.close();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*from w ww. ja v  a 2s.  c om*/

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);
    DatabaseMetaData dmd = connection.getMetaData();
    if (dmd.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)) {
        System.out.println("Insensitive scrollable result sets are supported");
    }
    if (dmd.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE)) {
        System.out.println("Sensitive scrollable result sets are supported");
    }
    if (!dmd.supportsResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE)
            && !dmd.supportsResultSetType(ResultSet.TYPE_SCROLL_SENSITIVE)) {
        System.out.println("Updatable result sets are not supported");
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//  w w  w. j a v a  2s.c  o  m

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);

    DatabaseMetaData dbmd = connection.getMetaData();

    // Get all stored procedures in any schema and catalog
    ResultSet resultSet = dbmd.getProcedures(null, null, "%");

    // Get stored procedure names from the result set
    while (resultSet.next()) {
        String procName = resultSet.getString(3);
        System.out.println(procName);
    }
}

From source file:Main.java

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

    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);//from ww w.  ja  va 2s .  co m

    String serverName = "127.0.0.1";
    String portNumber = "1433";
    String mydatabase = serverName + ":" + portNumber;
    String url = "jdbc:JSQLConnect://" + mydatabase;
    String username = "username";
    String password = "password";

    Connection connection = DriverManager.getConnection(url, username, password);

    // Get the fetch size of a statement
    Statement stmt = connection.createStatement();
    int fetchSize = stmt.getFetchSize();

    // Set the fetch size on the statement
    stmt.setFetchSize(100);

    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    // Change the fetch size on the result set
    resultSet.setFetchSize(100);

}