Example usage for java.sql Connection close

List of usage examples for java.sql Connection close

Introduction

In this page you can find the example usage for java.sql Connection close.

Prototype

void close() throws SQLException;

Source Link

Document

Releases this Connection object's database and JDBC resources immediately instead of waiting for them to be automatically released.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();

    DatabaseMetaData meta = conn.getMetaData();
    ResultSet catalogs = meta.getCatalogs();

    while (catalogs.next()) {
        String catalog = catalogs.getString(1); //"TABLE_CATALOG"
        System.out.println("catalog: " + catalog);
    }/*from w  w w  . j  a  va  2  s  .  co  m*/

    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {/*from   w  ww  .j  av a2s.c om*/
        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);
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection c = DriverManager
            .getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/access.mdb");

    ResultSet rs = c.createStatement().executeQuery("select objet from Employee");

    while (rs.next()) {
        System.out.println(rs.getString("objet"));
    }/* www.j  a  va 2  s .c o  m*/
    rs.close();
    c.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);//from   www  .  j av a  2 s .  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:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();

    DatabaseMetaData meta = conn.getMetaData();
    String productName = meta.getDatabaseProductName();
    String productVersion = meta.getDatabaseProductVersion();
    System.out.println("productName:" + productName);
    System.out.println("productVersion:" + productVersion);

    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getHSQLConnection();

    DatabaseMetaData meta = conn.getMetaData();

    int txisolation = meta.getDefaultTransactionIsolation();
    System.out.println("Database default transaction isolation is " + txisolation + " ("
            + transactionIsolationToString(txisolation) + ").");
    conn.close();
}

From source file:Main.java

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

    PreparedStatement ps = connection.prepareStatement("UPDATE books SET title = ? WHERE id = ?");
    ps.setString(1, "Java");
    ps.setInt(2, 1);//from ww w  .  j  a  v  a 2s .  c om
    int rows = ps.executeUpdate();

    System.out.printf("%d row(s) updated!", rows);
    connection.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    conn.setAutoCommit(false);/* www.  ja  v a 2s  .c  o m*/
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int,myURL CHAR);");

    String str = conn.nativeSQL("insert into survey(id) values(02)");

    System.out.println(str);

    conn.commit();
    st.close();
    conn.close();
}

From source file:TestSupportsTransactions.java

public static void main(String[] args) throws Exception {
    Connection conn = getOracleConnection();
    try {/*from   ww  w  .  ja va  2s  .  c  om*/
        System.out.println("conn=" + conn);
        System.out.println("Transaction Support:" + supportsTransactions(conn));
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    } finally {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;
    Statement stmt = null;/*from   ww  w.  j av  a  2 s . c o m*/

    Class.forName(JDBC_DRIVER);
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    System.out.println("Deleting database...");
    stmt = conn.createStatement();

    String sql = "DROP DATABASE STUDENTS";
    stmt.executeUpdate(sql);

    stmt.close();
    conn.close();
}