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 ww w. j  av  a  2  s. co  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);
    String procedure = "CREATE OR REPLACE PROCEDURE myprocinout(x IN OUT VARCHAR) IS BEGIN "
            + "INSERT INTO oracle_table VALUES(x); " // Use x as IN parameter
            + "x := 'outvalue'; " // Use x as OUT parameter
            + "END;";
    Statement stmt = connection.createStatement();
    stmt.executeUpdate(procedure);
}

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 av a2 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();
    String function = "CREATE OR REPLACE FUNCTION myfuncout(x OUT VARCHAR) RETURN VARCHAR IS " + "BEGIN "
            + "x:= 'outvalue'; " + "RETURN 'a returned string'; " + "END;";
    stmt.executeUpdate(function);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.jnetdirect.jsql.JSQLDriver";
    Class.forName(driverName);/*  www.  ja v  a2s .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 max table name length
    int length = dbmd.getMaxTableNameLength();
    System.out.println(length);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:odbc:databaseName";
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String user = "guest";
    String password = "guest";

    String theStatement = "SELECT lastname, firstname FROM autors";

    try {//from   ww  w  . ja v a 2 s . c o  m
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        Statement queryAuthors = connection.createStatement();
        ResultSet theResults = queryAuthors.executeQuery(theStatement);

        queryAuthors.close();
    } catch (ClassNotFoundException cnfe) {
        System.err.println(cnfe);
    } catch (SQLException sqle) {
        String sqlMessage = sqle.getMessage();
        String sqlState = sqle.getSQLState();
        int vendorCode = sqle.getErrorCode();
        System.err.println("Exception occurred:");
        System.err.println("Message: " + sqlMessage);
        System.err.println("SQL state: " + sqlState);
        System.err.println("Vendor code: " + vendorCode + "\n----------------");
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:mysql://192.168.100.100:3306/";
    String dbName = "databaseName";
    Statement stmt = null;//from   w w w .j av a2s .  co  m
    ResultSet result = null;
    String driver = "com.mysql.jdbc.Driver";
    String databaseUserName = "admin";
    String databasePassword = "root";
    Class.forName(driver).newInstance();
    Connection conn = DriverManager.getConnection(url + dbName, databaseUserName, databasePassword);
    stmt = conn.createStatement();
    result = null;
    String password, username;
    result = stmt.executeQuery("select * from userTable where username ='user1' ");
    if (!result.isBeforeFirst()) {
        System.out.println("resultset contin no rows");
    }
    while (result.next()) {
        username = result.getString("username");
        password = result.getString("password");
        System.out.println(username + "  " + password);
    }
    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 w w  w .j a  v a 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 object2 AS OBJECT(col_string2 VARCHAR(30), col_integer2 NUMBER)");

    stmt.execute("CREATE TYPE object1 AS OBJECT(col_string1 VARCHAR(30), col_integer2 object2)");

    stmt.execute("CREATE TABLE object1_table(col_integer NUMBER, col_object1 object1)");
}

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. j  a  v a2 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();

    // Use TRUNCATE
    String sql = "TRUNCATE my_table";
    // Execute deletion
    stmt.executeUpdate(sql);
    // Use DELETE
    sql = "DELETE FROM my_table";
    // Execute deletion
    stmt.executeUpdate(sql);
}

From source file:SimpleConnection.java

static public void main(String args[]) {
    Connection connection = null;

    if (args.length != 4) {
        System.out.println("Syntax: java SimpleConnection " + "DRIVER URL UID PASSWORD");
        return;//  w  ww .  java2  s  . co m
    }
    try {
        Class.forName(args[0]).newInstance();
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }
    try {
        connection = DriverManager.getConnection(args[1], args[2], args[3]);
        System.out.println("Connection successful!");
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:TestOCIApp.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:@dssnt01", "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 ww  .  j  a  v a2  s . c o m
    stmt.close();
    conn.close();
}

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.  j av  a  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);
    // Call a procedure with no parameters
    CallableStatement cs = connection.prepareCall("{call myprocin(?)}");

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

    cs.execute();
}