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[] argv) throws Exception {
    Connection dbConnection = null;
    PreparedStatement preparedStatement = null;
    Class.forName(DB_DRIVER);//  ww w.  java 2  s  . c o  m
    dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
    String updateTableSQL = "UPDATE Person SET USERNAME = ? " + " WHERE USER_ID = ?";
    preparedStatement = dbConnection.prepareStatement(updateTableSQL);

    preparedStatement.setString(1, "newValue");
    preparedStatement.setInt(2, 1001);

    preparedStatement.executeUpdate();

    preparedStatement.executeUpdate();
    preparedStatement.close();
    dbConnection.close();

}

From source file:TestClassForNameNewInstanceApp.java

public static void main(String args[]) {

    try {//from   w  ww . j  ava  2  s.  c om
        Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    } catch (ClassNotFoundException e) {
        System.out.println("Oops! Can't find class oracle.jdbc.driver.OracleDriver");
        System.exit(1);
    } catch (IllegalAccessException e) {
        System.out.println("Uh Oh! You can't load oracle.jdbc.driver.OracleDriver");
        System.exit(2);
    } catch (InstantiationException e) {
        System.out.println("Geez! Can't instantiate oracle.jdbc.driver.OracleDriver");
        System.exit(3);
    }

    Connection conn = null;
    Statement stmt = null;
    ResultSet rset = null;
    try {
        conn = DriverManager.getConnection("jdbc:oracle:thin:@dssw2k01:1521:orcl", "scott", "tiger");

        stmt = conn.createStatement();
        rset = stmt.executeQuery("select 'Hello '||USER||'!' result from dual");
        while (rset.next())
            System.out.println(rset.getString(1));
        rset.close();
        rset = null;
        stmt.close();
        stmt = null;
        conn.close();
        conn = null;
    } catch (SQLException e) {
        System.out.println("Darn! A SQL error: " + e.getMessage());
    } finally {
        if (rset != null)
            try {
                rset.close();
            } catch (SQLException ignore) {
            }
        if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException ignore) {
            }
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ignore) {
            }
    }
}

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')");
    st = conn.createStatement();/* ww  w  .  j  a  v a 2 s .co m*/
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

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

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    String simpleProc = "{ call simpleproc(?) }";
    CallableStatement cs = conn.prepareCall(simpleProc);
    cs.registerOutParameter(1, java.sql.Types.INTEGER);
    cs.execute();/*from ww  w.jav  a 2s.  c o m*/
    int param1 = cs.getInt(1);
    System.out.println("param1=" + param1);
    ParameterMetaData pmeta = cs.getParameterMetaData();
    if (pmeta == null) {
        System.out.println("Vendor does not support ParameterMetaData");
    } else {
        System.out.println(pmeta.getParameterType(1));
    }
    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_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')");
    st = conn.createStatement();/* www  .ja va 2s.c o  m*/
    ResultSet rs = st.executeQuery("SELECT * FROM survey");

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

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String connectionURL = "jdbc:mysql://localhost:3306/test";
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection(connectionURL, "login", "password");
    Statement stmt = con.createStatement();
    stmt.execute("CREATE TRIGGER obs_update BEFORE UPDATE ON obs " //
            + "FOR EACH ROW "//
            + "BEGIN "//
            + "IF OLD.voided = 0 AND NEW.voided = 1 THEN "//
            + "   DELETE FROM emp WHERE id = OLD.obs_id; "//
            + "ELSE "//
            + "   UPDATE emp SET emp.revision_token = NOW() "//
            + "   WHERE NEW.id = emp.id; "//
            + "END IF; "//
            + "END;");
    con.close();
}

From source file:Main.java

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

    st.executeUpdate("create table survey (id int,myURL CHAR);");
    String INSERT_RECORD = "insert into survey(id, myURL) values(?, ?)";

    PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD);
    pstmt.setString(1, "1");
    pstmt.setURL(2, new URL("http://www.java2s.com"));

    pstmt.executeUpdate();/*  www  .j ava 2 s. c o  m*/

    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 {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES");
    printRs(rs);/*w  w w. j  a  v a 2 s. c om*/

    rs.beforeFirst();

    while (rs.next()) {
        double newSalary = rs.getDouble(3) * 1.053;
        rs.updateDouble("salary", newSalary);
        rs.updateRow();
    }
    printRs(rs);
    conn.close();
}

From source file:TestDebug_MySQL.java

public static void main(String[] args) {
    Connection conn = null;
    try {//from   ww  w .j ava2 s  . co m
        PrintWriter pw = new PrintWriter(new FileOutputStream("mysql_debug.txt"));
        DriverManager.setLogWriter(pw);
        conn = getConnection();
        String tableName = "myTable";
        System.out.println("tableName=" + tableName);
        System.out.println("conn=" + conn);
        System.out.println("rowCount=" + countRows(conn, tableName));
    } 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 = getHSQLConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    st.executeUpdate("create table survey (id int,name varchar);");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    ResultSet rs = null;//from w  ww. j av  a2 s .  c  om
    DatabaseMetaData meta = conn.getMetaData();
    rs = meta.getTableTypes();

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

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