KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > web > struts > _Statement


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

16 package web.struts;
17
18 import java.lang.reflect.*;
19 import java.sql.PreparedStatement JavaDoc;
20 import java.sql.ResultSet JavaDoc;
21 import java.sql.Statement JavaDoc;
22
23 /**
24  * 数据库语句对象的代理
25  * @author Winter Lau
26  */

27 class _Statement implements InvocationHandler {
28
29     private final static Class JavaDoc[] infs = new Class JavaDoc[]{Statement JavaDoc.class};
30     private final static Class JavaDoc[] infs2 = new Class JavaDoc[]{PreparedStatement JavaDoc.class};
31
32     public _Statement(Statement JavaDoc stmt, boolean decode) {
33         statement = stmt;
34         this.decode = decode;
35     }
36
37     /**
38      * 获取Statement实例的代理
39      * @return
40      */

41     public Statement JavaDoc getStatement() {
42         if(statement instanceof PreparedStatement JavaDoc)
43         return (Statement JavaDoc)Proxy.newProxyInstance(
44             statement.getClass().getClassLoader(),infs2, this);
45         return (Statement JavaDoc)Proxy.newProxyInstance(
46                 statement.getClass().getClassLoader(),infs, this);
47     }
48
49     public Object JavaDoc invoke(Object JavaDoc proxy, Method m, Object JavaDoc args[]) throws Throwable JavaDoc {
50         String JavaDoc method = m.getName();
51         if (decode && SETSTRING.equals(method))
52             try {
53                 String JavaDoc param = (String JavaDoc) args[1];
54                 if (param != null)
55                     param = new String JavaDoc(param.getBytes(), "8859_1");
56                 return m.invoke(statement, new Object JavaDoc[] { args[0], param });
57             } catch (InvocationTargetException e) {
58                 throw e.getTargetException();
59             }
60         if (decode && EXECUTEQUERY.equals(method))
61             try {
62                 ResultSet JavaDoc rs = (ResultSet JavaDoc) m.invoke(statement, args);
63                 return (new _ResultSet(rs, decode)).getResultSet();
64             } catch (InvocationTargetException e) {
65                 throw e.getTargetException();
66             }
67         try {
68             return m.invoke(statement, args);
69         } catch (InvocationTargetException e) {
70             if (GETQUERYTIMEOUT.equals(method))
71                 return new Integer JavaDoc(0);
72             else
73                 throw e.getTargetException();
74         }
75     }
76
77     private Statement JavaDoc statement;
78
79     private boolean decode;
80
81     private static final String JavaDoc GETQUERYTIMEOUT = "getQueryTimeout";
82
83     private static final int gqt_return = 0;
84
85     private static final String JavaDoc SETSTRING = "setString";
86
87     private static final String JavaDoc EXECUTEQUERY = "executeQuery";
88 }
Popular Tags