Example usage for java.sql PreparedStatement close

List of usage examples for java.sql PreparedStatement close

Introduction

In this page you can find the example usage for java.sql PreparedStatement close.

Prototype

void close() throws SQLException;

Source Link

Document

Releases this Statement object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

Usage

From source file:DemoDisplayBlobFromDatabase.java

public static void main(String args[]) throws Exception {
    Connection conn = null;/*from   w ww.  ja  va  2  s.c  om*/
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    String query = "SELECT blob_column FROM blob_table WHERE id = ?";

    try {
        conn = getConnection();
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, "0001");
        rs = pstmt.executeQuery();
        rs.next();
        // materialize binary data onto client
        java.sql.Blob blob = rs.getBlob(1);
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}

From source file:DemoPreparedStatementSetURL.java

public static void main(String[] args) throws Exception {
    String id = "0001";
    String urlValue = "http://www.java2s.com";
    Connection conn = null;/* ww w . j av  a  2  s .co  m*/
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String query = "insert into url_table(id, url) values(?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, id);
        pstmt.setURL(2, new java.net.URL(urlValue));
        // execute query, and return number of rows created
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:jp.co.opentone.bsol.linkbinder.CorresponBodyUpdater.java

/**
 * @param args//from ww  w  .j  a va 2s  . com
 */
public static void main(String[] args) throws Exception {

    Connection con = getConnection();
    PreparedStatement stmt = null;
    boolean success = true;
    try {
        con.setAutoCommit(false);

        stmt = con.prepareStatement("UPDATE correspon SET body = ? WHERE id = ?");
        execute(stmt);

        success = true;
    } finally {
        if (success) {
            con.commit();
        } else {
            con.rollback();
        }

        if (stmt != null)
            stmt.close();
        con.close();
    }

}

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 .  j  av a 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:UpdateRecordsUsingPreparedStatement.java

public static void main(String[] args) throws Exception {
    Connection conn = null;//from   w w  w. j  a  va  2 s  .  co m
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String query = "update dept set DEPT_LOC = ? where DEPT_NUM = ? ";
        pstmt = conn.prepareStatement(query); // create a statement
        pstmt.setString(1, "deptLocation"); // set input parameter 1
        pstmt.setInt(2, 1001); // set input parameter 2
        pstmt.executeUpdate(); // execute update statement
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:DemoPreparedStatementSetDate.java

public static void main(String[] args) throws Exception {
    Connection conn = null;//  w w  w .j a  va  2  s  .  com
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String query = "insert into date_table(id, date_column) values(?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, "0001");
        java.sql.Date date = getCurrentJavaSqlDate();
        pstmt.setDate(2, date);

        // execute query, and return number of rows created
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String WRITE_OBJECT_SQL = "BEGIN " + "  INSERT INTO java_objects(object_id, object_name, object_value) "
            + "  VALUES (?, ?, empty_blob()) " + "  RETURN object_value INTO ?; " + "END;";
    String READ_OBJECT_SQL = "SELECT object_value FROM java_objects WHERE object_id = ?";

    Connection conn = getOracleConnection();
    conn.setAutoCommit(false);/*  ww w.j ava  2  s .c  o m*/
    List<Object> list = new ArrayList<Object>();
    list.add("This is a short string.");
    list.add(new Integer(1234));
    list.add(new java.util.Date());

    // write object to Oracle
    long id = 0001;
    String className = list.getClass().getName();
    CallableStatement cstmt = conn.prepareCall(WRITE_OBJECT_SQL);

    cstmt.setLong(1, id);
    cstmt.setString(2, className);

    cstmt.registerOutParameter(3, java.sql.Types.BLOB);

    cstmt.executeUpdate();
    BLOB blob = (BLOB) cstmt.getBlob(3);
    OutputStream os = blob.getBinaryOutputStream();
    ObjectOutputStream oop = new ObjectOutputStream(os);
    oop.writeObject(list);
    oop.flush();
    oop.close();
    os.close();

    // Read object from oracle
    PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL);
    pstmt.setLong(1, id);
    ResultSet rs = pstmt.executeQuery();
    rs.next();
    InputStream is = rs.getBlob(1).getBinaryStream();
    ObjectInputStream oip = new ObjectInputStream(is);
    Object object = oip.readObject();
    className = object.getClass().getName();
    oip.close();
    is.close();
    rs.close();
    pstmt.close();
    conn.commit();

    // de-serialize list a java object from a given objectID
    List listFromDatabase = (List) object;
    System.out.println("[After De-Serialization] list=" + listFromDatabase);
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String id = "0001";
    float floatValue = 0001f;
    double doubleValue = 1.0001d;
    Connection conn = null;//from   w  ww  . j  a v a2 s.  c  om
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        String query = "insert into double_table(id, float_column, double_column) values(?, ?, ?)";
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, id);
        pstmt.setFloat(2, floatValue);
        pstmt.setDouble(3, doubleValue);
        // execute query, and return number of rows created
        int rowCount = pstmt.executeUpdate();
        System.out.println("rowCount=" + rowCount);
    } finally {
        pstmt.close();
        conn.close();
    }
}

From source file:DeliverWork.java

public static void main(String[] args) throws UnsupportedEncodingException, MessagingException {
    JdbcFactory jdbcFactoryChanye = new JdbcFactory(
            "jdbc:mysql://127.0.0.1:3306/fp_guimin?useUnicode=true&characterEncoding=UTF-8", "guimin_db",
            "guimin@98fhi3p2hUFHfdfoi", "com.mysql.jdbc.Driver");
    connection = jdbcFactoryChanye.getConnection();
    Map<String, String> map = new HashMap();

    try {/*from  w  w  w.j av a2s  . c  om*/
        PreparedStatement ps = connection.prepareStatement(selectAccount);
        ResultSet resultSet = ps.executeQuery();
        while (resultSet.next()) {
            map.put(resultSet.getString(1), resultSet.getString(2));
        }
        ps.close();
        map.forEach((k, v) -> {
            syncTheFile(k, v);
        });
        connection.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:DemoDisplayBinaryDataFromDatabase.java

public static void main(String args[]) throws Exception {
    Connection conn = null;/* www .ja va2s  .co  m*/
    ResultSet rs = null;
    PreparedStatement pstmt = null;
    String query = "SELECT raw_column, long_raw_column FROM binary_table WHERE id = ?";
    try {
        conn = getConnection();
        Object[] results = new Object[2];
        pstmt = conn.prepareStatement(query);
        pstmt.setString(1, "0001");
        rs = pstmt.executeQuery();
        rs.next();
        // materialize binary data onto client
        results[0] = rs.getBytes("RAW_COLUMN");
        results[1] = rs.getBytes("LONG_RAW_COLUMN");
    } finally {
        rs.close();
        pstmt.close();
        conn.close();
    }
}