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:UpdateableRs.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 ww. ja  v  a  2 s .c om

    rs.beforeFirst();

    while (rs.next()) {
        double newSalary = rs.getDouble("salary") * 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 {
    String db_file_name_prefix = "c:\\mydbdir\\mydb";

    Connection con = null;//w w  w  .  j a  v a 2  s.  c  o m
    Class.forName("org.hsqldb.jdbcDriver");

    con = DriverManager.getConnection("jdbc:hsqldb:file:" + db_file_name_prefix, // filenames
            "sa", // username
            ""); // password

    Statement statement = con.createStatement();
    ResultSet rs = statement.executeQuery("SELECT * FROM \"User\"");
    while (rs.next()) {
        System.out.print("ID: " + rs.getString("ID"));
        System.out.print(" first name: " + rs.getString("firstname"));
        System.out.println(" last name: " + rs.getString("lastname"));
    }
    statement.close();
    con.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driverName = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "root";

    Class.forName(driverName).newInstance();
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", userName,
            password);//from www. j  a  v  a 2 s.  c o  m

    Statement st = con.createStatement();
    st.executeUpdate("DROP TABLE Employee1");
    con.close();
}

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 dbConn = DriverManager.getConnection(URL, "user", "pass");
    PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));
    DriverManager.setLogWriter(w);
    dbConn.close();//from  w  ww .j a  v  a 2 s . c  o m

    PreparedStatement prepstmt;
    prepstmt = dbConn.prepareStatement("SELECT id FROM employee");
    prepstmt.execute();
    prepstmt.close();
    dbConn.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery("DESCRIBE myTable");
    ResultSetMetaData md = rs.getMetaData();
    int col = md.getColumnCount();
    for (int i = 1; i <= col; i++) {
        String col_name = md.getColumnName(i);
        System.out.println(col_name);
    }/*from   w ww . jav a 2  s. c  om*/
    DatabaseMetaData dbm = con.getMetaData();
    ResultSet rs1 = dbm.getColumns(null, "%", "myTable", "%");
    while (rs1.next()) {
        String col_name = rs1.getString("COLUMN_NAME");
        String data_type = rs1.getString("TYPE_NAME");
        int data_size = rs1.getInt("COLUMN_SIZE");
        int nullable = rs1.getInt("NULLABLE");
        System.out.println(col_name + " " + data_type + "(" + data_size + ")");
        if (nullable == 1) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection connection = null;
    try {/*from ww w .ja  v a2  s. co  m*/
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        connection = DriverManager.getConnection("jdbc:sqlserver://MYSERVER;databaseName=MYDATABASE", "USERID",
                "PASSWORD");

        connection.setAutoCommit(false);

        Statement statement = connection.createStatement();

        statement.executeUpdate("UPDATE Table1 SET Value = 1 WHERE Name = 'foo'");
        statement.executeUpdate("UPDATE Table2 SET Value = 2 WHERE Name = 'bar'");

        connection.commit();

    } catch (SQLException ex) {
        connection.rollback();
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String connection = "jdbc:mysql://localhost:3306/YourDBName";
    String user = "root";
    String password = "root";
    Class.forName(driver);/* ww w . j  a v  a2s  .  c  o  m*/
    Connection con = DriverManager.getConnection(connection, user, password);
    if (!con.isClosed()) {
        con.close();
    }
}

From source file:MainClass.java

public static void main(String[] arguments) {
    String data = "jdbc:odbc:YourSettings";
    try {//from   w  w  w.j  a  v  a2  s.  c o m
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection conn = DriverManager.getConnection(data, "", "");
        Statement st = conn.createStatement();
        ResultSet rec = st
                .executeQuery("SELECT * FROM Coal WHERE (Country='" + arguments[0] + "') ORDER BY Year");
        while (rec.next()) {
            System.out.println(rec.getString(1) + "\t" + rec.getString(2) + "\t\t" + rec.getString(3) + "\t"
                    + rec.getString(4));
        }
        st.close();
    } catch (Exception e) {
        System.out.println("Error: " + e.toString() + e.getMessage());
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "root";

    Class.forName(driver);/*from w ww.  j  a v a 2s  . c o  m*/
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass);

    DatabaseMetaData dbm = con.getMetaData();
    String[] types = { "TABLE" };
    ResultSet rs = dbm.getTables(null, null, "%", types);
    System.out.println("Table name:");
    while (rs.next()) {
        String table = rs.getString("TABLE_NAME");
        System.out.println(table);
        con.close();
    }

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "root";

    Class.forName(driver).newInstance();
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", user, pass);

    Statement st = con.createStatement();
    ResultSet res = st.executeQuery("SELECT * FROM  emp");
    while (res.next()) {
        int i = res.getInt("ID");
        String s = res.getString("name");
        System.out.println(i + "\t\t" + s);
    }//from   www  . j av  a  2s.  c  o m
    con.close();
}