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 con = DriverManager.getConnection("jdbc:h2:mem:");
    Statement s = con.createStatement();
    s.execute("CREATE TABLE Table1 (Column1 CLOB)");

    InputStream is = new FileInputStream("data.txt");
    Reader rdr = new InputStreamReader(is, StandardCharsets.ISO_8859_1);
    PreparedStatement ps = con.prepareStatement("INSERT INTO Table1 (Column1) VALUES (?)");
    ps.setCharacterStream(1, rdr);//w ww . j  a  v a  2s .co  m
    ps.executeUpdate();

    ResultSet rs = s.executeQuery("SELECT Column1 FROM Table1");
    int rowNumber = 0;
    while (rs.next()) {
        String str = rs.getString("Column1");
        System.out.println(String.format("Row %d: CLOB is %d character(s) long.", ++rowNumber, str.length()));
    }
    rs.close();
    con.close();
}

From source file:MainClass.java

public static void main(String[] args) {
    Connection connection = null;
    Statement statement = null;/*  w w w . j  a  v a 2  s  .  co m*/
    try {
        Class.forName("org.gjt.mm.mysql.Driver").newInstance();
        String url = "jdbc:mysql://localhost/mysql";
        connection = DriverManager.getConnection(url, "username", "password");
        statement = connection.createStatement();
        String hrappSQL = "CREATE DATABASE hrapp";
        statement.executeUpdate(hrappSQL);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
            }
        }
    }
}

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 BINARY );");

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

    // create some binary data
    String myData = "some string data ...";
    byte[] binaryData = myData.getBytes();

    // set value for the prepared statement
    pstmt.setBytes(1, binaryData);/*www .jav a2  s .c  o m*/

    // insert the data
    pstmt.executeUpdate();

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

    rs.close();
    stmt.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 BINARY );");

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

    // create some binary data
    String myData = "some string data ...";
    byte[] binaryData = myData.getBytes();

    // set value for the prepared statement
    pstmt.setBytes(1, binaryData);/*from  ww w .j  a  va 2 s .c  o  m*/

    // insert the data
    pstmt.executeUpdate();

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

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

From source file:DemoDataTruncation.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySQLConnection();
    Statement stmt = null;/*w ww.  j av  a2  s.c  o m*/
    try {
        stmt = conn.createStatement();
        stmt.executeUpdate("DELETE FROM animals_table");
        displayError(stmt.getWarnings());
        stmt.executeUpdate(
                "INSERT INTO animals_table(id, name)" + "VALUES(1, 'NameLongerThanColumnLengthInDatabase')");
        displayError(stmt.getWarnings());
    } catch (DataTruncation dt) {
        displayError(dt);
        dt.printStackTrace();
    } catch (SQLException se) {
        System.out.println("Database error message: " + se.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        stmt.close();
        conn.close();
    }
}

From source file:Main.java

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

    DatabaseMetaData mtdt = conn.getMetaData();

    System.out.println(mtdt.getProcedureTerm());

    ResultSet rs = mtdt.getProcedures(conn.getCatalog(), "%", "%");

    ResultSetMetaData rsmd = rs.getMetaData();
    int numCols = rsmd.getColumnCount();
    for (int i = 1; i <= numCols; i++) {
        if (i > 1)
            System.out.print(", ");
        System.out.print(rsmd.getColumnLabel(i));
    }//from  www  .  j  av  a2 s.  com
    System.out.println("");
    while (rs.next()) {
        for (int i = 1; i <= numCols; i++) {
            if (i > 1)
                System.out.print(", ");
            System.out.print(rs.getString(i));
        }
        System.out.println("");
    }
    conn.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Connection dbConnection = null;
    PreparedStatement preparedStatement = null;
    Class.forName(DB_DRIVER);//w  w w  . ja va 2s. c o m
    dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);

    String insertTableSQL = "INSERT INTO Person" + "(USER_ID, USERNAME, CREATED_BY, CREATED_DATE) VALUES"
            + "(?,?,?,?)";

    preparedStatement = dbConnection.prepareStatement(insertTableSQL);

    preparedStatement.setInt(1, 11);
    preparedStatement.setString(2, "yourName");
    preparedStatement.setString(3, "system");
    java.util.Date today = new java.util.Date();

    preparedStatement.setTimestamp(4, new java.sql.Timestamp(today.getTime()));

    preparedStatement.executeUpdate();

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

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = null;
    PreparedStatement pstmt = null;
    java.sql.Array sqlArray = null;
    conn = getOracleConnection();//w  w  w .  j a v a  2s  .  c  om
    //  ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("CHAR_ARRAY", conn);
    String[] content = { "v1", "v2", "v3", "v4" };
    // sqlArray = new oracle.sql.ARRAY(arrayDescriptor, conn, content);

    String query = "insert into CHAR_ARRAY_TABLE(id, array) values(?, ?)";

    pstmt = conn.prepareStatement(query);
    pstmt.setString(1, "0001");
    pstmt.setArray(2, sqlArray);

    int rowCount = pstmt.executeUpdate();
    System.out.println("rowCount=" + rowCount);
    System.out.println("--Demo_PreparedStatement_SetArray end--");
    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_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 cursor to the beginning, before the first row.
    // cursor position is 0.
    rs.beforeFirst();/*  www. j av a 2  s  .c o m*/
    rs.next();
    // Get data at cursor
    String id = rs.getString("id");
    System.out.println(id);

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

From source file:SerializeJavaObjects_MySQL.java

public static void main(String args[]) throws Exception {
    Connection conn = null;
    try {//w  w w .j av  a 2s  . c  om
        conn = getConnection();
        System.out.println("conn=" + conn);
        conn.setAutoCommit(false);
        List<Object> list = new ArrayList<Object>();
        list.add("This is a short string.");
        list.add(new Integer(1234));
        list.add(new Date());

        long objectID = writeJavaObject(conn, list);
        conn.commit();
        System.out.println("Serialized objectID => " + objectID);
        List listFromDatabase = (List) readJavaObject(conn, objectID);
        System.out.println("[After De-Serialization] list=" + listFromDatabase);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        conn.close();
    }
}