KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > web > struts > _ResultSet


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.ResultSet JavaDoc;
20
21 /**
22  * 结果集合的接管,用于处理字符集的转码
23  * @author Winter Lau
24  */

25 class _ResultSet implements InvocationHandler {
26
27     private final static Class JavaDoc[] infs = new Class JavaDoc[]{ResultSet JavaDoc.class};
28
29     public _ResultSet(ResultSet JavaDoc rs, boolean decode) {
30         this.rs = rs;
31         this.decode = decode;
32     }
33
34     /**
35      * 获取ResultSet的代理
36      * @return
37      */

38     public ResultSet JavaDoc getResultSet() {
39         return (ResultSet JavaDoc)Proxy.newProxyInstance(
40             rs.getClass().getClassLoader(), infs, this);
41     }
42
43     public Object JavaDoc invoke(Object JavaDoc proxy, Method m, Object JavaDoc args[]) throws Throwable JavaDoc
44     {
45         String JavaDoc method = m.getName();
46         if (decode && GETSTRING.equals(method))
47             try {
48                 String JavaDoc result = (String JavaDoc) m.invoke(rs, args);
49                 if (result != null)
50                     return new String JavaDoc(result.getBytes("8859_1"));
51                 else
52                     return null;
53             } catch (InvocationTargetException e) {
54                 throw e.getTargetException();
55             }
56         try {
57             return m.invoke(rs, args);
58         } catch (InvocationTargetException e) {
59             throw e.getTargetException();
60         }
61     }
62
63     private ResultSet JavaDoc rs;
64
65     private boolean decode;
66
67     private static final String JavaDoc GETSTRING = "getString";
68 }
Popular Tags