下面列出了java.sql.Statement#SUCCESS_NO_INFO 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void executeSqls(BufferedWriter bw, List<String> tmp, long startTime, File file)
throws IOException {
try {
int[] result = statement.executeBatch();
for (int i = 0; i < result.length; i++) {
if (result[i] != Statement.SUCCESS_NO_INFO && i < tmp.size()) {
bw.write(tmp.get(i));
bw.newLine();
errorFlag = false;
}
}
statement.clearBatch();
tmp.clear();
} catch (SQLException e) {
bw.write(e.getMessage());
bw.newLine();
errorFlag = false;
System.out.println("Cannot execute sql because: " + e.getMessage());
}
}
private static void checkBatchSize(BufferedWriter bw, List<String> tmp)
throws SQLException, IOException {
if (count == BATCH_EXECUTE_COUNT) {
int[] result = statement.executeBatch();
for (int i = 0; i < result.length; i++) {
if (result[i] != Statement.SUCCESS_NO_INFO && i < tmp.size()) {
bw.write(tmp.get(i));
bw.newLine();
errorFlag = false;
}
}
statement.clearBatch();
count = 0;
tmp.clear();
}
}
public static void printResults(String query, int[] counts, HttpServletResponse response) throws IOException{
PrintWriter out = response.getWriter();
out.write("<!DOCTYPE html>\n<html>\n<body>\n<p>");
out.write("For query: " + ESAPI.encoder().encodeForHTML(query) + "<br>");
try {
if(counts.length > 0){
if(counts[0] == Statement.SUCCESS_NO_INFO){
out.write("The SQL query was processed successfully but the number of rows affected is unknown.");
System.out.println("The SQL query was processed successfully but the number of rows affected is unknown.");
}else if(counts[0] == Statement.EXECUTE_FAILED){
out.write("The SQL query failed to execute successfully and occurs only if a driver continues to process commands after a command fails");
System.out.println("The SQL query failed to execute successfully and occurs only if a driver continues to process commands after a command fails");
}else{
out.write("The number of affected rows are: " + counts[0]);
System.out.println("The number of affected rows are: " + counts[0]);
}
}
} finally {
out.write("</p>\n</body>\n</html>");
}
}
/**
* Execute the current batch of statements. If any exception occurs
* during execution, a {@link org.apache.phoenix.exception.BatchUpdateException}
* is thrown which includes the index of the statement within the
* batch when the exception occurred.
*/
@Override
public int[] executeBatch() throws SQLException {
int i = 0;
try {
int[] returnCodes = new int [batch.size()];
for (i = 0; i < returnCodes.length; i++) {
PhoenixPreparedStatement statement = batch.get(i);
returnCodes[i] = statement.execute(true) ? Statement.SUCCESS_NO_INFO : statement.getUpdateCount();
}
// If we make it all the way through, clear the batch
clearBatch();
return returnCodes;
} catch (Throwable t) {
throw new BatchUpdateExecution(t,i);
}
}
/**
* Execute the current batch of statements. If any exception occurs
* during execution, a {@link org.apache.phoenix.exception.BatchUpdateException}
* is thrown which includes the index of the statement within the
* batch when the exception occurred.
*/
@Override
public int[] executeBatch() throws SQLException {
int i = 0;
try {
int[] returnCodes = new int [batch.size()];
for (i = 0; i < returnCodes.length; i++) {
PhoenixPreparedStatement statement = batch.get(i);
returnCodes[i] = statement.execute(true) ? Statement.SUCCESS_NO_INFO : statement.getUpdateCount();
}
// Flush all changes in batch if auto flush is true
flushIfNecessary();
// If we make it all the way through, clear the batch
clearBatch();
return returnCodes;
} catch (Throwable t) {
throw new BatchUpdateExecution(t,i);
}
}
/**
* Executes the {@link BulkRequest} being hold by this state.
* @return an integer indicator for each executed request: Statement.SUCCESS_NO_INFO for success,
* else Statement.EXECUTE_FAILED)
*/
public int[] executeBulk(){
int[] result = new int[bulkList.size()];
SqlParser parser = new SqlParser();
for(int i=0; i<bulkList.size(); i++) try{
String sql = bulkList.get(i);
com.facebook.presto.sql.tree.Statement st = parser.createStatement(sql);
if(st instanceof DropTable){
this.execute(sql, (DropTable)st);
}else if(st instanceof DropView){
this.execute(sql, (DropView)st);
}else if(st instanceof CreateTable){
this.execute(sql, (CreateTable)st, this.statement.getConnection().getSchema());
}else if(st instanceof CreateTableAsSelect){
this.execute(sql, (CreateTableAsSelect)st, this.statement.getConnection().getSchema());
}else if(st instanceof CreateView){
this.execute(sql, (CreateView)st, this.statement.getConnection().getSchema());
}else if(st instanceof Delete){
this.execute(sql, (Delete)st, this.statement.getConnection().getSchema());
}else if(st instanceof Insert){
this.execute(sql, (Insert)st, this.statement.getConnection().getSchema());
}
result[i]= Statement.SUCCESS_NO_INFO;
}catch (Exception e){
result[i] = Statement.EXECUTE_FAILED;
}
this.clearBulk();
return result;
}
public static void printResults(String query, int[] counts, List<StringMessage> resp) throws IOException{
resp.add(new StringMessage("Message",
"For query: " + ESAPI.encoder().encodeForHTML(query) + "<br>"
));
try {
if(counts.length > 0){
if(counts[0] == Statement.SUCCESS_NO_INFO){
resp.add(new StringMessage("Message",
"The SQL query was processed successfully but the number of rows affected is unknown."
));
System.out.println("The SQL query was processed successfully but the number of rows affected is unknown.");
}else if(counts[0] == Statement.EXECUTE_FAILED){
resp.add(new StringMessage("Message",
"The SQL query failed to execute successfully and occurs only if a driver continues to process commands after a command fails"
));
System.out.println("The SQL query failed to execute successfully and occurs only if a driver continues to process commands after a command fails");
}else{
resp.add(new StringMessage("Message",
"The number of affected rows are: " + counts[0]
));
System.out.println("The number of affected rows are: " + counts[0]);
}
}
} finally {
resp.add(new StringMessage("Message",
"</p>\n</body>\n</html>"
));
}
}
@Override
public void insertBatch() {
int[] result = updateBatch();
for (int r : result) {
// Tolerate SUCCESS_NO_INFO for older versions of Oracle
if (r != 1 && r != Statement.SUCCESS_NO_INFO) {
throw new DatabaseException("Batch did not return the expected result: " + Arrays.toString(result));
}
}
}
/**
* Performs the batch for the given statement, and checks that the specified amount of rows have been changed.
*
* @param statement The prepared statement
* @param numRows The number of rows that should change
* @param table The changed table
*/
// GemStone changes BEGIN
private void executeBatch(PreparedStatement statement, int numRows,
Table table, DynaBean[] batchDynaBeans, Column[] autoIncrColumns)
throws DatabaseOperationException
/* (original code)
private void executeBatch(PreparedStatement statement, int numRows, Table table) throws DatabaseOperationException
*/
// GemStone changes BEGIN
{
if (statement != null)
{
try
{
Connection connection = statement.getConnection();
beforeInsert(connection, table);
int[] results = statement.executeBatch();
// GemStone changes BEGIN
if (batchDynaBeans != null) {
handleAutoIncrementValuesForBatch(statement,
autoIncrColumns, batchDynaBeans, numRows);
}
// GemStone changes END
closeStatement(statement);
afterInsert(connection, table);
boolean hasSum = true;
int sum = 0;
for (int idx = 0; (results != null) && (idx < results.length); idx++)
{
if (results[idx] < 0)
{
hasSum = false;
if (results[idx] == Statement.EXECUTE_FAILED)
{
_log.warn("The batch insertion of row " + idx + " into table " + table.getQualifiedName() + " failed but the driver is able to continue processing");
}
else if (results[idx] != Statement.SUCCESS_NO_INFO)
{
_log.warn("The batch insertion of row " + idx + " into table " + table.getQualifiedName() + " returned an undefined status value " + results[idx]);
}
}
else
{
sum += results[idx];
}
}
if (hasSum && (sum != numRows))
{
_log.warn("Attempted to insert " + numRows + " rows into table " + table.getQualifiedName() + " but changed " + sum + " rows");
}
}
catch (SQLException ex)
{
if (ex instanceof BatchUpdateException)
{
SQLException sqlEx = ((BatchUpdateException)ex).getNextException();
throw new DatabaseOperationException("Error while inserting into the database", sqlEx);
}
else
{
throw new DatabaseOperationException("Error while inserting into the database", ex);
}
}
}
}
/**
* Performs the batch for the given statement, and checks that the specified amount of rows have been changed.
*
* @param statement The prepared statement
* @param numRows The number of rows that should change
* @param table The changed table
*/
// GemStone changes BEGIN
private void executeBatch(PreparedStatement statement, int numRows,
Table table, DynaBean[] batchDynaBeans, Column[] autoIncrColumns)
throws DatabaseOperationException
/* (original code)
private void executeBatch(PreparedStatement statement, int numRows, Table table) throws DatabaseOperationException
*/
// GemStone changes BEGIN
{
if (statement != null)
{
try
{
Connection connection = statement.getConnection();
beforeInsert(connection, table);
int[] results = statement.executeBatch();
// GemStone changes BEGIN
if (batchDynaBeans != null) {
handleAutoIncrementValuesForBatch(statement,
autoIncrColumns, batchDynaBeans, numRows);
}
// GemStone changes END
closeStatement(statement);
afterInsert(connection, table);
boolean hasSum = true;
int sum = 0;
for (int idx = 0; (results != null) && (idx < results.length); idx++)
{
if (results[idx] < 0)
{
hasSum = false;
if (results[idx] == Statement.EXECUTE_FAILED)
{
_log.warn("The batch insertion of row " + idx + " into table " + table.getQualifiedName() + " failed but the driver is able to continue processing");
}
else if (results[idx] != Statement.SUCCESS_NO_INFO)
{
_log.warn("The batch insertion of row " + idx + " into table " + table.getQualifiedName() + " returned an undefined status value " + results[idx]);
}
}
else
{
sum += results[idx];
}
}
if (hasSum && (sum != numRows))
{
_log.warn("Attempted to insert " + numRows + " rows into table " + table.getQualifiedName() + " but changed " + sum + " rows");
}
}
catch (SQLException ex)
{
if (ex instanceof BatchUpdateException)
{
SQLException sqlEx = ((BatchUpdateException)ex).getNextException();
throw new DatabaseOperationException("Error while inserting into the database", sqlEx);
}
else
{
throw new DatabaseOperationException("Error while inserting into the database", ex);
}
}
}
}
/**
* <p>This method can be called with three cases:
*
* <ul>
* <li>Case 1: Success. statementResults contains the number of
* affected rows for all operations.
* <li>Case 2: Failure. statementResults contains the number of
* affected rows for all successful operations that were executed
* before the failed operation.
* <li>Case 3: Failure. statementResults contains the number of
* affected rows for all operations of the batch, i.e. further
* statements were executed after the first failed statement.
* </ul>
*
* <p>See {@link BatchUpdateException#getUpdateCounts()} for the specification
* of cases 2 and 3.
*
* @return all failed operations
*/
protected void postProcessJdbcBatchResult(
Iterator<DbOperation> operationsIt,
int[] statementResults,
Exception failure,
List<DbOperation> failedOperations) {
boolean failureHandled = false;
for (int i = 0; i < statementResults.length; i++) {
int statementResult = statementResults[i];
EnsureUtil.ensureTrue("More batch results than scheduled operations detected. This indicates a bug",
operationsIt.hasNext());
DbOperation operation = operationsIt.next();
if (statementResult == Statement.SUCCESS_NO_INFO) {
if (requiresAffectedRows(operation.getOperationType())) {
throw LOG.batchingNotSupported(operation);
} else {
postProcessOperationPerformed(operation, 1, null);
}
} else if (statementResult == Statement.EXECUTE_FAILED) {
/*
* All operations are marked with the root failure exception; this is not quite
* correct and leads to the situation that we treat all failed operations in the
* same way, whereas they might fail for different reasons.
*
* More precise would be to use BatchUpdateException#getNextException.
* E.g. if we have three failed statements in a batch, #getNextException can be used to
* access each operation's individual failure. However, this behavior is not
* guaranteed by the java.sql javadocs (it doesn't specify that the number
* and order of next exceptions matches the number of failures, unlike for row counts),
* so we decided to not rely on it.
*/
postProcessOperationPerformed(operation, 0, failure);
failureHandled = true;
} else { // it is the number of affected rows
postProcessOperationPerformed(operation, statementResult, null);
}
if (operation.isFailed()) {
failedOperations.add(operation);
}
}
/*
* case 2: The next operation is the one that failed
*/
if (failure != null && !failureHandled) {
EnsureUtil.ensureTrue("More batch results than scheduled operations detected. This indicates a bug",
operationsIt.hasNext());
DbOperation failedOperation = operationsIt.next();
postProcessOperationPerformed(failedOperation, 0, failure);
failedOperations.add(failedOperation);
}
}