本文整理匯總了Java中java.sql.Statement.clearBatch方法的典型用法代碼示例。如果您正苦於以下問題:Java Statement.clearBatch方法的具體用法?Java Statement.clearBatch怎麽用?Java Statement.clearBatch使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.sql.Statement
的用法示例。
在下文中一共展示了Statement.clearBatch方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: updatePoints
import java.sql.Statement; //導入方法依賴的package包/類
@Override
public Status updatePoints(List<TsPoint> points) {
//timestamp->device->sensor
long costTime=0;
// int count=0;
Connection connection = null;
Statement statement = null;
try {
connection=getConnection();
statement=connection.createStatement();
if(points!=null){
StringBuilder sc=new StringBuilder();
for(TsPoint point:points){
sc.append("update ");
sc.append(ROOT_SERIES_NAME);
sc.append(".");
sc.append(point.getDeviceCode());
sc.append(".");
sc.append(point.getSensorCode());
sc.append(" set value=");
sc.append(point.getValue());
sc.append(" where");
sc.append(" time=");
sc.append(point.getTimestamp());
statement.addBatch(sc.toString());
sc.setLength(0);
}
}
long startTime=System.nanoTime();
statement.executeBatch();//批量更新
statement.clearBatch();
long endTime=System.nanoTime();
costTime=endTime-startTime;
}catch(Exception e){
return Status.FAILED(-1);
}finally{
closeStatement(statement);
closeConnection(connection);
}
return Status.OK(costTime);
}
示例2: testBug77681
import java.sql.Statement; //導入方法依賴的package包/類
/**
* Tests fix for BUG#77681 - rewrite replace sql like insert when rewriteBatchedStatements=true (contribution)
*
* When using 'rewriteBatchedStatements=true' we rewrite several batched statements into one single query by extending its VALUES clause. Although INSERT
* REPLACE have the same syntax, this wasn't happening for REPLACE statements.
*
* This tests the number of queries actually sent to server when rewriteBatchedStatements is used and not by using a StatementInterceptor. The test is
* repeated for server side prepared statements. Without the fix, this test fails while checking the number of expected REPLACE queries.
*/
public void testBug77681() throws Exception {
createTable("testBug77681", "(id INT, txt VARCHAR(50), PRIMARY KEY (id))");
Properties props = new Properties();
props.setProperty("statementInterceptors", TestBug77681StatementInterceptor.class.getName());
for (int tst = 0; tst < 4; tst++) {
props.setProperty("useServerPrepStmts", Boolean.toString((tst & 0x1) != 0));
props.setProperty("rewriteBatchedStatements", Boolean.toString((tst & 0x2) != 0));
Connection testConn = getConnectionWithProps(props);
PreparedStatement testPstmt = testConn.prepareStatement("INSERT INTO testBug77681 VALUES (?, ?)");
testPstmt.setInt(1, 1);
testPstmt.setString(2, "one");
testPstmt.addBatch();
testPstmt.setInt(1, 2);
testPstmt.setString(2, "two");
testPstmt.addBatch();
testPstmt.setInt(1, 3);
testPstmt.setString(2, "three");
testPstmt.addBatch();
testPstmt.setInt(1, 4);
testPstmt.setString(2, "four");
testPstmt.addBatch();
testPstmt.setInt(1, 5);
testPstmt.setString(2, "five");
testPstmt.addBatch();
testPstmt.executeBatch();
testPstmt.close();
testPstmt = testConn.prepareStatement("REPLACE INTO testBug77681 VALUES (?, ?)");
testPstmt.setInt(1, 2);
testPstmt.setString(2, "TWO");
testPstmt.addBatch();
testPstmt.setInt(1, 4);
testPstmt.setString(2, "FOUR");
testPstmt.addBatch();
testPstmt.setInt(1, 6);
testPstmt.setString(2, "SIX");
testPstmt.addBatch();
testPstmt.executeBatch();
testPstmt.close();
Statement testStmt = testConn.createStatement();
testStmt.clearBatch();
testStmt.addBatch("INSERT INTO testBug77681 VALUES (7, 'seven')");
testStmt.addBatch("INSERT INTO testBug77681 VALUES (8, 'eight')");
testStmt.addBatch("INSERT INTO testBug77681 VALUES (9, 'nine')");
testStmt.addBatch("INSERT INTO testBug77681 VALUES (10, 'ten')");
testStmt.addBatch("INSERT INTO testBug77681 VALUES (11, 'eleven')");
testStmt.executeBatch();
testStmt.clearBatch();
testStmt.addBatch("REPLACE INTO testBug77681 VALUES (8, 'EIGHT')");
testStmt.addBatch("REPLACE INTO testBug77681 VALUES (10, 'TEN')");
testStmt.addBatch("REPLACE INTO testBug77681 VALUES (12, 'TWELVE')");
testStmt.addBatch("REPLACE INTO testBug77681 VALUES (14, 'FOURTEEN')");
testStmt.addBatch("REPLACE INTO testBug77681 VALUES (16, 'SIXTEEN')");
testStmt.executeBatch();
this.stmt.executeUpdate("DELETE FROM testBug77681");
}
}