下面列出了java.sql.ResultSet#updateBigDecimal ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Tests calling update on all columns of the row.
* @exception SQLException database access error. Causes test to
* fail with an error.
*/
public void jdbc2testUpdateBigDecimal()
throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = s.executeQuery(SELECT_STMT);
rs.next();
for (int i = 1; i <= COLUMNS; i++) {
rs.updateBigDecimal(i, BigDecimal.valueOf(2L));
assertEquals("Expected rs.getBigDecimal(" + i +
") to match updated value", 2,
rs.getBigDecimal(i).intValue());
}
rs.updateRow();
rs.close();
checkColumnsAreUpdated();
s.close();
}
/**
* Tests calling update on all columns of the row.
* @exception SQLException database access error. Causes test to
* fail with an error.
*/
public void jdbc2testUpdateBigDecimal()
throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = s.executeQuery(SELECT_STMT);
rs.next();
for (int i = 1; i <= COLUMNS; i++) {
rs.updateBigDecimal(i, BigDecimal.valueOf(2L));
assertEquals("Expected rs.getBigDecimal(" + i +
") to match updated value", 2,
rs.getBigDecimal(i).intValue());
}
rs.updateRow();
rs.close();
checkColumnsAreUpdated();
s.close();
}
protected boolean updateRowSFU_URSTx(ResultSet updatableRs, int tid) {
try {
int cid = updatableRs.getInt("CID");
BigDecimal sec = updatableRs.getBigDecimal("SECURITIES").add(
new BigDecimal(tid));
updatableRs.updateBigDecimal("SECURITIES", sec);
updatableRs.updateRow();
Log.getLogWriter().info(
"update trade.networth set securities to be " + sec + " for cid: "
+ cid);
} catch (SQLException se) {
if (isHATest && SQLHelper.gotTXNodeFailureException(se)) {
Log.getLogWriter().info(
"got node failure exception, needs to retry the op");
return false;
}
SQLHelper.handleSQLException(se);
}
return true;
}
/**
* Tests calling update on all columns of the row.
* @exception SQLException database access error. Causes test to
* fail with an error.
*/
public void jdbc2testUpdateBigDecimal()
throws SQLException
{
Statement s = createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = s.executeQuery(SELECT_STMT);
rs.next();
for (int i = 1; i <= COLUMNS; i++) {
rs.updateBigDecimal(i, BigDecimal.valueOf(2L));
assertEquals("Expected rs.getBigDecimal(" + i +
") to match updated value", 2,
rs.getBigDecimal(i).intValue());
}
rs.updateRow();
rs.close();
checkColumnsAreUpdated();
s.close();
}
protected boolean updateURSRowTx(ResultSet updatableRs, int tid) {
try {
int cid = updatableRs.getInt("CID");
BigDecimal sec = updatableRs.getBigDecimal("SECURITIES").add(
new BigDecimal(tid));
updatableRs.updateBigDecimal("SECURITIES", sec);
updatableRs.updateRow();
Log.getLogWriter().info("update trade.networth set securities to be " + sec
+ " for cid: " + cid);
} catch (SQLException se) {
if (se.getSQLState().equals("X0Z02")) {
SQLHelper.printSQLException(se);
return false; // expected updatable result set
} else
SQLHelper.handleSQLException(se);
}
return true;
}
/** This method is a wrapper for ResultSet method
* updateBigDecimal(int columnIndex, BigDecimal x)
* @param rs ResultSet
* @param columnIndex Column Index
* @param bdString String to be used in updateXXX method
* @throws SQLException
*/
public static void updateBigDecimalString(ResultSet rs, int columnIndex, String bdString) throws SQLException{
switch(representation){
case BIGDECIMAL_REPRESENTATION:
BigDecimal bd = (bdString == null) ? null : new BigDecimal(bdString);
rs.updateBigDecimal(columnIndex, bd);
break;
case STRING_REPRESENTATION:
rs.updateString(columnIndex, bdString);
break;
default:
new Exception("Failed: Invalid Big Decimal representation").printStackTrace();
}
}
/** This method is a wrapper for ResultSet method
* updateBigDecimal(String columnName, BigDecimal x)
* @param rs ResultSet
* @param columnName Column Name
* @param bdString String to be used in updateXXX method
* @throws SQLException
*/
public static void updateBigDecimalString(ResultSet rs, String columnName,String bdString) throws SQLException{
switch(representation){
case BIGDECIMAL_REPRESENTATION:
BigDecimal bd = (bdString == null) ? null : new BigDecimal(bdString);
rs.updateBigDecimal(columnName, bd);
break;
case STRING_REPRESENTATION:
rs.updateString(columnName, bdString);
break;
default:
new Exception("Failed: Invalid Big Decimal representation").printStackTrace();
}
}
/** This method is a wrapper for ResultSet method
* updateBigDecimal(String columnName, BigDecimal x)
* @param rs ResultSet
* @param columnName Column Name
* @param bdString String to be used in updateXXX method
* @throws SQLException
*/
public static void updateBigDecimalString(ResultSet rs, String columnName,String bdString) throws SQLException{
switch(representation){
case BIGDECIMAL_REPRESENTATION:
BigDecimal bd = (bdString == null) ? null : new BigDecimal(bdString);
rs.updateBigDecimal(columnName, bd);
break;
case STRING_REPRESENTATION:
rs.updateString(columnName, bdString);
break;
default:
new Exception("Failed: Invalid Big Decimal representation").printStackTrace();
}
}
/** This method is a wrapper for ResultSet method
* updateBigDecimal(String columnName, BigDecimal x)
* @param rs ResultSet
* @param columnName Column Name
* @param bdString String to be used in updateXXX method
* @throws SQLException
*/
public static void updateBigDecimalString(ResultSet rs, String columnName,String bdString) throws SQLException{
switch(representation){
case BIGDECIMAL_REPRESENTATION:
BigDecimal bd = (bdString == null) ? null : new BigDecimal(bdString);
rs.updateBigDecimal(columnName, bd);
break;
case STRING_REPRESENTATION:
rs.updateString(columnName, bdString);
break;
default:
new Exception("Failed: Invalid Big Decimal representation").printStackTrace();
}
}
private void assertUpdateState(
ResultSet rs,
String colName,
BigDecimal value,
String expected) throws SQLException {
try {
rs.updateBigDecimal(colName, value);
fail("exception expected");
} catch (SQLException e) {
println(e.toString());
assertSQLState(expected, e);
}
}
/** This method is a wrapper for ResultSet method
* updateBigDecimal(int columnIndex, BigDecimal x)
* @param rs ResultSet
* @param columnIndex Column Index
* @param bdString String to be used in updateXXX method
* @throws SQLException
*/
public static void updateBigDecimalString(ResultSet rs, int columnIndex, String bdString) throws SQLException{
switch(representation){
case BIGDECIMAL_REPRESENTATION:
BigDecimal bd = (bdString == null) ? null : new BigDecimal(bdString);
rs.updateBigDecimal(columnIndex, bd);
break;
case STRING_REPRESENTATION:
rs.updateString(columnIndex, bdString);
break;
default:
new Exception("Failed: Invalid Big Decimal representation").printStackTrace();
}
}
/** This method is a wrapper for ResultSet method
* updateBigDecimal(String columnName, BigDecimal x)
* @param rs ResultSet
* @param columnName Column Name
* @param bdString String to be used in updateXXX method
* @throws SQLException
*/
public static void updateBigDecimalString(ResultSet rs, String columnName,String bdString) throws SQLException{
switch(representation){
case BIGDECIMAL_REPRESENTATION:
BigDecimal bd = (bdString == null) ? null : new BigDecimal(bdString);
rs.updateBigDecimal(columnName, bd);
break;
case STRING_REPRESENTATION:
rs.updateString(columnName, bdString);
break;
default:
new Exception("Failed: Invalid Big Decimal representation").printStackTrace();
}
}
/** This method is a wrapper for ResultSet method
* updateBigDecimal(int columnIndex, BigDecimal x)
* @param rs ResultSet
* @param columnIndex Column Index
* @param bdString String to be used in updateXXX method
* @throws SQLException
*/
public static void updateBigDecimalString(ResultSet rs, int columnIndex, String bdString) throws SQLException{
switch(representation){
case BIGDECIMAL_REPRESENTATION:
BigDecimal bd = (bdString == null) ? null : new BigDecimal(bdString);
rs.updateBigDecimal(columnIndex, bd);
break;
case STRING_REPRESENTATION:
rs.updateString(columnIndex, bdString);
break;
default:
new Exception("Failed: Invalid Big Decimal representation").printStackTrace();
}
}
/** This method is a wrapper for ResultSet method
* updateBigDecimal(int columnIndex, BigDecimal x)
* @param rs ResultSet
* @param columnIndex Column Index
* @param bdString String to be used in updateXXX method
* @throws SQLException
*/
public static void updateBigDecimalString(ResultSet rs, int columnIndex, String bdString) throws SQLException{
switch(representation){
case BIGDECIMAL_REPRESENTATION:
BigDecimal bd = (bdString == null) ? null : new BigDecimal(bdString);
rs.updateBigDecimal(columnIndex, bd);
break;
case STRING_REPRESENTATION:
rs.updateString(columnIndex, bdString);
break;
default:
new Exception("Failed: Invalid Big Decimal representation").printStackTrace();
}
}
/** This method is a wrapper for ResultSet method
* updateBigDecimal(int columnIndex, BigDecimal x)
* @param rs ResultSet
* @param columnIndex Column Index
* @param bdString String to be used in updateXXX method
* @throws SQLException
*/
public static void updateBigDecimalString(ResultSet rs, int columnIndex, String bdString) throws SQLException{
switch(representation){
case BIGDECIMAL_REPRESENTATION:
BigDecimal bd = (bdString == null) ? null : new BigDecimal(bdString);
rs.updateBigDecimal(columnIndex, bd);
break;
case STRING_REPRESENTATION:
rs.updateString(columnIndex, bdString);
break;
default:
new Exception("Failed: Invalid Big Decimal representation").printStackTrace();
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateBigDecimalForColumnIndex() throws SQLException {
for (ResultSet each : resultSets) {
each.updateBigDecimal(1, new BigDecimal("1"));
}
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateBigDecimalForColumnLabel() throws SQLException {
for (ResultSet each : resultSets) {
each.updateBigDecimal("label", new BigDecimal("1"));
}
}
public void _testSelectForUpdateForDebugging() throws Exception {
Connection conn = getConnection();
Statement stmtForTableAndInsert = conn.createStatement();
stmtForTableAndInsert.execute("create table Employee"
+ "(firstname varchar(50) not null, lastname varchar(50) not null, "
+ "workdept varchar(50), bonus decimal(10,4), "
+ "primary key (firstname, lastname))");
stmtForTableAndInsert
.execute("insert into employee values('neeraj', 'kumar', 'rnd', 0.0), "
+ "('asif', 'shahid', 'rnd', 1.0), "
+ "('dada', 'ji', 'rnd', 2.0), ('sum', 'wale', 'rnd', 3.0)");
conn.commit();
// conn.setAutoCommit(false);
// Create the statement with concurrency mode CONCUR_UPDATABLE
// to allow result sets to be updatable
conn.setTransactionIsolation(getIsolationLevel());
Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE, ResultSet.CLOSE_CURSORS_AT_COMMIT);
// Statement stmt = conn.createStatement();
// Updatable statements have some requirements
// for example, select must be on a single table
// Only bonus can be updated
// ResultSet uprs = stmt.executeQuery(
// "SELECT WORKDEPT, BONUS /*, firstname, LastNaME*/ " +
// "FROM EMPLOYEE where lastname = 'kumar' FOR UPDATE of BONUS");
// Only bonus can be updated
ResultSet uprs = stmt.executeQuery("SELECT workdept, bonus "
+ "FROM EMPLOYEE where lastname = 'kumar' FOR UPDATE of BONUS");
// ResultSet uprs = stmt.executeQuery(
// "SELECT firstname, count(*) " +
// "FROM EMPLOYEE group by firstname FOR UPDATE of BONUS"); // Only bonus
// can be updated
while (uprs.next()) {
uprs.getString("WORKDEPT");
BigDecimal bonus = uprs.getBigDecimal("BONUS");
// if (workDept.equals(theDept)) {
if (true) {
// if the current row meets our criteria,
// update the updatable column in the row
uprs.updateBigDecimal("BONUS", bonus.add(BigDecimal.valueOf(250L)));
// uprs.updateBigDecimal("BONUS", null);
uprs.updateRow();
// System.out.println("Updating bonus for employee:" +
// firstnme +" "+ lastName);
}
}
conn.commit(); // commit the transaction
// close object
uprs.close();
ResultSet rs = stmt.executeQuery("select * from employee");
while (rs.next()) {
System.out.println(rs.getString(1) + ", " + rs.getString(2) + ", "
+ rs.getString(3) + ", " + rs.getBigDecimal(4));
}
conn.commit();
stmt.close();
// Close connection if the application does not need it any more
conn.close();
}
@Test(expected = SQLFeatureNotSupportedException.class)
public void assertUpdateBigDecimalForColumnLabel() throws SQLException {
for (ResultSet each : resultSets) {
each.updateBigDecimal("label", new BigDecimal("1"));
}
}
public void _testSelectForUpdateForDebugging() throws Exception {
Connection conn = getConnection();
Statement stmtForTableAndInsert = conn.createStatement();
stmtForTableAndInsert.execute("create table Employee"
+ "(firstname varchar(50) not null, lastname varchar(50) not null, "
+ "workdept varchar(50), bonus decimal(10,4), "
+ "primary key (firstname, lastname))");
stmtForTableAndInsert
.execute("insert into employee values('neeraj', 'kumar', 'rnd', 0.0), "
+ "('asif', 'shahid', 'rnd', 1.0), "
+ "('dada', 'ji', 'rnd', 2.0), ('sum', 'wale', 'rnd', 3.0)");
conn.commit();
// conn.setAutoCommit(false);
// Create the statement with concurrency mode CONCUR_UPDATABLE
// to allow result sets to be updatable
conn.setTransactionIsolation(getIsolationLevel());
Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE, ResultSet.CLOSE_CURSORS_AT_COMMIT);
// Statement stmt = conn.createStatement();
// Updatable statements have some requirements
// for example, select must be on a single table
// Only bonus can be updated
// ResultSet uprs = stmt.executeQuery(
// "SELECT WORKDEPT, BONUS /*, firstname, LastNaME*/ " +
// "FROM EMPLOYEE where lastname = 'kumar' FOR UPDATE of BONUS");
// Only bonus can be updated
ResultSet uprs = stmt.executeQuery("SELECT workdept, bonus "
+ "FROM EMPLOYEE where lastname = 'kumar' FOR UPDATE of BONUS");
// ResultSet uprs = stmt.executeQuery(
// "SELECT firstname, count(*) " +
// "FROM EMPLOYEE group by firstname FOR UPDATE of BONUS"); // Only bonus
// can be updated
while (uprs.next()) {
uprs.getString("WORKDEPT");
BigDecimal bonus = uprs.getBigDecimal("BONUS");
// if (workDept.equals(theDept)) {
if (true) {
// if the current row meets our criteria,
// update the updatable column in the row
uprs.updateBigDecimal("BONUS", bonus.add(BigDecimal.valueOf(250L)));
// uprs.updateBigDecimal("BONUS", null);
uprs.updateRow();
// System.out.println("Updating bonus for employee:" +
// firstnme +" "+ lastName);
}
}
conn.commit(); // commit the transaction
// close object
uprs.close();
ResultSet rs = stmt.executeQuery("select * from employee");
while (rs.next()) {
System.out.println(rs.getString(1) + ", " + rs.getString(2) + ", "
+ rs.getString(3) + ", " + rs.getBigDecimal(4));
}
conn.commit();
stmt.close();
// Close connection if the application does not need it any more
conn.close();
}