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

public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;//from w  w  w  .ja  v  a 2 s. c o  m
    try {
        Class.forName("org.gjt.mm.mysql.Driver").newInstance();
        String url = "jdbc:mysql://localhost/mysql";
        connection = DriverManager.getConnection(url, "username", "password");
        statement = connection.createStatement();
        String hrappSQL = "CREATE DATABASE hrapp";
        statement.executeUpdate(hrappSQL);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);

    DatabaseMetaData metadata = conn.getMetaData();
    ResultSet result = metadata.getProcedures(null, "JAVA", "%");
    while (result.next()) {
        System.out.println(result.getString("PROCEDURE_CAT") + " - " + result.getString("PROCEDURE_SCHEM")
                + " - " + result.getString("PROCEDURE_NAME"));
    }//from w  w  w.j a va2  s.c  o  m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    System.out.println("Count number of rows in a specific table!");
    Connection con = null;/*from w ww  .  j  av  a  2  s .c o  m*/
    int count = 0;
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial", "root", "root");

    Statement st = con.createStatement();
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    ResultSet res = st.executeQuery("SELECT COUNT(*) FROM EMP");
    while (res.next()) {
        count = res.getInt(1);
    }
    System.out.println("Number of row:" + count);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    String insert = "INSERT INTO orders (username, order_date) VALUES ('foobar', '2007-12-13')";
    Statement stmt = conn.createStatement();

    stmt.executeUpdate(insert, Statement.RETURN_GENERATED_KEYS);

    ResultSet keys = stmt.getGeneratedKeys();
    int lastKey = 1;
    while (keys.next()) {
        lastKey = keys.getInt(1);/*from   w  ww  .j  a  v a2s.co m*/
    }
    System.out.println("Last Key: " + lastKey);
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "jdbc4";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "root";

    Class.forName(driver).newInstance();

    Connection conn = DriverManager.getConnection(url + dbName, userName, password);
    Statement st = conn.createStatement();

    int rows = st.executeUpdate("INSERT INTO Copyemployee SELECT * FROM employee");
    if (rows == 0) {
        System.out.println("Don't add any row!");
    } else {/*from www.  jav  a2s  .  c o  m*/
        System.out.println(rows + " row(s)affected.");
        conn.close();
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;//from  w  w w. j  ava 2 s .c om
    try {
        Class.forName("org.gjt.mm.mysql.Driver").newInstance();
        String url = "jdbc:mysql://localhost/hrapp";
        connection = DriverManager.getConnection(url, "username", "password");

        statement = connection.createStatement();
        String employees1SQL = "UPDATE employees SET " + "num_dependants = 4 " + "WHERE employee_id = 123456";
        statement.executeUpdate(employees1SQL);
        String employees2SQL = "UPDATE employees SET " + "num_dependants = 4 " + "WHERE employee_id = 123457";
        statement.executeUpdate(employees2SQL);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
    }
}

From source file:CreateDatabase.java

public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;//from w  w  w.  j a v  a 2  s  . c  om
    try {
        Class.forName("org.gjt.mm.mysql.Driver").newInstance();
        String url = "jdbc:mysql://localhost/mysql";
        connection = DriverManager.getConnection(url, "username", "password");

        statement = connection.createStatement();
        String hrappSQL = "CREATE DATABASE hrapp";
        statement.executeUpdate(hrappSQL);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
            } // nothing we can do
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
            } // nothing we can do
        }
    }
}

From source file:SqlWarning.java

public static void main(String[] args) {
    try {//from  w ww  . j  a va 2  s .co  m
        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_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        SQLWarning sw = null;

        ResultSet rs = stmt.executeQuery("Select * from employees");
        sw = stmt.getWarnings();
        System.out.println(sw.getMessage());

        while (rs.next()) {
            System.out.println("Employee name: " + rs.getString(2));
        }
        rs.previous();
        rs.updateString("name", "Jon");

    } catch (SQLException se) {
        System.out.println("SQLException occurred: " + se.getMessage());

    } catch (Exception e) {
        e.printStackTrace();

    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Statement statement = null;/* w w w .ja  v  a2  s. co  m*/
    String url = "jdbc:odbc:databaseName";
    String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
    String username = "guest";
    String password = "guest";
    Class.forName(driver);
    Connection connection = DriverManager.getConnection(url, username, password);
    statement = connection.createStatement();
    System.out.println("Driver          :  " + driver);

    // Put each method call in a separate try block to execute them all
    System.out.print("\nMaximum rows    :");
    int maxRows = statement.getMaxRows();
    System.out.print(maxRows == 0 ? " No limit" : " " + maxRows);

    System.out.print("\nMax field size  :");
    int maxFieldSize = statement.getMaxFieldSize();
    System.out.print(maxFieldSize == 0 ? " No limit" : " " + maxFieldSize);

    System.out.print("\nTimeout          :");
    int queryTimeout = statement.getQueryTimeout();
    System.out.print(queryTimeout == 0 ? " No limit" : " " + queryTimeout);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {/*w  w  w. j a  v a 2s .  c  o m*/
        String url = "jdbc:odbc:yourdatabasename";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);

        DatabaseMetaData meta = connection.getMetaData();

        System.out.println("We are using " + meta.getDatabaseProductName());
        System.out.println("Version is " + meta.getDatabaseProductVersion());

        connection.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}