下面列出了怎么用java.sql.SQLWarning的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* The first warning reported by calls on this Statement is returned. A
* Statement's execute methods clear its java.sql.SQLWarning chain.
* Subsequent Statement warnings will be chained to this
* java.sql.SQLWarning.
*
* <p>
* The Warning chain is automatically cleared each time a statement is (re)executed.
* </p>
*
* <p>
* <B>Note:</B> If you are processing a ResultSet then any warnings associated with ResultSet reads will be chained on the ResultSet object.
* </p>
*
* @return the first java.sql.SQLWarning or null
*
* @exception SQLException
* if a database access error occurs
*/
public java.sql.SQLWarning getWarnings() throws SQLException {
synchronized (checkClosed().getConnectionMutex()) {
if (this.clearWarningsCalled) {
return null;
}
if (this.connection.versionMeetsMinimum(4, 1, 0)) {
SQLWarning pendingWarningsFromServer = SQLError.convertShowWarningsToSQLWarnings(this.connection);
if (this.warningChain != null) {
this.warningChain.setNextWarning(pendingWarningsFromServer);
} else {
this.warningChain = pendingWarningsFromServer;
}
return this.warningChain;
}
return this.warningChain;
}
}
private int updateFromTable(PreparedStatement stmt, int whichUpdate, int qty,
int tid, int sid, int sid2) throws SQLException {
Log.getLogWriter().info("update statement is " + uniqUpdate[whichUpdate]);
int rowCount = 0;
switch (whichUpdate) {
case 0:
Log.getLogWriter().info("updating record where sid between " + sid +
" and " + sid2 + " and tid is " +tid);
stmt.setInt(1, qty);
stmt.setInt(2,tid);
stmt.setInt(3, sid);
stmt.setInt(4, sid2);
rowCount = stmt.executeUpdate();
break;
default:
throw new TestException("incorrect update statement, should not happen");
}
SQLWarning warning = stmt.getWarnings(); //test to see there is a warning
if (warning != null) {
SQLHelper.printSQLWarning(warning);
}
return rowCount;
}
/**
* Test that you get a warning when specifying a query which is not
* updatable and concurrency mode CONCUR_UPDATABLE.
* In this case, the query contains an "order by"
*/
public void testConcurrencyModeWarning1()
throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
s.setCursorName(getNextCursorName());
ResultSet rs = s.executeQuery("select * from t1 order by a");
SQLWarning warn = rs.getWarnings();
assertEquals("Expected resultset to be read only",
ResultSet.CONCUR_READ_ONLY,
rs.getConcurrency());
assertWarning(warn, QUERY_NOT_QUALIFIED_FOR_UPDATABLE_RESULTSET);
scrollForward(rs);
rs.close();
s.close();
}
public SQLWarning getWarnings() throws SQLException {
//try {
return connection.getWarnings();
//} catch( SQLException e ) {
// throw sqlExceptionThrown(e);
//}
}
protected void exeProcedure(Connection conn, String procedure) throws SQLException {
Statement stmt = conn.createStatement();
stmt.executeUpdate(procedure);
SQLWarning warning = stmt.getWarnings(); //test to see there is a warning
if (warning != null) {
SQLHelper.printSQLWarning(warning);
}
}
/**
* Create SQLWarning with message, SQLState, and error code
*/
@Test
public void test4() {
SQLWarning ex = new SQLWarning(reason, state, errorCode);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& ex.getCause() == null
&& ex.getErrorCode() == errorCode);
}
protected int deleteToTableCidRangeTx(PreparedStatement stmt, int cid, int cid2,
int oid, BigDecimal bid, int whichDelete) throws SQLException {
int rowCount = 0;
switch (whichDelete) {
case 0:
//"delete from trade.buyorders where cid=? and oid=? and bid <?",
Log.getLogWriter().info("deleting from buyorders table for cid: " + cid
+ " and oid: " + oid + " and bid<" + bid);
stmt.setInt(1, cid);
stmt.setInt(2, oid);
stmt.setBigDecimal(3, bid);
rowCount = stmt.executeUpdate();
break;
case 1:
// "delete from trade.buyorders where cid>? and cid<? and status IN ('cancelled', 'filled')",
Log.getLogWriter().info("deleting from buyorders table for cid> "+ cid + " to cid< " + cid2 +
" and status IN ('cancelled', 'filled')");
stmt.setInt(1, cid);
stmt.setInt(2, cid2);
rowCount = stmt.executeUpdate();
break;
default:
throw new TestException ("Wrong update sql string here");
}
SQLWarning warning = stmt.getWarnings(); //test to see there is a warning
if (warning != null) {
SQLHelper.printSQLWarning(warning);
}
return rowCount;
}
/**
* Create SQLWarning with Throwable
*/
@Test
public void test9() {
SQLWarning ex = new SQLWarning(t);
assertTrue(ex.getMessage().equals(cause)
&& ex.getSQLState() == null
&& cause.equals(ex.getCause().toString())
&& ex.getErrorCode() == 0);
}
@Override
public SQLWarning getWarnings()
throws SQLException
{
checkOpen();
return null;
}
@Override
public SQLWarning getWarnings()
throws SQLException
{
checkOpen();
return null;
}
/**
* Create SQLWarning with message
*/
@Test
public void test2() {
SQLWarning ex = new SQLWarning(reason);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState() == null
&& ex.getCause() == null
&& ex.getErrorCode() == 0);
}
/** log the warnings, if any, in execution of an SQL statement */
public static void logWarnings(Statement stmt, String sqlText, String prefix,
LogWriter logger) throws SQLException {
SQLWarning warning = stmt.getWarnings();
if (warning != null) {
if (logger.warningEnabled()) {
logger.warning(prefix + sqlText + " "+ warning.getMessage(), null);
}
while ((warning = warning.getNextWarning()) != null) {
if (logger.warningEnabled()) {
logger.warning(prefix + sqlText + " " + warning.getMessage(), null);
}
}
}
}
/**
* Validate that the ordering of the returned SQLWarning is correct using
* for-each loop
*/
@Test
public void test13() {
SQLWarning ex = new SQLWarning("Warning 1", t1);
SQLWarning ex1 = new SQLWarning("Warning 2");
SQLWarning ex2 = new SQLWarning("Warning 3", t2);
ex.setNextWarning(ex1);
ex.setNextWarning(ex2);
int num = 0;
for (Throwable e : ex) {
assertTrue(warnings[num++].equals(e.getMessage()));
}
}
@Override
public SQLWarning getWarnings() throws SQLException
{
logger.debug("SQLWarning getWarnings()");
raiseSQLExceptionIfConnectionIsClosed();
return sqlWarnings;
}
protected int deleteFromTable(PreparedStatement stmt, long cid, int tid,
int whichDelete) throws SQLException {
int txId = (Integer) SQLDistTxTest.curTxId.get();
String database = SQLHelper.isDerbyConn(stmt.getConnection()) ? "Derby - " : "gemfirexd - TXID:" + txId + " " ;
String query = " QUERY: " + delete[whichDelete];
int rowCount=0;
switch (whichDelete) {
case 0:
//"delete from trade.customersv1 where cid=?",
Log.getLogWriter().info(database + "deleting from trade.customersv1 " +
"with CID:" +cid + query);
stmt.setLong(1, cid);
rowCount = stmt.executeUpdate();
Log.getLogWriter().info(database + "deleted " + rowCount + " rows from " +
"trade.customersv1 with CID:" +cid + query);
break;
case 1:
//"delete from trade.customersv1 where cid in (? , ?) ",
Log.getLogWriter().info(database + "deleting from trade.customersv1 with" +
" CID:" + cid +",CID:" + (cid-tid) + query);
stmt.setLong(1, cid);
stmt.setLong(2, cid-tid);
rowCount = stmt.executeUpdate();
Log.getLogWriter().info(database + "deleted " + rowCount + " rows from " +
"trade.customersv1 with CID:" + cid +",CID:" + (cid-tid) + query);
break;
default:
throw new TestException("incorrect delete statement, should not happen");
}
SQLWarning warning = stmt.getWarnings(); //test to see there is a warning
if (warning != null) {
SQLHelper.printSQLWarning(warning);
}
return rowCount;
}
/**
* Connect to a database to test whether a connection can be made
*
* @param writer connection to send message to
* @param database database directory to connect to
* @param user user to use
* @param password password to use
*/
private void connectToDatabase(DDMWriter writer, String database, String user,
String password) throws Exception
{
Properties p = new Properties();
if (user != null)
p.put("user", user);
if (password != null)
p.put("password", password);
try {
Class.forName(CLOUDSCAPE_DRIVER);
}
catch (Exception e) {
sendMessage(writer, ERROR, e.getMessage());
return;
}
try {
//Note, we add database to the url so that we can allow additional
//url attributes
// GemStone changes BEGIN
Connection conn = DriverManager.getConnection(com.pivotal.gemfirexd.Attribute.PROTOCOL, p);
/* Connection conn = DriverManager.getConnection(Attribute.PROTOCOL+database, p); */
// GemStone changes END
// send warnings
SQLWarning warn = conn.getWarnings();
if (warn != null)
sendSQLMessage(writer, warn, SQLWARNING);
else
sendOK(writer);
conn.close();
return;
} catch (SQLException se) {
sendSQLMessage(writer, se, SQLERROR);
}
}
protected int deleteFromTable(PreparedStatement stmt, int sec_id, String symbol,
String exchange, int tid, int whichDelete) throws SQLException {
int txId = (Integer) SQLDistTxTest.curTxId.get();
String database = SQLHelper.isDerbyConn(stmt.getConnection()) ? "Derby - " : "gemfirexd - TXID:" + txId + " " ;
String query = " QUERY: " + delete[whichDelete];
if (!SQLHelper.isDerbyConn(stmt.getConnection()))
query = " QUERY: " + deleteJSON[whichDelete];
int rowCount=0;
switch (whichDelete) {
case 0:
//delete from trade.securities where sec_id=?",
Log.getLogWriter().info(database + "deleteing from trade.securities with SECID:" +sec_id + query);
stmt.setInt(1, sec_id);
rowCount = stmt.executeUpdate();
Log.getLogWriter().info(database + "deleted " + rowCount + " rows from trade.securities with SECID:" +sec_id + query);
break;
case 1:
//"delete from trade.securities where (symbol like ? and exchange = ? ) and tid=?"
Log.getLogWriter().info(database + "deleteing from trade.securities with SYMBOL:" + symbol +",EXCHANGE:" + exchange + ",TID:" + tid + query);
stmt.setString(1, symbol);
stmt.setString(2, exchange);
stmt.setInt(3, tid);
rowCount = stmt.executeUpdate();
Log.getLogWriter().info(database + "deleted " + rowCount + " rows from trade.securities with SYMBOL:" + symbol +",EXCHANGE:" + exchange + ",TID:" + tid + query);
break;
default:
throw new TestException("incorrect delete statement, should not happen");
}
SQLWarning warning = stmt.getWarnings(); //test to see there is a warning
if (warning != null) {
SQLHelper.printSQLWarning(warning);
}
return rowCount;
}
/**
* Test if call to {@link org.firebirdsql.gds.ng.listeners.StatementListenerDispatcher#warningReceived(org.firebirdsql.gds.ng.FbStatement, java.sql.SQLWarning)}
* is forwarded correctly.
*/
@Test
public void testWarningReceived() {
final Expectations expectations = new Expectations();
final SQLWarning warning = new SQLWarning();
expectations.exactly(1).of(listener).warningReceived(statement, warning);
context.checking(expectations);
dispatcher.warningReceived(statement, warning);
}
/**
* Create SQLWarning with message, SQLState, and error code
*/
@Test
public void test4() {
SQLWarning ex = new SQLWarning(reason, state, errorCode);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& ex.getCause() == null
&& ex.getErrorCode() == errorCode);
}
public SQLWarning getWarnings() throws SQLException {
try {
if (this.wrappedStmt != null) {
return this.wrappedStmt.getWarnings();
}
throw SQLError.createSQLException("Statement already closed", SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
} catch (SQLException sqlEx) {
checkAndFireConnectionError(sqlEx);
}
return null;
}
@Override
public SQLWarning getWarnings() throws SQLException {
if (statements.size() > 0) {
return statements.iterator().next().getWarnings();
}
throw new UnsupportedOperationException();
}
protected int deleteToTableTidListTx(PreparedStatement stmt, int oid1, int oid2,
int oid, int whichDelete, int tid) throws SQLException {
int rowCount = 0;
switch (whichDelete) {
case 0:
//"delete from trade.sellorders where oid=? and tid=? ",
Log.getLogWriter().info("deleting from sellorders for oid: " + oid
+ " and tid: " + tid);
stmt.setInt(1, oid);
stmt.setInt(2, tid);
rowCount = stmt.executeUpdate();
break;
case 1:
//"delete from trade.sellorders where oid>? and oid<? and status IN ('cancelled', 'filled') and tid=? ",
Log.getLogWriter().info("deleting from sellorders for oid > " + oid1
+ " oid < " + oid2 + " and status IN ('cancelled', 'filled') and tid: " + tid);
stmt.setInt(1, oid1);
stmt.setInt(2, oid2);
stmt.setInt(3, tid);
rowCount = stmt.executeUpdate();
break;
default:
throw new TestException ("Wrong delete sql string here");
}
SQLWarning warning = stmt.getWarnings(); //test to see there is a warning
if (warning != null) {
SQLHelper.printSQLWarning(warning);
}
return rowCount;
}
/**
* Serialize a SQLWarning and make sure you can read it back properly
*/
@Test
public void test10() throws Exception {
SQLWarning e = new SQLWarning(reason, state, errorCode, t);
SQLWarning ex1 = createSerializedException(e);
assertTrue(reason.equals(ex1.getMessage())
&& ex1.getSQLState().equals(state)
&& cause.equals(ex1.getCause().toString())
&& ex1.getErrorCode() == errorCode);
}
@Override
public SQLWarning getWarnings() throws SQLException
{
logger.debug("public SQLWarning getWarnings()");
raiseSQLExceptionIfResultSetIsClosed();
return null;
}
/**
* Create SQLWarning with message, and SQLState
*/
@Test
public void test3() {
SQLWarning ex = new SQLWarning(reason, state);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& ex.getCause() == null
&& ex.getErrorCode() == 0);
}
/**
* Create SQLWarning with message, SQLState, errorCode, and Throwable
*/
@Test
public void test5() {
SQLWarning ex = new SQLWarning(reason, state, errorCode, t);
assertTrue(ex.getMessage().equals(reason)
&& ex.getSQLState().equals(state)
&& cause.equals(ex.getCause().toString())
&& ex.getErrorCode() == errorCode);
}
protected int deleteToTableTidListTx(PreparedStatement stmt, int cid, String cust_name,
int whichDelete, int tid) throws SQLException {
int rowCount = 0;
switch (whichDelete) {
case 0:
//"delete from trade.customers where cid=? and tid=? ",
Log.getLogWriter().info("delete from customers for cid: " + cid + " and tid: " + tid);
stmt.setInt(1, cid);
stmt.setInt(2, tid);
rowCount = stmt.executeUpdate();
break;
case 1:
//"delete from trade.customers where (cust_name > ? or cid < ? ) and tid = ?",
Log.getLogWriter().info("delete from customers for cust_name>'" + cust_name + "'and " +
"cid< " + cid + " and tid: " + tid);
stmt.setString(1, cust_name);
stmt.setInt(2, cid);
stmt.setInt(3, tid);
rowCount = stmt.executeUpdate();
break;
default:
throw new TestException ("Wrong delete sql string here");
}
SQLWarning warning = stmt.getWarnings(); //test to see there is a warning
if (warning != null) {
SQLHelper.printSQLWarning(warning);
}
return rowCount;
}
@Override
public SQLWarning getWarnings() throws SQLException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void warningReceived(FbStatement sender, SQLWarning warning) {
warnings.add(warning);
}
@Override
public SQLWarning getWarnings() throws SQLException {
// TODO Auto-generated method stub
return null;
}