Java SQL Update executeUpdateSP(Connection conn, String spName, Object... sqlParameters)

Here you can find the source of executeUpdateSP(Connection conn, String spName, Object... sqlParameters)

Description

execute stored procedure

License

Open Source License

Parameter

Parameter Description
conn : the connection that got the Stored Procedure
spName : stored peocedure call query e.g.: {call sp(?)}
sqlParameters : list filled with the stored procedure parameters

Return

: resultSet filled with the stored Procedure output

Declaration

public static int executeUpdateSP(Connection conn, String spName, Object... sqlParameters) throws SQLException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.sql.*;

public class Main {
    /** execute stored procedure
     *@param conn : the connection that got the Stored Procedure
     *@param spName : stored peocedure call query e.g.: {call sp(?)}
     *@param sqlParameters : list filled with the stored procedure parameters
     *@return : resultSet filled with the stored Procedure output
     *//*ww  w  .  jav a  2s.  c  o  m*/
    public static int executeUpdateSP(Connection conn, String spName, Object... sqlParameters) throws SQLException {
        //setNullableParameters(sqlParameters);
        spName = getQueryString(spName, sqlParameters);
        int rs = 0;
        CallableStatement cs = null;
        if (conn != null) {
            cs = conn.prepareCall(spName);
            if (sqlParameters != null) {
                for (int i = 1; i <= sqlParameters.length; i++) {
                    cs.setObject(i, sqlParameters[i - 1]);
                }
            }
            rs = cs.executeUpdate();
        }
        return rs;
    }

    public static String getQueryString(String spName, Object[] sqlParameters) {
        String spName1 = "{call  " + spName + "(";
        for (int i = 0; i < sqlParameters.length; i++) {
            spName1 += "?";
            if (i < sqlParameters.length - 1) {
                spName1 += ",";
            }
        }
        spName1 += ")}";
        return spName1;
    }
}

Related

  1. executeUpdate(String queryStatement, Connection database)
  2. ExecuteUpdate(String sql, List paras)
  3. executeUpdateNoCommit(Connection c, String query)
  4. executeUpdatePreparedStm(Connection conn, String preparedStm, Object... sqlParameters)
  5. ExecuteUpdateQuery(String query, Connection con)
  6. ExecuteUpdateStmt(Connection conn, PreparedStatement pstmt, PreparedStatement lockStatement, String tablename)