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 user = "root";
    String pass = "root";

    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass);
    Statement st = con.createStatement();
    String table = "CREATE TABLE java_DataTypes2(typ_boolean BOOL, "
            + "typ_byte          TINYINT, typ_short         SMALLINT, "
            + "typ_int           INTEGER, typ_long          BIGINT, "
            + "typ_float         FLOAT,   typ_double        DOUBLE PRECISION, "
            + "typ_bigdecimal    DECIMAL(13,0), typ_string        VARCHAR(254), "
            + "typ_date          DATE,    typ_time          TIME, " + "typ_timestamp     TIMESTAMP, "
            + "typ_asciistream   TEXT,    typ_binarystream  LONGBLOB, " + "typ_blob          BLOB)";

    st.executeUpdate(table);//from  ww w.  jav a2  s . c o  m
    con.close();
}

From source file:Main.java

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

    Timestamp tstamp = new Timestamp(0);

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    String sql = "INSERT myTable VALUES(?,?)";
    PreparedStatement prest = con.prepareStatement(sql);
    prest.setString(1, "x");
    prest.setTimestamp(2, tstamp.valueOf("2009-02-24 12:51:42.11"));
    int row = prest.executeUpdate();
    System.out.println(row + " row(s) affected)");

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String URL = "jdbc:odbc:dbName";
    Connection conn = DriverManager.getConnection(URL, "user", "passw");

    DatabaseMetaData dmd = conn.getMetaData();
    ResultSet rs1 = dmd.getSchemas();
    while (rs1.next()) {
        String ss = rs1.getString(1);
        ResultSet rs2 = dmd.getTables(null, ss, "%", null);
        while (rs2.next())
            System.out.println(rs2.getString(3) + " " + rs2.getString(4));
    }//from   w w  w .j  a v a2  s  .com
    conn.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection con = null;/* w ww . j a  v a 2  s. c om*/
    CallableStatement proc_stmt = null;

    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    con = DriverManager.getConnection("jdbc:sqlserver://MYSERVER;databaseName=MYDATABASE", "USERID",
            "PASSWORD");
    proc_stmt = con.prepareCall("{ call someStoredProc() }");

    proc_stmt.executeQuery();

}

From source file:Example.java

public static void main(String args[]) {
    try {/*from  w  w  w.j a  v a  2s  .com*/
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

        Connection con = DriverManager.getConnection("jdbc:odbc:inventory", "", "");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM inventory ORDER BY price");

        ResultSetMetaData rsmd = rs.getMetaData();
        int numberOfColumns = rsmd.getColumnCount();
        int rowCount = 1;
        while (rs.next()) {
            for (int i = 1; i <= numberOfColumns; i++) {
                System.out.print(rs.getString(i) + " ");
            }
            rowCount++;
        }
        stmt.close();
        con.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/booltest", "booltest", "booltest");
    conn.prepareStatement("create table booltest (id bigint, truefalse varchar(10));").execute();
    PreparedStatement stmt = conn.prepareStatement("insert into booltest (id, truefalse) values (?, ?);");
    stmt.setLong(1, (long) 123);
    stmt.setBoolean(2, true);/*from w w  w . java 2s  . com*/
    stmt.execute();
    stmt.setLong(1, (long) 456);
    stmt.setBoolean(2, false);
    stmt.execute();
    ResultSet rs = conn.createStatement().executeQuery("select id, truefalse from booltest");
    while (rs.next()) {
        System.out.println(rs.getLong(1) + " => " + rs.getBoolean(2));
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String myDB = "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=c:/data.xls;"
            + "DriverID=22;READONLY=false";
    Connection con = DriverManager.getConnection(myDB, "", "");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES");
    printRs(rs);// w w w  .ja  v a2 s .  c o m

    rs.beforeFirst();

    while (rs.next()) {
        double newSalary = rs.getDouble(3) * 1.053;
        rs.updateDouble("salary", newSalary);
        rs.updateRow();
    }
    printRs(rs);
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    // DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
    final Connection c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system",
            "manager");
    String plsql = " declare " + "    p_id varchar2(20) := null; " + "    l_rc sys_refcursor;" + " begin "
            + "    p_id := ?; " + "    ? := 'input parameter was = ' || p_id;" + "    open l_rc for "
            + "        select 1 id, 'abc' name from dual; " + "    ? := l_rc;" + " end;";

    CallableStatement cs = c.prepareCall(plsql);
    cs.setString(1, "12345");
    cs.registerOutParameter(2, Types.VARCHAR);
    //  cs.registerOutParameter(3, OracleTypes.CURSOR);

    cs.execute();//  ww w  .  j  ava2  s .  c  om

    System.out.println("Result = " + cs.getObject(2));

    ResultSet cursorResultSet = (ResultSet) cs.getObject(3);
    while (cursorResultSet.next()) {
        System.out.println(cursorResultSet.getInt(1) + " " + cursorResultSet.getString(2));
    }
    cs.close();
    c.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    System.out.println("Oracle driver registered");
    Connection conn = null;//from   w w  w  . j  a v  a2 s .c  o  m
    conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orclh", "scott", "tiger");

    Statement stmt = conn.createStatement();
    ResultSet r = stmt.executeQuery("Select * from emp");
    while (r.next()) {
        String str = r.getString("ename");
        System.out.println(str);
    }
}