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

public static void main(String[] args) throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    Class.forName(driver).newInstance();

    String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:ORCL";
    Connection conn = DriverManager.getConnection(jdbcUrl, "yourName", "mypwd");

    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM Employees");

    printColumnInfo(rs);//  w  w w .jav a  2s .  c om

    printColumnNames(rs);
    processRs(rs);

    rs = stmt.executeQuery("SELECT * FROM Location");
    printColumnInfo(rs);

    printColumnNames(rs);
    processRs(rs);
    conn.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, b CLOB);");

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO survey VALUES(1,?)");

    File file = new File("c:/Java_Dev/data.txt");
    FileInputStream fis = new FileInputStream(file);
    pstmt.setAsciiStream(1, fis, (int) file.length());
    pstmt.execute();/*w w  w  . j  a va 2  s  . co  m*/

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    try {/*from  ww w. ja  v a  2  s  . co  m*/
        DatabaseMetaData dbmd = conn.getMetaData();
        if (dbmd == null) {
            System.out.println("No");
        }
        if (dbmd.supportsStatementPooling()) {
            System.out.println("statement pooling is supported");
        } else {
            System.out.println("No");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        conn.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String dbURL = "jdbc:odbc:Companies";
    try {/*w  ww  .j a v a  2 s  .com*/
        // Load the jdbc-odbc bridge driver
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        // Enable logging
        DriverManager.setLogWriter(new PrintWriter((System.err)));

        System.out.println("Getting Connection");
        Connection conn = DriverManager.getConnection(dbURL, "user", "password");

        SQLWarning warn = conn.getWarnings();
        while (warn != null) {
            System.out.println("SQLState: " + warn.getSQLState());
            System.out.println("Message:  " + warn.getMessage());
            System.out.println("Vendor:   " + warn.getErrorCode());
            System.out.println("");
            warn = warn.getNextWarning();
        }

        conn.close();
    } catch (ClassNotFoundException e) {
        System.out.println("Can't load driver " + e);
    } catch (SQLException e) {
        System.out.println("Database access failed " + e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);

    String insert = "INSERT INTO orders (username, order_date) VALUES ('foobar', '2007-12-13')";
    Statement stmt = conn.createStatement();

    stmt.executeUpdate(insert, Statement.RETURN_GENERATED_KEYS);

    ResultSet keys = stmt.getGeneratedKeys();
    int lastKey = 1;
    while (keys.next()) {
        lastKey = keys.getInt(1);//from   w  ww .  j  a va2  s.  c om
    }
    System.out.println("Last Key: " + lastKey);
    conn.close();
}

From source file:DemoPreparedStatementSetBoolean.java

public static void main(String[] args) throws Exception {
    boolean booleanValue = true;
    Connection conn = null;
    PreparedStatement pstmt = null;
    try {//  ww  w. j  av a  2s .  c o m
        conn = getConnection();
        String query = "insert into boolean_table(id, boolean_column) values(?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, "0001");
        pstmt.setBoolean(2, booleanValue);
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:CheckJDBCInstallation_Oracle.java

public static void main(String[] args) {
    Connection conn = null;
    try {/*from  w  ww.ja  v a2  s.  c  o  m*/
        conn = getConnection();
        System.out.println("conn=" + conn);
        System.out.println("valid connection = " + isValidConnection(conn));
    } catch (Exception e) {
        // handle the exception
        e.printStackTrace();
        System.exit(1);
    } finally {
        // release database resources
        try {
            conn.close();
        } catch (Exception e) {
            // ignore
        }
    }
}

From source file:CheckJDBCInstallation_MySQL.java

public static void main(String[] args) {

    Connection conn = null;
    try {/*ww  w .  ja va  2 s  .  c  om*/
        conn = getConnection();
        System.out.println("conn=" + conn);
        System.out.println("valid connection = " + isValidConnection(conn));
    } catch (Exception e) {
        // handle the exception
        e.printStackTrace();
        System.exit(1);
    } finally {
        // release database resources
        try {
            conn.close();
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {//from  www  . ja  v a 2 s.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);
        Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);

        ResultSet rs = stmt.executeQuery("SELECT * from DUMMY");

        System.out.println(rs.getType());
        System.out.println(rs.getConcurrency());

        rs.deleteRow();
        rs.close();

        rs.close();
        stmt.close();
        connection.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:CountRows_Oracle.java

public static void main(String[] args) {
    Connection conn = null;
    try {/* w  w  w.j ava2 s.co  m*/
        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 {
        // release database resources
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}