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 = getConnection();
    conn.setAutoCommit(false);/*www.j  ava 2 s . co m*/
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int, name VARCHAR(30) );");

    String INSERT_RECORD = "select * from survey where id < ?";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);

    pstmt.setInt(1, 1);

    pstmt.setFetchSize(200);

    ResultSet rs = pstmt.executeQuery();

    outputResultSet(rs);

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

From source file:TestOCINet8App.java

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

    Class.forName("oracle.jdbc.driver.OracleDriver");
    // or you can use:
    //  DriverManager.registerDriver(
    //   new oracle.jdbc.driver.OracleDriver());
    Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@(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 OCI driver tester '||USER||'!' result from dual");
    while (rset.next())
        System.out.println(rset.getString(1));
    rset.close();//from w w w.  j a  v  a  2  s  .  co m
    stmt.close();
    conn.close();
}

From source file:DemoDisplayBlobFromDatabase.java

public static void main(String args[]) throws Exception {
    Connection conn = null;
    ResultSet rs = null;// w  ww .j ava 2  s. c  o  m
    PreparedStatement pstmt = null;
    String query = "SELECT blob_column FROM blob_table WHERE id = ?";

    try {
        conn = getConnection();
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, "0001");
        rs = pstmt.executeQuery();
        rs.next();
        // materialize binary data onto client
        java.sql.Blob blob = rs.getBlob(1);
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}

From source file:DemoPreparedStatementSetURL.java

public static void main(String[] args) throws Exception {
    String id = "0001";
    String urlValue = "http://www.java2s.com";
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {// ww  w  . ja  va 2 s.c  om
        conn = getConnection();
        String query = "insert into url_table(id, url) values(?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, id);
        pstmt.setURL(2, new java.net.URL(urlValue));
        // execute query, and return number of rows created
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } finally {
        pstmt.close();
        conn.close();
    }
}

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);
    conn.setAutoCommit(false);/* w  w  w .  java2  s  .c  om*/

    String sql = "INSERT INTO documents (name, description, data) VALUES (?, ?, ?)";
    PreparedStatement stmt = conn.prepareStatement(sql);
    stmt.setString(1, "a.txt");
    stmt.setString(2, "b");

    File data = new File("C:\\a.txt");
    FileReader reader = new FileReader(data);
    stmt.setCharacterStream(3, reader, (int) data.length());
    stmt.execute();

    conn.commit();
    reader.close();
    conn.close();

}

From source file:edu.lternet.pasta.dml.database.pooling.DatabaseConnectionPoolFactory.java

public static void main(String arg[]) {
    Connection conn = null;
    try {//w  w  w  .  j  av a  2 s  .  com
        conn = DatabaseConnectionPoolFactory.getDatabaseConnectionPoolInterface().getConnection();
        log.debug("conn=" + conn);

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ConnectionNotAvailableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();

    stmt.executeUpdate("create table survey (id int, name BINARY);");

    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);

    pstmt.setBytes(1, "asdfasdf".getBytes());
    pstmt.executeUpdate();/*from  w  w  w.j av  a2s  .  co m*/

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.println(rs.getBytes(2));
    }

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

From source file:TestSSL.java

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

    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

    Properties prop = new Properties();
    prop.setProperty("user", "scott");
    prop.setProperty("password", "tiger");
    // THIS DOES NOT WORK YET
    prop.setProperty("oracle.net.ssl_cipher_suites",
            "(ssl_rsa_export_with_rc4_40_md5, ssl_rsa_export_with_des40_cbc_sha)");
    prop.setProperty("oracle.net.ssl_client_authentication", "false");
    prop.setProperty("oracle.net.ssl_version", "3.0");
    prop.setProperty("oracle.net.encryption_client", "REJECTED");
    prop.setProperty("oracle.net.crypto_checksum_client", "REJECTED");
    Connection conn = DriverManager.getConnection(
            "jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCPS)(HOST = dssw2k01)(PORT = 2484))) (CONNECT_DATA = (SERVICE_NAME = DSSW2K01)))",
            prop);/*from www.j  a  v a 2s  . c o m*/
    Statement stmt = conn.createStatement();
    ResultSet rset = stmt
            .executeQuery("select 'Hello Thin driver SSL " + "tester '||USER||'!' result from dual");
    while (rset.next())
        System.out.println(rset.getString(1));
    rset.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

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

    stmt.executeUpdate("create table survey (id int, name CHAR(5) );");

    stmt.executeUpdate("INSERT INTO survey(id, name)VALUES(111, '123456789')");

    // since there were no errors, commit
    conn.commit();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    outputResultSet(rs);

    stmt.executeUpdate("drop table survey");

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

From source file:jp.co.opentone.bsol.linkbinder.CorresponBodyUpdater.java

/**
 * @param args/*from ww  w . j  ava  2s.com*/
 */
public static void main(String[] args) throws Exception {

    Connection con = getConnection();
    PreparedStatement stmt = null;
    boolean success = true;
    try {
        con.setAutoCommit(false);

        stmt = con.prepareStatement("UPDATE correspon SET body = ? WHERE id = ?");
        execute(stmt);

        success = true;
    } finally {
        if (success) {
            con.commit();
        } else {
            con.rollback();
        }

        if (stmt != null)
            stmt.close();
        con.close();
    }

}