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 {
    try {/*w ww  .jav a  2  s  .  co m*/
        Connection conn = getHSQLConnection();

        conn.setAutoCommit(false);
        Statement st = conn.createStatement();
        st.executeUpdate("create table survey (id int,name varchar(30));");
        st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
        st = conn.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM survey");

        outputResultSet(rs);

        rs.close();
        st.close();
        conn.close();
    } catch (SQLException sqle) {
        do { // loop through each exception
             // do something with each exception
            System.err.println("Exception occurred:\nMessage: " + sqle.getMessage());
            System.err.println("SQL state: " + sqle.getSQLState());
            System.err.println("Vendor code: " + sqle.getErrorCode() + "\n----------------");
        } while ((sqle = sqle.getNextException()) != null);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    conn.setAutoCommit(false);//w ww .j  a v  a 2s .  c  om
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (id int,myURL CHAR);");
    st.executeUpdate("insert into survey(id) values(01)");
    st.executeUpdate("insert into survey(id) values(02)");

    Savepoint mySavepoint = conn.setSavepoint("MYSAVEPOINT");

    st.executeUpdate("insert into survey(id) values(03)");
    conn.commit();

    conn.rollback(mySavepoint);

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {/*from  w w w.j  a  v  a 2s. c o  m*/
        Connection conn = getHSQLConnection();

        conn.setAutoCommit(false);
        Statement st = conn.createStatement();
        st.executeUpdate("create table survey (id int,name varchar(30));");
        st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
        st = conn.createStatement();
        ResultSet rs = st.executeQuery("SELECT * FROM survey");

        outputResultSet(rs);

        rs.close();
        st.close();
        conn.close();
    } catch (SQLException sqle) {
        String sqlMessage = sqle.getMessage();
        String sqlState = sqle.getSQLState();
        int vendorCode = sqle.getErrorCode();
        System.err.println("Exception occurred:");
        System.err.println("Message: " + sqlMessage);
        System.err.println("SQL state: " + sqlState);
        System.err.println("Vendor code: " + vendorCode + "\n----------------");
    }
}

From source file:TestConnectToMoreThanOneDatabase.java

public static void main(String[] args) {

    Connection oracleConn = null;
    Connection mysqlConn = null;/*from ww w  . j  a v a 2s .c om*/
    try {
        oracleConn = getOracleConnection();
        mysqlConn = getMySqlConnection();
        System.out.println("oracleConn=" + oracleConn);
        System.out.println("mysqlConn=" + mysqlConn);
    } catch (Exception e) {
        // handle the exception
        e.printStackTrace();
        System.exit(1);
    } finally {
        // release database resources
        try {
            oracleConn.close();
            mysqlConn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

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

    Connection conn = getHSQLConnection();

    conn.setAutoCommit(false);/* w  w w .  j a v a  2  s. c om*/
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st = conn.createStatement();
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    outputResultSet(rs);

    checkForWarning(rs.getWarnings());

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

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};"
            + "DBQ=C:\\Users\\Public\\uls\\ulsTest.mdb;" + "SystemDB=C:\\Users\\Public\\uls\\Security.mdw;"
            + "Uid=Gord;" + "Pwd=obfuscated;" + "ExtendedAnsiSQL=1;");

    String UID = "Tim";
    String oldPWD = "oldpassword";
    String newPWD = "I like Java";

    Statement s = conn.createStatement();
    s.execute("ALTER USER " + UID + " PASSWORD \"" + newPWD + "\" \"" + oldPWD + "\"");

    s.close();//from   ww w  .j av  a 2  s.  com
    conn.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. j  a v a2 s  . c  o m
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName(DRIVER);//from ww w. j a v  a  2s . co m
    Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    DatabaseMetaData metadata = connection.getMetaData();
    ResultSet resultSet = metadata.getColumns(null, null, "users", null);
    while (resultSet.next()) {
        String name = resultSet.getString("COLUMN_NAME");
        String type = resultSet.getString("TYPE_NAME");
        int size = resultSet.getInt("COLUMN_SIZE");

        System.out.println("Column name: [" + name + "]; type: [" + type + "]; size: [" + size + "]");
    }
    connection.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection conn = DriverManager
            .getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};"
                    + "Dbq=C://Book1.xlsx;");

    PreparedStatement s = conn.prepareStatement("SELECT * FROM [Sheet1$] WHERE [MetricMonth] = ?");
    s.setString(1, "Jul-2013");
    s.execute();//  w  ww  .j  a v a 2  s .c  om
    ResultSet rs = s.getResultSet();
    if (rs != null) {
        while (rs.next()) {
            System.out.println(rs.getInt("All"));
        }
    }
    s.close();

    conn.close();
}

From source file:Main.java

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

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

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

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    outputResultSet(rs);/*from w w  w . j a v a 2  s  .  com*/

    stmt.executeUpdate("delete from survey");

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

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