Source Code Cross Referenced for StoredProcedureCall.java in  » Database-ORM » ORBroker » net » sourceforge » orbroker » 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 ORM » ORBroker » net.sourceforge.orbroker 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Created on Jun 24, 2004
003:         */
004:        package net.sourceforge.orbroker;
005:
006:        import java.sql.CallableStatement;
007:        import java.sql.Connection;
008:        import java.sql.ParameterMetaData;
009:        import java.sql.ResultSet;
010:        import java.sql.SQLException;
011:
012:        /**
013:         * @author Nils Kilden-Pedersen
014:         */
015:        final class StoredProcedureCall extends StaticStatement {
016:
017:            /**
018:             * Is statement a stored procedure call?
019:             * @param statement
020:             * @return true, if it's a CALL
021:             */
022:            static boolean isProcedureCall(String statement) {
023:                if (statement.length() > 4) {
024:                    statement = statement.substring(0, 4).toUpperCase();
025:                }
026:                return statement.startsWith("CALL")
027:                        || statement.startsWith("{")
028:                        || statement.startsWith(":")
029:                        || statement.startsWith("}");
030:            }
031:
032:            /**
033:             * Constructor.
034:             * @param id
035:             * @param statement
036:             * @param resultObjectDef
037:             */
038:            StoredProcedureCall(String id, String statement,
039:                    ResultObjectDefinition resultObjectDef) {
040:                super (id, statement, resultObjectDef);
041:            }
042:
043:            /**
044:             * For stored procedure calls.
045:             * @param context
046:             * @param cs
047:             * @param parsed
048:             * @return Parameter OUTPUT status
049:             * @throws SQLException
050:             */
051:            private boolean[] attachParameterValues(ConnectionContext context,
052:                    CallableStatement cs, ImmutableSQL parsed)
053:                    throws SQLException {
054:
055:                ParameterMetaData metaData = cs.getParameterMetaData();
056:                int parmCount = metaData.getParameterCount();
057:                if (parmCount != parsed.getParameterCount()) {
058:                    throw new BrokerException("Metadata parameter count of "
059:                            + parmCount + " does not match actual count of "
060:                            + parsed.getParameterCount());
061:                }
062:
063:                boolean[] isParameterOutput = new boolean[parmCount];
064:                for (int i = 0, parameterIndex = 1; i < parmCount; i++, parameterIndex++) {
065:                    String treePath = parsed.getParameterName(i);
066:                    int mode = metaData.getParameterMode(parameterIndex);
067:                    if (mode == ParameterMetaData.parameterModeIn
068:                            || mode == ParameterMetaData.parameterModeInOut) {
069:                        Object parmValue = context.getParameterValue(treePath);
070:                        setColumnValue(cs, parmValue, parameterIndex);
071:                    }
072:                    isParameterOutput[i] = (mode == ParameterMetaData.parameterModeInOut || mode == ParameterMetaData.parameterModeOut);
073:                    if (isParameterOutput[i]) {
074:                        cs.registerOutParameter(parameterIndex, metaData
075:                                .getParameterType(parameterIndex));
076:                    }
077:                }
078:                return isParameterOutput;
079:            }
080:
081:            private CallableStatement buildCallableStatement(Connection con,
082:                    boolean scrollableSupport, ImmutableSQL parsedSQL)
083:                    throws SQLException {
084:
085:                String statement = parsedSQL.getSqlStatement();
086:                logSqlStatement(statement);
087:                return con.prepareCall(statement, Broker
088:                        .getResultSetType(scrollableSupport),
089:                        ResultSet.CONCUR_READ_ONLY);
090:            }
091:
092:            /**
093:             * @param context
094:             * @param cs
095:             * @param parsedSQL
096:             * @param isParameterOutput
097:             * @throws SQLException
098:             */
099:            private void setOutputParameters(ConnectionContext context,
100:                    CallableStatement cs, ImmutableSQL parsedSQL,
101:                    boolean[] isParameterOutput) throws SQLException {
102:                ResultSet resultSet = new ResultParameters(cs,
103:                        isParameterOutput, parsedSQL.copyParameterNames());
104:                ResultRow row = new ResultRow(resultSet, context, cs
105:                        .getConnection());
106:                setValuesOnMap(context.getParametersAsMap(), row);
107:            }
108:
109:            /**
110:             * @inheritDoc
111:             * @throws SQLException
112:             */
113:            int[] executeBatch(Connection con, ConnectionContext context,
114:                    String batchParameterName, Object[] batchParameters)
115:                    throws SQLException {
116:
117:                ImmutableSQL sql = getRunnableSQL(context);
118:
119:                CallableStatement cs = buildCallableStatement(con, false, sql);
120:                try {
121:                    for (int i = 0; i < batchParameters.length; i++) {
122:                        context.setParameter(batchParameterName,
123:                                batchParameters[i]);
124:                        attachParameterValues(context, cs, sql);
125:                        cs.addBatch();
126:                    }
127:                    context.removeParameter(batchParameterName);
128:                    return cs.executeBatch();
129:                } finally {
130:                    closeStatement(cs);
131:                }
132:            }
133:
134:            /**
135:             * 
136:             * @inheritDoc
137:             * @throws SQLException
138:             */
139:            QueryResult executeQuery(Connection con, boolean scrollableSupport,
140:                    ConnectionContext context) throws SQLException {
141:
142:                ImmutableSQL parsedSQL = getRunnableSQL(context);
143:                CallableStatement cs = buildCallableStatement(con,
144:                        scrollableSupport, parsedSQL);
145:                boolean[] isParameterOutput = attachParameterValues(context,
146:                        cs, parsedSQL);
147:                ResultSet resultSet;
148:                cs.execute();
149:
150:                try {
151:                    resultSet = cs.getResultSet();
152:                } catch (SQLException e) {
153:                    resultSet = null;
154:                }
155:
156:                ResultSet outputParameters = new ResultParameters(cs,
157:                        isParameterOutput, parsedSQL.copyParameterNames());
158:
159:                if (resultSet == null) {
160:                    resultSet = outputParameters;
161:                    outputParameters = null;
162:                }
163:                return new QueryResult(cs, resultSet, context, outputParameters);
164:            }
165:
166:            /**
167:             * @inheritDoc
168:             * @see net.sourceforge.orbroker.Statement#executeUpdate(java.sql.Connection, net.sourceforge.orbroker.ConnectionContext)
169:             */
170:            int executeUpdate(Connection con, ConnectionContext context)
171:                    throws SQLException {
172:
173:                ImmutableSQL parsedSql = getRunnableSQL(context);
174:                CallableStatement cs = buildCallableStatement(con, false,
175:                        parsedSql);
176:                try {
177:                    boolean[] isParameterOutput = attachParameterValues(
178:                            context, cs, parsedSql);
179:                    int rowsAffected = cs.executeUpdate();
180:                    setOutputParameters(context, cs, parsedSql,
181:                            isParameterOutput);
182:                    return rowsAffected;
183:                } finally {
184:                    closeStatement(cs);
185:                }
186:            }
187:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.