Example usage for java.sql CallableStatement executeUpdate

List of usage examples for java.sql CallableStatement executeUpdate

Introduction

In this page you can find the example usage for java.sql CallableStatement executeUpdate.

Prototype

int executeUpdate() throws SQLException;

Source Link

Document

Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such as a DDL statement.

Usage

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);/*w w  w.j  a  va  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[] argv) throws Exception {
    Class.forName(DB_DRIVER);//from  w w  w  .  j a va  2s .  c o  m
    Connection dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);

    CallableStatement callableStatement = null;
    String insertStoreProc = "{call insertPERSON(?,?,?,?)}";

    java.util.Date today = new java.util.Date();
    callableStatement = dbConnection.prepareCall(insertStoreProc);
    callableStatement.setInt(1, 1000);
    callableStatement.setString(2, "name");
    callableStatement.setString(3, "system");
    callableStatement.setDate(4, new java.sql.Date(today.getTime()));

    callableStatement.executeUpdate();
    System.out.println("Record is inserted into PERSON table!");
    callableStatement.close();
    dbConnection.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Class.forName(DB_DRIVER);/*from   w w  w  .jav  a2  s . c om*/
    Connection dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);

    CallableStatement callableStatement = null;
    String getPERSONByUserIdSql = "{call getPERSONByUserId(?,?,?,?)}";
    callableStatement = dbConnection.prepareCall(getPERSONByUserIdSql);

    callableStatement.setInt(1, 10);
    callableStatement.registerOutParameter(2, java.sql.Types.VARCHAR);
    callableStatement.registerOutParameter(3, java.sql.Types.VARCHAR);
    callableStatement.registerOutParameter(4, java.sql.Types.DATE);

    callableStatement.executeUpdate();

    String userName = callableStatement.getString(2);
    String createdBy = callableStatement.getString(3);
    Date createdDate = callableStatement.getDate(4);

    System.out.println("UserName : " + userName);
    System.out.println("CreatedBy : " + createdBy);
    System.out.println("CreatedDate : " + createdDate);
    callableStatement.close();
    dbConnection.close();
}

From source file:StoredProcUtil.java

public static void addRaceEvent(String name, String location, String date) {

    if ((!check(name)) || (!check(location)) || (!check(date)))
        throw new IllegalArgumentException("Invalid param values passed to addRaceEvent()");

    Connection conn = null;//from   w  w  w  . jav a 2 s  . com

    try {

        conn = pool.getConnection();

        if (conn == null)
            throw new SQLException("Invalid Connection in addRaceEvent method");

        CallableStatement cs = null;

        //Create an instance of the CallableStatement
        cs = conn.prepareCall("{call addEvent (?,?,?)}");

        cs.setString(1, name);
        cs.setString(2, location);
        cs.setString(3, date);

        //Call the inherited PreparedStatement.executeUpdate() method
        cs.executeUpdate();

        // return the connection to the pool
        conn.close();

    } catch (SQLException sqle) {
    }

}

From source file:com.novartis.opensource.yada.util.YADAUtils.java

/**
 * One-liner execution of an SQL function.
 * @param sql the query to execute/*from  www.j  a  va  2  s .  com*/
 * @return the result of the function
 * @throws YADAConnectionException when the datasource is inaccessible
 * @throws YADASQLException when the JDBC configuration or execution fails
 */
public static int executeCallableStatement(String sql) throws YADAConnectionException, YADASQLException {
    CallableStatement c = null;
    int result = -1;
    try {
        c = ConnectionFactory.getConnectionFactory().getConnection(ConnectionFactory.YADA_APP).prepareCall(sql);
        result = c.executeUpdate();
    } catch (SQLException e) {
        throw new YADASQLException(e.getMessage(), e);
    } finally {
        if (c != null) {
            ConnectionFactory.releaseResources(c);
        }
    }
    return result;
}

From source file:org.squale.welcom.struts.bean.JCryptable.java

/**
 * Crypte les attributs crypts d'un Bean Date de cration : (07/10/2002 15:57:23)
 * /* w  ww .ja va 2s  .c om*/
 * @param conn la connection
 * @throws JCryptableException exception pouvant etre levee
 */
public void crypte(final java.sql.Connection conn) throws JCryptableException {
    try {
        final int nbAttr = cryptableAttr.length;

        // Pour chaque attr  crypter, appelle la procdure stocke de cryptage auprs de la BDD
        for (int i = 0; i < nbAttr; i++) {
            final String attr = cryptableAttr[i];

            final String value = (String) PropertyUtils.getProperty(this, attr);

            if ((value != null) && (value.length() > 0)) {
                final CallableStatement cs = conn.prepareCall("{? = call crypte_FCT(?)}");
                cs.registerOutParameter(1, java.sql.Types.VARCHAR);
                cs.setString(2, value);

                cs.executeUpdate();
                PropertyUtils.setProperty(this, attr, cs.getString(1));
                cs.close();
            }
        }
    } catch (final SQLException ex) {
        throw new JCryptableException(ex.getMessage());
    } catch (final IllegalAccessException ex) {
        throw new JCryptableException(ex.getMessage());
    } catch (final InvocationTargetException ex) {
        throw new JCryptableException(ex.getMessage());
    } catch (final NoSuchMethodException ex) {
        throw new JCryptableException(ex.getMessage());
    }
}

From source file:org.squale.welcom.struts.bean.JCryptable.java

/**
 * Decrypte les attributs crypts d'un Bean Date de cration : (07/10/2002 15:57:13)
 * /*w ww.j a v a 2  s. co m*/
 * @param conn la connection
 * @throws JCryptableException exception pouvant etre levee
 */
public void decrypte(final java.sql.Connection conn) throws JCryptableException {
    try {
        if (cryptableAttr != null) {
            final int nbAttr = cryptableAttr.length;

            // Pour chaque attr crypt, appelle la procdure stocke de dcryptage auprs de la BDD
            for (int i = 0; i < nbAttr; i++) {
                final String attr = cryptableAttr[i];

                final String value = (String) PropertyUtils.getProperty(this, attr);

                if ((value != null) && (value.length() > 0)) {
                    final CallableStatement cs = conn.prepareCall("{? = call decrypte_FCT(?)}");
                    cs.registerOutParameter(1, java.sql.Types.VARCHAR);
                    cs.setString(2, value);

                    cs.executeUpdate();
                    PropertyUtils.setProperty(this, attr, cs.getString(1));
                    cs.close();
                }
            }
        }
    } catch (final SQLException ex) {
        throw new JCryptableException(ex.getMessage());
    } catch (final IllegalAccessException ex) {
        throw new JCryptableException(ex.getMessage());
    } catch (final InvocationTargetException ex) {
        throw new JCryptableException(ex.getMessage());
    } catch (final NoSuchMethodException ex) {
        throw new JCryptableException(ex.getMessage());
    }
}

From source file:org.apache.ojb.broker.util.sequence.SequenceManagerStoredProcedureImpl.java

/**
 * Calls the stored procedure stored procedure throws an
 * error if it doesn't exist.// www.j  a  v  a2 s.c  om
 * @param broker
 * @param cld
 * @param sequenceName
 * @return
 * @throws LookupException
 * @throws SQLException
 */
protected long buildNextSequence(PersistenceBroker broker, ClassDescriptor cld, String sequenceName)
        throws LookupException, SQLException, PlatformException {
    CallableStatement cs = null;
    try {
        Connection con = broker.serviceConnectionManager().getConnection();
        cs = getPlatform().prepareNextValProcedureStatement(con, PROCEDURE_NAME, sequenceName);
        cs.executeUpdate();
        return cs.getLong(1);
    } finally {
        try {
            if (cs != null)
                cs.close();
        } catch (SQLException ignore) {
            // ignore it
        }
    }
}

From source file:com.splicemachine.derby.utils.SpliceAdminIT.java

@Test
public void testSplitTableTableDoesNotExist() throws Exception {
    try {/* w ww. j  av  a  2s  . c  o m*/
        CallableStatement cs = methodWatcher
                .prepareCall(format("call syscs_util.SYSCS_SPLIT_TABLE('%s','%s')", CLASS_NAME, "IAMNOTHERE"));
        cs.executeUpdate();
    } catch (SQLException e) {
        Assert.assertEquals("Message Mismatch", "Table 'SPLICEADMINIT.IAMNOTHERE' does not exist.  ",
                e.getMessage());
    }
}

From source file:org.squale.welcom.outils.jdbc.WJdbc.java

/**
 * Crype la chaine via l'appel  la Focntion decrypte_FCT
 * /*from w  ww  . ja v  a  2 s  . c o  m*/
 * @param text Texte a dcript
 * @return Chaine dcrypt
 * @throws SQLException Erreur SQL
 */
public String crypte(final String text) throws SQLException {
    CallableStatement cs = null;

    try {
        cs = conn.prepareCall("{? = call crypte_FCT(?)}");
        cs.registerOutParameter(1, java.sql.Types.VARCHAR);
        cs.setString(2, text);
        cs.executeUpdate();

        return cs.getString(1);
    } finally {
        if (cs != null) {
            cs.close();
        }
    }
}