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

public static void main(String[] args) throws Exception {
    Connection conn1 = null;
    Connection conn2 = null;/*from   ww  w. j av  a 2  s  . com*/
    Connection conn3 = null;

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
    String jdbcUrl = "jdbc:odbc:authors";

    String user = "yourName";
    String pwd = "mypwd";

    conn1 = DriverManager.getConnection(jdbcUrl);
    if (conn1 != null) {
        System.out.println("Connection 1 successful!");
    }
    Properties prop = new Properties();
    prop.put("user", user);
    prop.put("password", pwd);

    conn2 = DriverManager.getConnection(jdbcUrl, prop);
    if (conn2 != null) {
        System.out.println("Connection 2 successful!");
    }
    conn3 = DriverManager.getConnection(jdbcUrl, user, pwd);
    if (conn3 != null) {
        System.out.println("Connection 3 successful!");
    }
    conn1.close();
    conn2.close();
    conn3.close();
    if (conn1.isClosed()) {
        System.out.println("Connection 1 is closed");
    }

    if (conn2.isClosed()) {
        System.out.println("Connection 2 is closed");
    }
    if (conn3.isClosed()) {
        System.out.println("Connection 3 is closed");
    }

    conn1.close();
    conn2.close();
    conn3.close();

}

From source file:TableTypes.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con;

    try {/*from   ww w.  ja va  2s  .  co  m*/
        Class.forName("myDriver.ClassName");

    } catch (java.lang.ClassNotFoundException e) {
        System.err.print("ClassNotFoundException: ");
        System.err.println(e.getMessage());
    }

    try {
        con = DriverManager.getConnection(url, "myLogin", "myPassword");

        DatabaseMetaData dbmd = con.getMetaData();
        String dbmsName = dbmd.getDatabaseProductName();
        ResultSet rs = dbmd.getTableTypes();
        System.out.print("The following types of tables are ");
        System.out.println("available in " + dbmsName + ":  ");

        while (rs.next()) {
            String tableType = rs.getString("TABLE_TYPE");
            System.out.println("    " + tableType);
        }

        rs.close();
        con.close();

    } catch (SQLException ex) {
        System.err.print("SQLException: ");
        System.err.println(ex.getMessage());
    }
}

From source file:DemoGetGeneratedKeysMySQL.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = null;//from ww  w .  j  a  va 2  s  .  c o m
    ResultSet rs = null;
    try {
        conn = getConnection();
        stmt = conn.createStatement();
        stmt.executeUpdate("insert into animals_table (name) values('newName')");
        rs = stmt.getGeneratedKeys();
        while (rs.next()) {
            ResultSetMetaData rsMetaData = rs.getMetaData();
            int columnCount = rsMetaData.getColumnCount();

            for (int i = 1; i <= columnCount; i++) {
                String key = rs.getString(i);
                System.out.println("key " + i + " is " + key);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    } finally {
        try {
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:SelectRecordsUsingPreparedStatement.java

public static void main(String[] args) {
    ResultSet rs = null;//  w  w  w  .j  a  va2s .  c  o  m
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String query = "select deptno, deptname, deptloc from dept where deptno > ?";

        pstmt = conn.prepareStatement(query); // create a statement
        pstmt.setInt(1, 1001); // set input parameter
        rs = pstmt.executeQuery();
        // extract data from the ResultSet
        while (rs.next()) {
            int dbDeptNumber = rs.getInt(1);
            String dbDeptName = rs.getString(2);
            String dbDeptLocation = rs.getString(3);
            System.out.println(dbDeptNumber + "\t" + dbDeptName + "\t" + dbDeptLocation);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            rs.close();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:DemoPreparedStatementSetClob.java

public static void main(String[] args) throws Exception {
    String id = "0001";
    String newID = "0002";

    ResultSet rs = null;//  w  w w.  ja v a 2 s  .  c o m
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        // begin transaction
        conn.setAutoCommit(false);
        String query1 = "select clob_column from clob_table where id = ?";
        pstmt = conn.prepareStatement(query1);
        pstmt.setString(1, id);
        rs = pstmt.executeQuery();
        rs.next();
        java.sql.Clob clob = (java.sql.Clob) rs.getObject(1);
        String query = "insert into clob_table(id, clob_column) values(?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, newID);
        pstmt.setClob(2, clob);

        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
        conn.commit();
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,null)");
    st = conn.createStatement();//  w  ww .  j ava 2s . c  o m
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    int rsConcurrency = rs.getConcurrency();
    if (rsConcurrency == java.sql.ResultSet.CONCUR_READ_ONLY) {
        System.out.println("java.sql.ResultSet.CONCUR_READ_ONLY");
    } else if (rsConcurrency == java.sql.ResultSet.CONCUR_UPDATABLE) {
        System.out.println("java.sql.ResultSet.CONCUR_UPDATABLE");
    } else {
        // it is an error

    }

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

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);

    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    st.executeUpdate("insert into survey (id,name ) values (2,null)");
    st.executeUpdate("insert into survey (id,name ) values (3,'Tom')");
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

    // move to the end of the result set
    rs.last();/*from w  w w .  j a va2 s  .  com*/

    // get the row number of the last row, which is also the row count
    int rowCount = rs.getRow();
    System.out.println(rowCount);

    // now you may move the cursor to the front of this ResultSet object,
    // just before the first row
    rs.beforeFirst();

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

From source file:Main.java

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

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

    String INSERT_RECORD = "insert into survey(id, name) values(?,?)";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
    pstmt.setString(1, "1");
    pstmt.setString(2, "name1");
    pstmt.addBatch();

    pstmt.setString(1, "2");
    pstmt.setString(2, "name2");
    pstmt.addBatch();

    // execute the batch
    int[] updateCounts = pstmt.executeBatch();

    checkUpdateCounts(updateCounts);

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

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

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

From source file:Main.java

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

    DatabaseMetaData dbmd = conn.getMetaData();

    ResultSet rs = dbmd.getTypeInfo();
    while (rs.next()) {
        String typeName = rs.getString("TYPE_NAME");
        short dataType = rs.getShort("DATA_TYPE");
        String createParams = rs.getString("CREATE_PARAMS");
        int nullable = rs.getInt("NULLABLE");
        boolean caseSensitive = rs.getBoolean("CASE_SENSITIVE");
        System.out.println("DBMS type " + typeName + ":");
        System.out.println("     java.sql.Types:  " + dataType);
        System.out.print("     parameters used to create: ");
        System.out.println(createParams);
        System.out.println("     nullable?:  " + nullable);
        System.out.print("     case sensitive?:  ");
        System.out.println(caseSensitive);
        System.out.println("");

    }//from w ww .  j  a  va2s.  c  om
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    Statement st = conn.createStatement();
    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");
    DatabaseMetaData meta = conn.getMetaData();
    ResultSet rs = meta.getExportedKeys(conn.getCatalog(), null, "survey");
    while (rs.next()) {
        String fkTableName = rs.getString("FKTABLE_NAME");
        String fkColumnName = rs.getString("FKCOLUMN_NAME");
        int fkSequence = rs.getInt("KEY_SEQ");
        System.out.println("getExportedKeys(): fkTableName=" + fkTableName);
        System.out.println("getExportedKeys(): fkColumnName=" + fkColumnName);
        System.out.println("getExportedKeys(): fkSequence=" + fkSequence);
    }//from  w ww .j a  v  a 2 s.c o m

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