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:JDBCVersionApp.java

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

    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@dssnt01:1521:dssora01", "scott", "tiger");
    DatabaseMetaData meta = conn.getMetaData();
    System.out.println("You're using version: " + meta.getDriverVersion() + " of the Oracle JDBC driver.");
    conn.close();/*from w  ww  .j a  v a 2 s.  c om*/
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "jdbcMysql";
    String userName = "root";
    String password = "root";

    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection conn = DriverManager.getConnection(url + dbName, userName, password);
    Statement st = conn.createStatement();
    int rows = st.executeUpdate("INSERT INTO myTable SELECT * FROM jdbc4.Copyemployee");
    if (rows == 0) {
        System.out.println("Don't add any row!");
    } else {/* ww  w. j ava  2s  . co m*/
        System.out.println(rows + " row(s)affected.");
        conn.close();
    }
}

From source file:TestThinApp.java

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

    Class.forName("oracle.jdbc.driver.OracleDriver");
    //  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");

    Statement stmt = conn.createStatement();
    ResultSet rset = stmt.executeQuery("select 'Hello Thin driver tester '||USER||'!' result from dual");
    while (rset.next())
        System.out.println(rset.getString(1));
    rset.close();/*w ww . j a va  2s  . co m*/
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driver).newInstance();
    System.out.println("Connecting to database...");
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
    CallableStatement cstmt = conn.prepareCall("{call getEmpName (?,?)}");
    cstmt.setInt(1, 111111111);//from  ww  w  .  j  a  va 2  s. c  om
    cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
    cstmt.execute();
    String empName = cstmt.getString(2);
    System.out.println(empName);
    conn.close();

}

From source file:CallableStmt.java

    public static void main(String[] args) throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driver).newInstance();
    System.out.println("Connecting to database...");
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
    CallableStatement cstmt = conn.prepareCall("{call getEmpName (?,?)}");
    cstmt.setInt(1, 111111111);/* w ww  .  ja v  a 2s  . c o m*/
    cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);
    cstmt.execute();
    String empName = cstmt.getString(2);
    System.out.println(empName);
    conn.close();

}

From source file:ResultSetMetaDataExample.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con = DriverManager.getConnection("jdbc:odbc:Inventory", "", "");
    Statement stmt = con.createStatement();

    boolean notDone = true;
    String sqlStr = null;/*  w w w .jav a  2s.com*/
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    while (notDone) {
        sqlStr = br.readLine();
        if (sqlStr.startsWith("SELECT") || sqlStr.startsWith("select")) {
            ResultSet rs = stmt.executeQuery(sqlStr);
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            for (int x = 1; x <= columnCount; x++) {
                String columnName = rsmd.getColumnName(x);
                System.out.print(columnName);
            }
            while (rs.next()) {
                for (int x = 1; x <= columnCount; x++) {
                    if (rsmd.getColumnTypeName(x).compareTo("CURRENCY") == 0)
                        System.out.print("$");
                    String resultStr = rs.getString(x);
                    System.out.print(resultStr + "\t");
                }
            }
        } else if (sqlStr.startsWith("exit"))
            notDone = false;
    }
    stmt.close();
    con.close();
}

From source file:TestThinNet8App.java

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

    Class.forName("oracle.jdbc.driver.OracleDriver");
    //  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@(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 Thin driver tester '||USER||'!' result from dual");
    while (rset.next())
        System.out.println(rset.getString(1));
    rset.close();/*from   w w  w. java  2 s  .c o m*/
    stmt.close();
    conn.close();
}

From source file:ScrollableRs.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
    ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES");
    while (rs.next()) {
        printRow(rs);//from   w  w  w  . j a v  a 2  s .c  o  m
    }
    rs.afterLast();
    System.out.println("\"After-last-row\" = " + rs.isAfterLast());
    rs.beforeFirst();
    System.out.println("\"Before-first-row\" = " + rs.isBeforeFirst());
    rs.first();
    printRow(rs);
    rs.last();
    printRow(rs);
    rs.previous();
    printRow(rs);

    rs.next();
    printRow(rs);

    rs.absolute(3);
    printRow(rs);

    rs.relative(-2);
    printRow(rs);
    if (conn != null)
        conn.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from  w  w  w  .java  2s .c o m*/
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, username, password);
        System.out.println(conn.getMetaData().getDatabaseProductName());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    System.setProperty("oracle.net.tns_admin", "C:/app/product/11.2.0/client_1/NETWORK/ADMIN");
    String dbURL = "jdbc:oracle:thin:@ENTRY_FROM_TNSNAMES";

    Class.forName("oracle.jdbc.OracleDriver");

    Connection conn = DriverManager.getConnection(dbURL, "your_user_name", "your_password");

    System.out.println("Connection established");

    Statement stmt = conn.createStatement();

    ResultSet rs = stmt.executeQuery("SELECT dummy FROM dual");

    if (rs.next()) {
        System.out.println(rs.getString(1));
    }//from   ww  w .j  a  va  2s .  c  o  m
    stmt.close();
    conn.close();
}