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

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con = null;//from  www.j a  va 2 s. com
    Statement stmt;
    PreparedStatement updateSales;
    PreparedStatement updateTotal;
    String updateString = "update COFFEES " + "set SALES = ? where COF_NAME = ?";

    String updateStatement = "update COFFEES " + "set TOTAL = TOTAL + ? where COF_NAME = ?";
    String query = "select COF_NAME, SALES, TOTAL from COFFEES";

    try {
        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");

        updateSales = con.prepareStatement(updateString);
        updateTotal = con.prepareStatement(updateStatement);
        int[] salesForWeek = { 175, 150, 60, 155, 90 };
        String[] coffees = { "Colombian", "French_Roast", "Espresso", "Colombian_Decaf", "French_Roast_Decaf" };
        int len = coffees.length;
        con.setAutoCommit(false);
        for (int i = 0; i < len; i++) {
            updateSales.setInt(1, salesForWeek[i]);
            updateSales.setString(2, coffees[i]);
            updateSales.executeUpdate();

            updateTotal.setInt(1, salesForWeek[i]);
            updateTotal.setString(2, coffees[i]);
            updateTotal.executeUpdate();
            con.commit();
        }

        con.setAutoCommit(true);

        updateSales.close();
        updateTotal.close();

        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {
            String c = rs.getString("COF_NAME");
            int s = rs.getInt("SALES");
            int t = rs.getInt("TOTAL");
            System.out.println(c + "     " + s + "    " + t);
        }

        stmt.close();
        con.close();

    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        if (con != null) {
            try {
                System.err.print("Transaction is being ");
                System.err.println("rolled back");
                con.rollback();
            } catch (SQLException excep) {
                System.err.print("SQLException: ");
                System.err.println(excep.getMessage());
            }
        }
    }
}

From source file:TransactionPairs.java

public static void main(String args[]) {

    String url = "jdbc:mySubprotocol:myDataSource";
    Connection con = null;/*from ww w  . ja va  2  s .  c  o  m*/
    Statement stmt;
    PreparedStatement updateSales;
    PreparedStatement updateTotal;
    String updateString = "update COFFEES " + "set SALES = ? where COF_NAME like ?";

    String updateStatement = "update COFFEES " + "set TOTAL = TOTAL + ? where COF_NAME like ?";
    String query = "select COF_NAME, SALES, TOTAL from COFFEES";

    try {
        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");

        updateSales = con.prepareStatement(updateString);
        updateTotal = con.prepareStatement(updateStatement);
        int[] salesForWeek = { 175, 150, 60, 155, 90 };
        String[] coffees = { "Colombian", "French_Roast", "Espresso", "Colombian_Decaf", "French_Roast_Decaf" };
        int len = coffees.length;
        con.setAutoCommit(false);
        for (int i = 0; i < len; i++) {
            updateSales.setInt(1, salesForWeek[i]);
            updateSales.setString(2, coffees[i]);
            updateSales.executeUpdate();

            updateTotal.setInt(1, salesForWeek[i]);
            updateTotal.setString(2, coffees[i]);
            updateTotal.executeUpdate();
            con.commit();
        }

        con.setAutoCommit(true);

        updateSales.close();
        updateTotal.close();

        stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {
            String c = rs.getString("COF_NAME");
            int s = rs.getInt("SALES");
            int t = rs.getInt("TOTAL");
            System.out.println(c + "     " + s + "    " + t);
        }

        stmt.close();
        con.close();

    } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        if (con != null) {
            try {
                System.err.print("Transaction is being ");
                System.err.println("rolled back");
                con.rollback();
            } catch (SQLException excep) {
                System.err.print("SQLException: ");
                System.err.println(excep.getMessage());
            }
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    ResultSet rs = null;//from w  w  w.j av a2s.  co m
    Connection conn = null;
    PreparedStatement pstmt = null;
    PreparedStatement pstmt2 = null;
    conn = getOracleConnection();
    String[] columnNames = { "id", "name", "content", "date_created" };
    Object[] inputValues = new Object[columnNames.length];
    inputValues[0] = new java.math.BigDecimal(100);
    inputValues[1] = new String("String Value");
    inputValues[2] = new String("This is my resume.");
    inputValues[3] = new Timestamp((new java.util.Date()).getTime());

    // prepare blob object from an existing binary column
    String insert = "insert into resume (id, name, content, date_created ) values(?, ?, ?, ?)";
    pstmt = conn.prepareStatement(insert);

    pstmt.setObject(1, inputValues[0]);
    pstmt.setObject(2, inputValues[1]);
    pstmt.setObject(3, inputValues[2]);
    pstmt.setObject(4, inputValues[3]);

    pstmt.executeUpdate();
    String query = "select id, name, content, date_created from resume where id=?";

    pstmt2 = conn.prepareStatement(query);
    pstmt2.setObject(1, inputValues[0]);
    rs = pstmt2.executeQuery();
    Object[] outputValues = new Object[columnNames.length];
    if (rs.next()) {
        for (int i = 0; i < columnNames.length; i++) {
            outputValues[i] = rs.getObject(i + 1);
        }
    }
    System.out.println("id=" + ((java.math.BigDecimal) outputValues[0]).toString());
    System.out.println("name=" + ((String) outputValues[1]));
    System.out.println("content=" + ((Clob) outputValues[2]));
    System.out.println("date_created=" + ((java.sql.Date) outputValues[3]).toString());

    rs.close();
    pstmt.close();
    pstmt2.close();
    conn.close();

}

From source file:com.plexobject.testplayer.dao.jdbc.JdbcUtil.java

static void attemptClose(PreparedStatement o) {
    try {/*from  w w  w.j  av a  2 s.c  om*/
        if (o != null)
            o.close();
    } catch (Exception e) {
        log.error("Failed to close " + o, e);
    }
}

From source file:de.unibayreuth.bayceer.bayeos.xmlrpc.ConnectionPool.java

/** Sets the user id for the current session
 *///  www  .  j  a  v a  2  s. c o  m
private static void setUserId(Connection con, int Id) throws SQLException {
    PreparedStatement st = con.prepareStatement("select set_userid(?)");
    st.setInt(1, Id);
    st.execute();
    st.close();
}

From source file:Main.java

/**
 * <p>// w w w.  j  a  v a2  s .  c om
 * Execute a DDL statement
 * </p>
 */
public static void executeDDL(String ddl) throws SQLException {
    Connection conn = getLocalConnection();

    // now register the function
    print(ddl);
    try {
        PreparedStatement createStatement = conn.prepareStatement(ddl);

        createStatement.execute();
        createStatement.close();
    } catch (SQLException t) {
        SQLException s = new SQLException("Could not execute DDL:\n" + ddl);

        s.setNextException(t);

        throw s;
    }
}

From source file:Main.java

/**
 * <p>/*from  w  w  w.j av  a2  s  .c o  m*/
 * Execute a DDL statement to drop an object if it exists. Swallow exceptions.
 * </p>
 */
public static void dropObject(String objectType, String objectName, boolean objectIfMissing)
        throws SQLException {
    String dropDDL = "drop " + objectType + " " + objectName;
    Connection conn = getLocalConnection();

    // Drop the object if it does exist. Swallow exception if it doesn't.
    print(dropDDL);
    try {
        PreparedStatement dropStatement = conn.prepareStatement(dropDDL);

        dropStatement.execute();
        dropStatement.close();
    } catch (SQLException s) {
        if (objectIfMissing) {
            throw s;
        }
    }
}

From source file:com.cgdecker.guice.jdbc.Hsqldb.java

private static void setUpDatabase(DataSource dataSource) {
    Connection conn = null;//from  w  w w  .ja  v a 2s  .  c o  m
    try {
        conn = dataSource.getConnection();
        PreparedStatement pS = conn.prepareStatement("SET DATABASE TRANSACTION CONTROL MVCC");
        pS.executeUpdate();
        pS.close();

        pS = conn.prepareStatement("DROP TABLE foo IF EXISTS");
        pS.executeUpdate();
        pS.close();

        pS = conn.prepareStatement("CREATE TABLE foo ( id INTEGER, name VARCHAR(100) )");
        pS.executeUpdate();
        pS.close();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.alibaba.napoli.metamorphosis.client.consumer.storage.JDBCUtils.java

public static void close(PreparedStatement preparedStatement) {
    if (preparedStatement != null) {
        try {/*from w w w .j  a  v  a2s.com*/
            preparedStatement.close();
        } catch (SQLException e) {
            log.error("Close PreparedStatement failed", e);
        }

    }
}

From source file:net.sf.sprockets.sql.Statements.java

/**
 * Execute the insert, update, or delete statement, get the number of rows affected, and close
 * the statement./*from w w  w  .  j a v  a  2  s  . co  m*/
 * 
 * @param stmt
 *            must already have parameters set
 * @since 1.4.0
 */
public static int update(PreparedStatement stmt) throws SQLException {
    int rows = stmt.executeUpdate();
    stmt.close();
    return rows;
}