KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ziclix > python > sql > handler > UpdateCountDataHandler


1 /*
2  * Jython Database Specification API 2.0
3  *
4  * $Id: UpdateCountDataHandler.java,v 1.4 2005/02/23 04:26:19 bzimmer Exp $
5  *
6  * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
7  *
8  */

9 package com.ziclix.python.sql.handler;
10
11 import com.ziclix.python.sql.DataHandler;
12 import com.ziclix.python.sql.FilterDataHandler;
13 import com.ziclix.python.sql.zxJDBC;
14 import org.python.core.Py;
15
16 import java.sql.SQLException JavaDoc;
17 import java.sql.Statement JavaDoc;
18
19 /**
20  * A data handler that keeps track of the update count for each execution of a
21  * Statement.
22  * <p/>
23  * <p><b>Note:</b> MySql does not return the correct count for a
24  * <a HREF="http://www.mysql.com/doc/D/E/DELETE.html">delete</a> statement that has
25  * no <code>where</code> clause. Therefore, to assure the correct update count is returned,
26  * either include a <code>where</code> clause, or understand that the value will always be
27  * <code>0</code>.</p>
28  *
29  * @author brian zimmer
30  * @author last revised by $Author: bzimmer $
31  * @version $Revision: 1.4 $
32  * @see java.sql.Statement#getUpdateCount()
33  */

34 public class UpdateCountDataHandler extends FilterDataHandler {
35
36     private static boolean once = false;
37
38     /**
39      * The update count for the last executed statement.
40      */

41     public int updateCount;
42
43     /**
44      * Handle capturing the update count. The initial value of the updateCount is
45      * <code>-1</code>.
46      */

47     public UpdateCountDataHandler(DataHandler datahandler) {
48
49         super(datahandler);
50
51         if (!once) {
52             Py.writeError("UpdateCountDataHandler", zxJDBC.getString("updateCountDeprecation"));
53             once = true;
54         }
55
56         this.updateCount = -1;
57     }
58
59     /**
60      * Sets the update count to <code>-1</code> prior to the statement being executed.
61      */

62     public void preExecute(Statement JavaDoc stmt) throws SQLException JavaDoc {
63
64         super.preExecute(stmt);
65
66         this.updateCount = -1;
67     }
68
69     /**
70      * Gets the update count from the statement after successfully executing.
71      */

72     public void postExecute(Statement JavaDoc stmt) throws SQLException JavaDoc {
73
74         super.postExecute(stmt);
75
76         this.updateCount = stmt.getUpdateCount();
77     }
78 }
79
Popular Tags