Java Code Examples for java.sql.Statement.getUpdateCount()

The following are Jave code examples for showing how to use getUpdateCount() of the java.sql.Statement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Supreme-Bot   File: Database.java   Source Code and License Vote up 6 votes
public final boolean execute(String sql, Object... args) {
    if (!isConnected()) {
        return false;
    }
    String temp = null;
    try {
        temp = (args != null && args.length != 0) ? String.format(sql, args) : sql;
        if (DEBUG_SQL) {
            System.out.println(temp);
        }
        final Statement statement = createStatement();
        statement.execute(temp);
        final boolean result = statement.getUpdateCount() >= 0;
        statement.close();
        return result;
    } catch (Exception ex) {
        System.err.println("Database: Executing error");
        if (DEBUG) {
            System.err.println("SQL: " + temp);
        }
        ex.printStackTrace();
        return false;
    }
}
 
Example 2
Project: lams   File: ResultSetReturnImpl.java   Source Code and License Vote up 6 votes
@Override
public ResultSet execute(Statement statement, String sql) {
	sqlStatementLogger.logStatement( sql );
	try {
		final ResultSet rs;
		try {
			jdbcCoordinator.getTransactionCoordinator().getTransactionContext().startStatementExecution();
			if ( !statement.execute( sql ) ) {
				while ( !statement.getMoreResults() && statement.getUpdateCount() != -1 ) {
					// do nothing until we hit the resultset
				}
			}
			rs = statement.getResultSet();
		}
		finally {
			jdbcCoordinator.getTransactionCoordinator().getTransactionContext().endStatementExecution();
		}
		postExtract( rs, statement );
		return rs;
	}
	catch (SQLException e) {
		throw sqlExceptionHelper.convert( e, "could not execute statement" );
	}
}
 
Example 3
Project: OpenDiabetes   File: TestUtil.java   Source Code and License Vote up 5 votes
protected boolean test(Statement aStatement) {

        try {
            try {

                //execute the SQL
                aStatement.execute(getSql());
            } catch (SQLException s) {
                throw new Exception("Expected an update count of "
                                    + getCountWeWant()
                                    + ", but got the error: "
                                    + s.getMessage());
            }

            if (aStatement.getUpdateCount() != getCountWeWant()) {
                throw new Exception("Expected an update count of "
                                    + getCountWeWant()
                                    + ", but got an update count of "
                                    + aStatement.getUpdateCount() + ".");
            }
        } catch (Exception x) {
            message = x.toString();

            return false;
        }

        return true;
    }
 
Example 4
Project: OpenDiabetes   File: TestUtil.java   Source Code and License Vote up 5 votes
protected boolean test(Statement aStatement) {

        try {

            //execute the SQL
            try {
                aStatement.execute(getSql());
            } catch (SQLException s) {
                throw new Exception("Expected a ResultSet containing "
                                    + getCountWeWant()
                                    + " rows, but got the error: "
                                    + s.getMessage());
            }

            //check that update count != -1
            if (aStatement.getUpdateCount() != -1) {
                throw new Exception(
                    "Expected a ResultSet, but got an update count of "
                    + aStatement.getUpdateCount());
            }

            //iterate over the ResultSet
            ResultSet results = aStatement.getResultSet();
            int       count   = 0;

            while (results.next()) {
                count++;
            }

            //check that we got as many rows as expected
            if (count != getCountWeWant()) {

                //we don't have the expected number of rows
                throw new Exception("Expected the ResultSet to contain "
                                    + getCountWeWant()
                                    + " rows, but it contained " + count
                                    + " rows.");
            }
        } catch (Exception x) {
            message = x.toString();

            return false;
        }

        return true;
    }
 
Example 5
Project: kinetica-client-jdbc   File: JDBCSample.java   Source Code and License Vote up 5 votes
/**
 * Show the result of given query.
 * 
 * @param conn
 *            The connection to use for querying the data source.
 * @param sql
 *            The SQL query to use for querying the data source.
 * @throws SQLException
 *             if error happens when executing the query.
 */
private static void showQueryResult(Connection conn, String sql)
		throws SQLException {
	System.out.println("Executing SQL query: " + sql);
	Statement stmt = null;
	try {
		stmt = conn.createStatement();
		boolean isResultSet = stmt.execute(sql);
		boolean hasMoreResult = true;
		while (hasMoreResult) {
			if (isResultSet) {
				ResultSet rs = null;
				try {
					rs = stmt.getResultSet();
					printResultSet(rs);
				} finally {
					if (rs != null) {
						rs.close();
					}
				}
			} else {
				int rowCount = stmt.getUpdateCount();
				if (rowCount == -1) {
					hasMoreResult = false;
				} else {
					System.out.println(String.format(
							"Query OK, %d row affected.", rowCount));
				}
			}
			isResultSet = stmt.getMoreResults();
		}
	} finally {
		if (stmt != null) {
			stmt.close();
		}
	}
}
 
Example 6
Project: dev-courses   File: TestUtil.java   Source Code and License Vote up 5 votes
protected boolean test(Statement aStatement) {

        try {
            try {

                //execute the SQL
                aStatement.execute(getSql());
            } catch (SQLException s) {
                throw new Exception("Expected an update count of "
                                    + getCountWeWant()
                                    + ", but got the error: "
                                    + s.getMessage());
            }

            if (aStatement.getUpdateCount() != getCountWeWant()) {
                throw new Exception("Expected an update count of "
                                    + getCountWeWant()
                                    + ", but got an update count of "
                                    + aStatement.getUpdateCount() + ".");
            }
        } catch (Exception x) {
            message = x.toString();

            return false;
        }

        return true;
    }
 
Example 7
Project: parabuild-ci   File: TestUtil.java   Source Code and License Vote up 5 votes
protected boolean test(Statement aStatement) {

        try {
            try {

                //execute the SQL
                aStatement.execute(getSql());
            } catch (SQLException s) {
                throw new Exception("Expected an update count of "
                                    + getCountWeWant()
                                    + ", but got the error: "
                                    + s.getMessage());
            }

            if (aStatement.getUpdateCount() != getCountWeWant()) {
                throw new Exception("Expected an update count of "
                                    + getCountWeWant()
                                    + ", but got an update count of "
                                    + aStatement.getUpdateCount() + ".");
            }
        } catch (Exception x) {
            message = x.getMessage();

            return false;
        }

        return true;
    }
 
Example 8
Project: parabuild-ci   File: TestUtil.java   Source Code and License Vote up 5 votes
protected boolean test(Statement aStatement) {

        try {
            try {

                //execute the SQL
                aStatement.execute(getSql());
            } catch (SQLException s) {
                throw new Exception("Expected an update count of "
                                    + getCountWeWant()
                                    + ", but got the error: "
                                    + s.getMessage());
            }

            if (aStatement.getUpdateCount() != getCountWeWant()) {
                throw new Exception("Expected an update count of "
                                    + getCountWeWant()
                                    + ", but got an update count of "
                                    + aStatement.getUpdateCount() + ".");
            }
        } catch (Exception x) {
            message = x.getMessage();

            return false;
        }

        return true;
    }
 
Example 9
Project: parabuild-ci   File: TestUtil.java   Source Code and License Vote up 5 votes
protected boolean test(Statement aStatement) {

        try {
            try {

                //execute the SQL
                aStatement.execute(getSql());
            } catch (SQLException s) {
                throw new Exception("Expected an update count of "
                                    + getCountWeWant()
                                    + ", but got the error: "
                                    + s.getMessage());
            }

            if (aStatement.getUpdateCount() != getCountWeWant()) {
                throw new Exception("Expected an update count of "
                                    + getCountWeWant()
                                    + ", but got an update count of "
                                    + aStatement.getUpdateCount() + ".");
            }
        } catch (Exception x) {
            message = x.getMessage();

            return false;
        }

        return true;
    }
 
Example 10
Project: parabuild-ci   File: TestUtil.java   Source Code and License Vote up 5 votes
protected boolean test(Statement aStatement) {

        try {
            try {

                //execute the SQL
                aStatement.execute(getSql());
            } catch (SQLException s) {
                throw new Exception("Expected an update count of "
                                    + getCountWeWant()
                                    + ", but got the error: "
                                    + s.getMessage());
            }

            if (aStatement.getUpdateCount() != getCountWeWant()) {
                throw new Exception("Expected an update count of "
                                    + getCountWeWant()
                                    + ", but got an update count of "
                                    + aStatement.getUpdateCount() + ".");
            }
        } catch (Exception x) {
            message = x.getMessage();

            return false;
        }

        return true;
    }
 
Example 11
Project: bdf2   File: DbJdbcUtils.java   Source Code and License Vote up 5 votes
public static int[] executeUpdate(Connection conn, String[] sqls) throws Exception {
	boolean isShowSql = false;
	List<String> sqlList = new ArrayList<String>();
	for (String sql : sqls) {
		if (StringUtils.hasText(sql)) {
			sqlList.add(sql);
		}
	}
	String[] sqlArray = sqlList.toArray(new String[] {});
	if (isShowSql) {
		for (String s : sqlArray) {
			System.out.println(s);
		}
	}
	Statement st = null;
	int[] rowsAffected = new int[] { sqlArray.length };
	try {
		conn.setAutoCommit(true);
		st = conn.createStatement();
		if (DbJdbcUtils.supportsBatchUpdates(conn)) {
			for (String currentSql : sqlArray) {
				st.addBatch(currentSql);
			}
			rowsAffected = st.executeBatch();
		} else {
			for (int i = 0; i < sqlArray.length; i++) {
				if (!st.execute(sqlArray[i])) {
					rowsAffected[i] = st.getUpdateCount();
				}
			}
		}
	} catch (SQLException e) {
		throw new SQLException(e);
	} finally {
		DbJdbcUtils.closeStatement(st);
	}
	return rowsAffected;
}
 
Example 12
Project: OpenDiabetes   File: TestUtil.java   Source Code and License Vote up 4 votes
protected boolean test(Statement aStatement) {

        try {
            try {

                //execute the SQL
                aStatement.execute(getSql());
            } catch (SQLException s) {
                throw new Exception("Expected a ResultSet, but got the error: "
                                    + s.getMessage());
            }

            //check that update count != -1
            if (aStatement.getUpdateCount() != -1) {
                throw new Exception(
                    "Expected a ResultSet, but got an update count of "
                    + aStatement.getUpdateCount());
            }

            //iterate over the ResultSet
            ResultSet    results  = aStatement.getResultSet();
            StringBuffer printVal = new StringBuffer();

            while (results.next()) {
                for (int j = 0; j < results.getMetaData().getColumnCount();
                        j++) {
                    if (j != 0) {
                        printVal.append(',');
                    }

                    printVal.append(results.getString(j + 1));
                }

                printVal.append(LS);
            }

            throw new Exception(printVal.toString());
        } catch (Exception x) {
            message = x.toString();

            return false;
        }
    }
 
Example 13
Project: gretl   File: SqlExecutorStep.java   Source Code and License Vote up 4 votes
/**
 * @param dbstmt        Database sql-Statement
 * @param statement     sql-Statement
 * @throws Exception    SQL-Exception while executing sqlstatement
 */
private void executeSqlStatement (Statement dbstmt, String statement)
        throws Exception {

    try {
        boolean isResultSet = dbstmt.execute(statement);  //only allows SQL INSERT, UPDATE or DELETE
        if(isResultSet) {
            ResultSet rs=dbstmt.getResultSet();
            StringBuffer res=new StringBuffer();
            int colCount=rs.getMetaData().getColumnCount();
            String sep="";
            while(rs.next()) {
                for(int i=1;i<=colCount;i++) {
                    String value=rs.getString(i);
                    if(value!=null) {
                        res.append(sep);
                        res.append(value);
                        sep=" ";
                    }
                }
                if(res.length()>0) {
                    log.lifecycle(taskName + ": " + res.toString());
                }
                res.setLength(0);
                sep="";
            }
        }else {
            int modifiedLines=dbstmt.getUpdateCount();
            if (modifiedLines==1) {
                log.lifecycle(taskName + ": " + modifiedLines + " Line has been modified.");
            } else if (modifiedLines>1) {
                log.lifecycle(taskName +": " + modifiedLines + " Lines have been modified.");
            } else if (modifiedLines<1){
                log.lifecycle(taskName + ": No Line has been modified.");
            }
        }


    } catch (SQLException ex) {
        throw new SQLException("Error while executing the sqlstatement. " + ex.getMessage());
    } finally {
        dbstmt.close();
    }
}
 
Example 14
Project: dev-courses   File: TestUtil.java   Source Code and License Vote up 4 votes
protected boolean test(Statement aStatement) {

        try {
            try {

                //execute the SQL
                aStatement.execute(getSql());
            } catch (SQLException s) {
                throw new Exception("Expected a ResultSet, but got the error: "
                                    + s.getMessage());
            }

            //check that update count != -1
            if (aStatement.getUpdateCount() != -1) {
                throw new Exception(
                    "Expected a ResultSet, but got an update count of "
                    + aStatement.getUpdateCount());
            }

            //iterate over the ResultSet
            ResultSet    results  = aStatement.getResultSet();
            StringBuffer printVal = new StringBuffer();

            while (results.next()) {
                for (int j = 0; j < results.getMetaData().getColumnCount();
                        j++) {
                    if (j != 0) {
                        printVal.append(',');
                    }

                    printVal.append(results.getString(j + 1));
                }

                printVal.append(LS);
            }

            throw new Exception(printVal.toString());
        } catch (Exception x) {
            message = x.toString();

            return false;
        }
    }
 
Example 15
Project: dev-courses   File: TestUtil.java   Source Code and License Vote up 4 votes
protected boolean test(Statement aStatement) {

        try {

            //execute the SQL
            try {
                aStatement.execute(getSql());
            } catch (SQLException s) {
                throw new Exception("Expected a ResultSet containing "
                                    + getCountWeWant()
                                    + " rows, but got the error: "
                                    + s.getMessage());
            }

            //check that update count != -1
            if (aStatement.getUpdateCount() != -1) {
                throw new Exception(
                    "Expected a ResultSet, but got an update count of "
                    + aStatement.getUpdateCount());
            }

            //iterate over the ResultSet
            ResultSet results = aStatement.getResultSet();
            int       count   = 0;

            while (results.next()) {
                count++;
            }

            //check that we got as many rows as expected
            if (count != getCountWeWant()) {

                //we don't have the expected number of rows
                throw new Exception("Expected the ResultSet to contain "
                                    + getCountWeWant()
                                    + " rows, but it contained " + count
                                    + " rows.");
            }
        } catch (Exception x) {
            message = x.toString();

            return false;
        }

        return true;
    }
 
Example 16
Project: parabuild-ci   File: TestUtil.java   Source Code and License Vote up 4 votes
protected boolean test(Statement aStatement) {

        try {

            //execute the SQL
            try {
                aStatement.execute(getSql());
            } catch (SQLException s) {
                throw new Exception("Expected a ResultSet containing "
                                    + getCountWeWant()
                                    + " rows, but got the error: "
                                    + s.getMessage());
            }

            //check that update count != -1
            if (aStatement.getUpdateCount() != -1) {
                throw new Exception(
                    "Expected a ResultSet, but got an update count of "
                    + aStatement.getUpdateCount());
            }

            //iterate over the ResultSet
            ResultSet results = aStatement.getResultSet();
            int       count   = 0;

            while (results.next()) {
                count++;
            }

            //check that we got as many rows as expected
            if (count != getCountWeWant()) {

                //we don't have the expected number of rows
                throw new Exception("Expected the ResultSet to contain "
                                    + getCountWeWant()
                                    + " rows, but it contained " + count
                                    + " rows.");
            }
        } catch (Exception x) {
            message = x.getMessage();

            return false;
        }

        return true;
    }
 
Example 17
Project: parabuild-ci   File: TestUtil.java   Source Code and License Vote up 4 votes
protected boolean test(Statement aStatement) {

        try {

            //execute the SQL
            try {
                aStatement.execute(getSql());
            } catch (SQLException s) {
                throw new Exception("Expected a ResultSet containing "
                                    + getCountWeWant()
                                    + " rows, but got the error: "
                                    + s.getMessage());
            }

            //check that update count != -1
            if (aStatement.getUpdateCount() != -1) {
                throw new Exception(
                    "Expected a ResultSet, but got an update count of "
                    + aStatement.getUpdateCount());
            }

            //iterate over the ResultSet
            ResultSet results = aStatement.getResultSet();
            int       count   = 0;

            while (results.next()) {
                count++;
            }

            //check that we got as many rows as expected
            if (count != getCountWeWant()) {

                //we don't have the expected number of rows
                throw new Exception("Expected the ResultSet to contain "
                                    + getCountWeWant()
                                    + " rows, but it contained " + count
                                    + " rows.");
            }
        } catch (Exception x) {
            message = x.getMessage();

            return false;
        }

        return true;
    }
 
Example 18
Project: parabuild-ci   File: TestUtil.java   Source Code and License Vote up 4 votes
protected boolean test(Statement aStatement) {

        try {

            //execute the SQL
            try {
                aStatement.execute(getSql());
            } catch (SQLException s) {
                throw new Exception("Expected a ResultSet containing "
                                    + getCountWeWant()
                                    + " rows, but got the error: "
                                    + s.getMessage());
            }

            //check that update count != -1
            if (aStatement.getUpdateCount() != -1) {
                throw new Exception(
                    "Expected a ResultSet, but got an update count of "
                    + aStatement.getUpdateCount());
            }

            //iterate over the ResultSet
            ResultSet results = aStatement.getResultSet();
            int       count   = 0;

            while (results.next()) {
                count++;
            }

            //check that we got as many rows as expected
            if (count != getCountWeWant()) {

                //we don't have the expected number of rows
                throw new Exception("Expected the ResultSet to contain "
                                    + getCountWeWant()
                                    + " rows, but it contained " + count
                                    + " rows.");
            }
        } catch (Exception x) {
            message = x.getMessage();

            return false;
        }

        return true;
    }
 
Example 19
Project: parabuild-ci   File: TestUtil.java   Source Code and License Vote up 4 votes
protected boolean test(Statement aStatement) {

        try {

            //execute the SQL
            try {
                aStatement.execute(getSql());
            } catch (SQLException s) {
                throw new Exception("Expected a ResultSet containing "
                                    + getCountWeWant()
                                    + " rows, but got the error: "
                                    + s.getMessage());
            }

            //check that update count != -1
            if (aStatement.getUpdateCount() != -1) {
                throw new Exception(
                    "Expected a ResultSet, but got an update count of "
                    + aStatement.getUpdateCount());
            }

            //iterate over the ResultSet
            ResultSet results = aStatement.getResultSet();
            int       count   = 0;

            while (results.next()) {
                count++;
            }

            //check that we got as many rows as expected
            if (count != getCountWeWant()) {

                //we don't have the expected number of rows
                throw new Exception("Expected the ResultSet to contain "
                                    + getCountWeWant()
                                    + " rows, but it contained " + count
                                    + " rows.");
            }
        } catch (Exception x) {
            message = x.getMessage();

            return false;
        }

        return true;
    }