Source Code Cross Referenced for ReflectDataBase.java in  » Database-Client » SQLMinus » isql » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » SQLMinus » isql 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package isql;
002:
003:        import java.util.*;
004:        import java.lang.reflect.*;
005:        import java.sql.*;
006:        import util.*;
007:
008:        public class ReflectDataBase {
009:
010:            /** shoud be in format "methodname(params)" 
011:             * e.g. getTables(null, "root", "%", null);
012:             */
013:            public static void main(String args[]) {
014:                String str = null;
015:                if (args.length == 1)
016:                    str = args[0];
017:                else
018:                    str = "getTables(null,\"root\", \"%\", null)";
019:
020:                System.out.println("recvd:" + str);
021:                Connection conn = null;
022:                DatabaseMetaData dma = null;
023:                try {
024:                    //commented off since  i dont want this package 
025:                    //conn = com.t4h.util.mysqlUtil.getConnection();
026:                    dma = conn.getMetaData();
027:                    ReflectDataBase rd = new ReflectDataBase(dma, str);
028:                    Object o = rd.getResult();
029:                    System.out.println(o);
030:                } catch (Exception exc) {
031:                    System.err.println("MAIN:" + exc.toString());
032:                }
033:            }
034:
035:            Object _result;
036:
037:            // dma could just have been an object ??
038:            public ReflectDataBase(DatabaseMetaData dma, String str) {
039:                try {
040:                    //Class mainclass = Class.forName("java.sql.DatabaseMetaData");
041:                    Class mainclass = dma.getClass();
042:                    String[] sarr = PerlWrapper.perlMatch("\\s*(.*)\\((.*)\\)",
043:                            str);
044:                    // elem 0 has method name
045:                    // elem 1 has parameters
046:                    Method mainmethod = getMainMethod(mainclass, sarr[0]);
047:                    System.out.println(" main:" + mainmethod.getName());
048:                    for (int i = 0; i < sarr.length; i++) {
049:                        System.out.println("got:" + sarr[i]);
050:                    }
051:                    Object oparams[] = null;
052:                    Class[] classes = null;
053:                    // there are parameters
054:                    if (sarr.length == 2) {
055:                        if (sarr[1].trim().length() > 0) { // check that not blank
056:                            // break up params based on commas
057:                            String[] sparams = PerlWrapper.perlSplit(
058:                                    "/\\s*,\\s*/", sarr[1]);
059:                            int length = sparams.length;
060:                            for (int j = 0; j < length; j++) {
061:                                System.out.println("gotparams:" + sparams[j]);
062:                            }
063:                            oparams = new Object[length];
064:                            classes = new Class[length];
065:                            for (int i = 0; i < length; i++) {
066:                                String strp = sparams[i];
067:                                if (PerlWrapper.isMatching("^\\d+$", strp)) {
068:                                    //oparams[i] = Integer.valueOf(strp);
069:                                    oparams[i] = new Integer(Integer
070:                                            .parseInt(strp));
071:                                    classes[i] = int.class;
072:                                } else if (strp.equals("true")
073:                                        || strp.equals("false")) {
074:                                    oparams[i] = new Boolean(strp);
075:                                    classes[i] = boolean.class;
076:                                } else if (strp.equals("null")
077:                                        || strp.equals("NULL")) {
078:                                    System.out
079:                                            .println("oracle driver gives null");
080:                                    // find out the actual parameter!!!
081:                                    Class tmpc = String.class;
082:                                    classes[i] = String.class;
083:                                    try {
084:                                        classes[i] = getMethodParameterClass(
085:                                                mainmethod, i);
086:                                        tmpc = getMethodParameterClass(
087:                                                mainmethod, i);
088:                                    } catch (Exception exc) {
089:                                        System.err.println("Reflect: "
090:                                                + exc.toString());
091:                                    }
092:                                    if (!tmpc.isArray())
093:                                        oparams[i] = tmpc.newInstance();
094:                                    else
095:                                        oparams[i] = java.lang.reflect.Array
096:                                                .newInstance(tmpc
097:                                                        .getComponentType(), 0);
098:                                } else if (PerlWrapper.isMatching("\".*\"",
099:                                        strp))
100:                                    // string
101:                                    oparams[i] = strp.substring(1, strp
102:                                            .length() - 1);
103:                                else
104:                                // appears to be a constant
105:                                // which we need to reflect
106:                                {
107:                                    String[] elems = ArrayUtil.split(strp, '.');
108:                                    String classname = null;
109:                                    String fieldname = null;
110:                                    if (elems.length == 4) {
111:                                        classname = elems[0] + '.' + elems[1]
112:                                                + '.' + elems[2];
113:                                        fieldname = elems[3];
114:                                    } else if (elems.length == 2) {
115:                                        classname = "java.sql." + elems[0];
116:                                        fieldname = elems[1];
117:                                    } else {
118:                                        System.err
119:                                                .println("Dont know how to handle parameter! :"
120:                                                        + strp);
121:                                    }
122:                                    oparams[i] = getFieldValue(classname,
123:                                            fieldname);
124:                                } // constant
125:                                if (classes[i] == null)
126:                                    classes[i] = oparams[i].getClass();
127:                                System.out.println("Class=" + classes[i] + "="
128:                                        + oparams[i] + ".");
129:                            } // for i
130:                        } // if sarr >0
131:                    }// sarr == 2
132:                    System.out.println("reached 823");
133:                    Method m = mainclass.getMethod(sarr[0], classes);
134:                    System.out.println("reached 826" + m.getName() + ":"
135:                            + m.toString());
136:                    if (oparams != null)
137:                        for (int i = 0; i < oparams.length; i++) {
138:                            System.out.println("oparams=" + oparams[i]);
139:                        }
140:                    _result = m.invoke(dma, oparams);
141:                } catch (Exception exc) {
142:                    System.err.println("EXC:" + exc.toString());
143:                    _result = null;
144:                }
145:            }
146:
147:            public Object getResult() {
148:                return _result;
149:            }
150:
151:            /** a public static constant has been used, which we must try to
152:             * decipher.
153:             */
154:            public static Object getFieldValue(String classname,
155:                    String fieldname) throws ClassNotFoundException,
156:                    NoSuchFieldException, IllegalAccessException {
157:                Class c = Class.forName(classname);
158:                Field f = c.getDeclaredField(fieldname);
159:                return f.get(null);
160:            }
161:
162:            /** get the class of a particular parameter
163:             */
164:            public static Class getMethodParameterClass(Method m, int index) {
165:                System.err.println(" temp: inside getMethod param:"
166:                        + m.getName() + " " + index);
167:                Class[] pt = m.getParameterTypes();
168:                return pt[index];
169:            }
170:
171:            /** returns first match for method name, but what if overloaded.
172:             */
173:            public static Method getMainMethod(Class c, String methodname) {
174:                Method[] m = c.getDeclaredMethods();
175:                for (int i = 0; i < m.length; i++) {
176:                    if (m[i].getName().equals(methodname))
177:                        return m[i];
178:                }
179:                return null;
180:            }
181:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.