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

The following are Jave code examples for showing how to use clearBatch() 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: ts-benchmark   File: TsfileDB.java   Source Code and License Vote up 4 votes
@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);
	}
 
Example 2
Project: the-vigilantes   File: StatementRegressionTest.java   Source Code and License Vote up 4 votes
/**
 * 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");
    }
}
 
Example 3
Project: OpenVertretung   File: StatementRegressionTest.java   Source Code and License Vote up 4 votes
/**
 * 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");
    }
}
 
Example 4
Project: ProyectoPacientes   File: StatementRegressionTest.java   Source Code and License Vote up 4 votes
/**
 * 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");
    }
}
 
Example 5
Project: BibliotecaPS   File: StatementRegressionTest.java   Source Code and License Vote up 4 votes
/**
 * 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");
    }
}