KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > database > statement > AutomaticPreparedBatchStatement


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

21 package org.dbunit.database.statement;
22
23 import org.dbunit.dataset.datatype.DataType;
24 import org.dbunit.dataset.datatype.TypeCastException;
25
26 import java.sql.SQLException JavaDoc;
27
28 /**
29  * @author Manuel Laflamme
30  * @since Jun 12, 2003
31  * @version $Revision: 1.4 $
32  */

33 public class AutomaticPreparedBatchStatement implements IPreparedBatchStatement
34 {
35     private final IPreparedBatchStatement _statement;
36     private int _batchCount = 0;
37     private int _threshold;
38     private int _result = 0;
39
40     public AutomaticPreparedBatchStatement(IPreparedBatchStatement statement, int threshold)
41     {
42         _statement = statement;
43         _threshold = threshold;
44     }
45
46     ////////////////////////////////////////////////////////////////////////////
47
// IPreparedBatchStatement interface
48

49     public void addValue(Object JavaDoc value, DataType dataType) throws TypeCastException,
50             SQLException JavaDoc
51     {
52         _statement.addValue(value, dataType);
53     }
54
55     public void addBatch() throws SQLException JavaDoc
56     {
57         _statement.addBatch();
58         _batchCount++;
59
60         if (_batchCount % _threshold == 0)
61         {
62             _result += _statement.executeBatch();
63         }
64     }
65
66     public int executeBatch() throws SQLException JavaDoc
67     {
68         _result += _statement.executeBatch();
69         return _result;
70     }
71
72     public void clearBatch() throws SQLException JavaDoc
73     {
74         _statement.clearBatch();
75         _batchCount = 0;
76     }
77
78     public void close() throws SQLException JavaDoc
79     {
80         _statement.close();
81     }
82 }
83
Popular Tags