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[] args) {
    Connection conn = null;//from ww  w  .  j a va 2s.c  o  m
    Statement stmt = null;
    boolean executeResult;
    try {
        String driver = "oracle.jdbc.driver.OracleDriver";
        Class.forName(driver).newInstance();
        System.out.println("Connecting to database...");
        String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
        conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
        stmt = conn.createStatement();
        conn.setAutoCommit(false);
        if (!conn.getAutoCommit())
            System.out.println("Auto-commit is set to false");
        String sql = "INSERT INTO Location VALUES(715,'Houston')";
        stmt.executeUpdate(sql);
        sql = "INSERT INTO Employees VALUES" + "(8,'K','4351',{d '2000-02-00'},715)";
        stmt.executeUpdate(sql);
        conn.commit();
    } catch (SQLException se) {
        String msg = se.getMessage();
        msg = "SQLException occured with message: " + msg;
        System.out.println(msg);
        System.out.println("Starting rollback operations...");
        try {
            conn.rollback();
        } catch (SQLException se2) {
            se2.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

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

    Class.forName(DRIVER);/*from w ww .  jav  a2s . c om*/
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("SELECT * FROM users");

    ResultSetMetaData metadata = resultSet.getMetaData();
    int columnCount = metadata.getColumnCount();

    ArrayList<String> columns = new ArrayList<String>();
    for (int i = 1; i < columnCount; i++) {
        String columnName = metadata.getColumnName(i);
        columns.add(columnName);
    }

    while (resultSet.next()) {
        for (String columnName : columns) {
            String value = resultSet.getString(columnName);
            System.out.println(columnName + " = " + value);
        }
    }
}

From source file:Main.java

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

    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);// ww w . jav 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 dmd = connection.getMetaData();
    if (dmd.supportsTransactions()) {
        // Transactions are supported
    } else {
        // Transactions 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);// ww  w.  jav  a 2  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 myfunc}");

    // Register the type of the return value
    int intValue = 0;
    cs.registerOutParameter(1, intValue);

    // 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   www. 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);
    String sql = "INSERT INTO my_table (col_string) VALUES(?)";
    PreparedStatement pstmt = connection.prepareStatement(sql);

    // Insert 10 rows
    for (int i = 0; i < 10; i++) {
        // Set the value
        pstmt.setString(1, "row " + i);

        // Insert the row
        pstmt.executeUpdate();
    }

}

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.jav  a 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);

    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
}

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  av 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);

    Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

}

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  av a 2 s .com*/

    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();
    //     Column Name          SQLServer Type           Java Type
    String sql = "CREATE TABLE sqlserver_all_table(" + "col_boolean          BIT, " // boolean
            + "col_byte             TINYINT, " // byte
            + "col_short            SMALLINT, " // short
            + "col_int              INTEGER, " // int
            + "col_float            REAL, " // float
            + "col_double           DOUBLE PRECISION, " // double
            + "col_bigdecimal       DECIMAL(13,0), " // BigDecimal; can also be NUMERIC(p,s)
            + "col_string           VARCHAR(254), " // String
            + "col_date             DATETIME, " // Date
            + "col_time             DATETIME, " // Time
            + "col_timestamp        TIMESTAMP, " // Timestamp
            + "col_characterstream  TEXT, " // CharacterStream or AsciiStream (< 2 GBytes)
            + "col_binarystream     IMAGE)"; // BinaryStream (< 2 GBytes)

    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 av  a2  s .  com

    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(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet resultSet = stmt.executeQuery("SELECT * FROM my_table");

    // Retrieve the current values of the row from the database
    resultSet.refreshRow();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {//from  www .j  av a2 s . c o  m
        String url = "jdbc:odbc:databaseName";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";
        FileInputStream fis = new FileInputStream("somefile.txt");
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        Statement createTable = connection.createStatement();

        createTable.executeUpdate("CREATE TABLE source_code (name CHAR(20), source LONGTEXT)");

        String ins = "INSERT INTO source_code VALUES(?,?)";
        PreparedStatement statement = connection.prepareStatement(ins);

        statement.setString(1, "TryInputStream"); // Set first field
        statement.setAsciiStream(2, fis, fis.available()); // Stream is source

        int rowsUpdated = statement.executeUpdate();
        System.out.println("Rows affected: " + rowsUpdated);
        connection.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}