Source Code Cross Referenced for DbQuery.java in  » Development » jodd » jodd » db » 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 » Development » jodd » jodd.db 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
0002:
0003:        package jodd.db;
0004:
0005:        import jodd.util.collection.IntArrayList;
0006:        import jodd.mutable.MutableByte;
0007:        import jodd.mutable.MutableDouble;
0008:        import jodd.mutable.MutableFloat;
0009:        import jodd.mutable.MutableInteger;
0010:        import jodd.mutable.MutableLong;
0011:        import jodd.mutable.MutableShort;
0012:        import jodd.bean.BeanUtil;
0013:
0014:        import java.sql.Array;
0015:        import java.sql.Blob;
0016:        import java.sql.Clob;
0017:        import java.sql.Connection;
0018:        import java.sql.Date;
0019:        import java.sql.PreparedStatement;
0020:        import java.sql.Ref;
0021:        import java.sql.ResultSet;
0022:        import java.sql.SQLException;
0023:        import java.sql.Statement;
0024:        import java.sql.Time;
0025:        import java.sql.Timestamp;
0026:        import java.sql.Types;
0027:        import java.util.Iterator;
0028:        import java.util.Map;
0029:        import java.util.Set;
0030:        import java.util.HashSet;
0031:        import java.math.BigDecimal;
0032:        import java.math.BigInteger;
0033:        import java.net.URL;
0034:
0035:        /**
0036:         * Encapsulates {@link Statement} and all its operations.
0037:         * <p>
0038:         * It may be:
0039:         * <ol>
0040:         * <li>used in an un-managed way, created directly from connection;</li>
0041:         * <li>managed by {@link DbSession};</li>
0042:         * <li>managed by {@link jodd.db.DbThreadSession}.</li>
0043:         * </ol>
0044:         */
0045:        public class DbQuery {
0046:
0047:            /**
0048:             * If set to <code>true</code> all created statements will be prepared.
0049:             */
0050:            public static boolean FORCE_PREPARED_STATEMENTS;
0051:
0052:            protected static int totalOpenResultSetCount;
0053:
0054:            /**
0055:             * Returns total number of open result sets.
0056:             * @see #getOpenResultSetCount() 
0057:             */
0058:            public static int getTotalOpenResultSetCount() {
0059:                return totalOpenResultSetCount;
0060:            }
0061:
0062:            // ---------------------------------------------------------------- params
0063:
0064:            protected Statement statement;
0065:            protected PreparedStatement preparedStatement;
0066:            protected Set<ResultSet> resultSets;
0067:            protected DbQueryMode mode;
0068:            protected DbQueryParser query;
0069:            protected Connection connection;
0070:            protected String sqlString;
0071:
0072:            protected DbSession session;
0073:            /**
0074:             * Default {@link DbQueryMode} for all queries.
0075:             */
0076:            public static DbQueryMode DEFAULT_QUERY_MODE = new DbQueryMode();
0077:
0078:            /**
0079:             * Returns query sql string. For prepared statmements, returns sql string with quick-and-dirty replaced values.
0080:             */
0081:            public String getQueryString() {
0082:                if ((preparedStatement != null)
0083:                        && (preparedStatement instanceof  LoggablePreparedStatement)) {
0084:                    return ((LoggablePreparedStatement) preparedStatement)
0085:                            .getQueryString();
0086:                }
0087:                if (query != null) {
0088:                    return query.sql;
0089:                }
0090:                return sqlString;
0091:            }
0092:
0093:            /**
0094:             * @see #getQueryString()
0095:             */
0096:            @Override
0097:            public String toString() {
0098:                return getQueryString();
0099:            }
0100:
0101:            // ---------------------------------------------------------------- ctors
0102:
0103:            /**
0104:             * Creates new query,
0105:             */
0106:            public DbQuery(Connection conn, String sqlString) {
0107:                init(conn, sqlString, DEFAULT_QUERY_MODE);
0108:            }
0109:
0110:            /**
0111:             * Creates new query.
0112:             */
0113:            public DbQuery(Connection conn, String sqlString, DbQueryMode mode) {
0114:                init(conn, sqlString, mode);
0115:            }
0116:
0117:            /**
0118:             * Creates a new query from {@link DbSession}.
0119:             */
0120:            public DbQuery(DbSession session, String sqlString, DbQueryMode mode) {
0121:                this .session = session;
0122:                session.attachQuery(this );
0123:                init(session.getConnection(), sqlString, mode);
0124:            }
0125:
0126:            /**
0127:             * Creates a new query from {@link DbSession}.
0128:             */
0129:            public DbQuery(DbSession session, String sqlString) {
0130:                this (session, sqlString, DEFAULT_QUERY_MODE);
0131:            }
0132:
0133:            /**
0134:             * Creates a new query from {@link DbThreadSession} that is
0135:             * assigned to current thread.
0136:             * @see jodd.db.DbThreadSession#getCurrentSession()
0137:             */
0138:            public DbQuery(String sqlString, DbQueryMode mode) {
0139:                DbSession session = DbThreadSession.getCurrentSession();
0140:                if (session == null) {
0141:                    throw new DbSqlException(
0142:                            "No session associated to current thread.");
0143:                }
0144:                session.attachQuery(this );
0145:                init(session.getConnection(), sqlString, mode);
0146:            }
0147:
0148:            /**
0149:             * Creates a new query from {@link DbThreadSession} that is
0150:             * assigned to current thread.
0151:             */
0152:            public DbQuery(String sqlString) {
0153:                this (sqlString, DEFAULT_QUERY_MODE);
0154:            }
0155:
0156:            /**
0157:             * Initializes query. Must be called from ctors.
0158:             */
0159:            protected void init(Connection connection, String sqlString,
0160:                    DbQueryMode mode) {
0161:                this .connection = connection;
0162:                this .mode = mode;
0163:                if (sqlString == null) {
0164:                    return;
0165:                }
0166:                init(sqlString);
0167:            }
0168:
0169:            protected void init(String sqlString) {
0170:                this .sqlString = sqlString;
0171:                this .query = new DbQueryParser(sqlString);
0172:                if (statement == null) {
0173:                    if ((FORCE_PREPARED_STATEMENTS == false)
0174:                            && (query.prepared == false)) {
0175:                        try {
0176:                            statement = connection.createStatement(mode
0177:                                    .getType(), mode.getConcurrencyType());
0178:                        } catch (SQLException sex) {
0179:                            throw new DbSqlException(
0180:                                    "Unable to create statement.", sex);
0181:                        }
0182:                    } else {
0183:                        try {
0184:                            if (mode.isDebug() == true) {
0185:                                statement = new LoggablePreparedStatement(
0186:                                        connection, query.sql, mode.getType(),
0187:                                        mode.getConcurrencyType());
0188:                            } else {
0189:                                statement = connection.prepareStatement(
0190:                                        query.sql, mode.getType(), mode
0191:                                                .getConcurrencyType());
0192:                            }
0193:                        } catch (SQLException sex) {
0194:                            throw new DbSqlException(
0195:                                    "Unable to create prepared statement.", sex);
0196:                        }
0197:                        preparedStatement = (PreparedStatement) statement;
0198:                    }
0199:                }
0200:            }
0201:
0202:            /**
0203:             * Resets an used query, using the same connection, sql query string and {@link DbQueryMode query mode},
0204:             * Closes current statement and creates a new one.  
0205:             */
0206:            public void reset() {
0207:                checkActive();
0208:                init(connection, sqlString, mode);
0209:            }
0210:
0211:            /**
0212:             * Returns <code>true</code> if query is closed.
0213:             */
0214:            public boolean isClosed() {
0215:                return (statement == null);
0216:            }
0217:
0218:            /**
0219:             * Returns <code>true</code> if query is opened.
0220:             */
0221:            public boolean isActive() {
0222:                return (statement != null);
0223:            }
0224:
0225:            protected void checkActive() {
0226:                if (statement == null) {
0227:                    throw new DbSqlException("Query is closed.");
0228:                }
0229:            }
0230:
0231:            /**
0232:             * Closes a query.
0233:             * @see #close()
0234:             */
0235:            protected void closeQuery() {
0236:                if (resultSets != null) {
0237:                    for (ResultSet rs : resultSets) {
0238:                        try {
0239:                            totalOpenResultSetCount--;
0240:                            rs.close();
0241:                        } catch (SQLException sex) {
0242:                            // ignore
0243:                        }
0244:                    }
0245:                    resultSets.clear();
0246:                    resultSets = null;
0247:                }
0248:                if (statement != null) {
0249:                    try {
0250:                        statement.close();
0251:                    } catch (SQLException sex) {
0252:                        // ignore
0253:                    }
0254:                    statement = null;
0255:                }
0256:                query = null;
0257:            }
0258:
0259:            /**
0260:             * Closes the query and all created results sets and detaches itself from session.
0261:             * No further usage is possible after the query is closed!
0262:             */
0263:            public void close() {
0264:                closeQuery();
0265:                connection = null;
0266:                if (this .session != null) {
0267:                    this .session.detachQuery(this );
0268:                }
0269:            }
0270:
0271:            /**
0272:             * Closes single result set that was created by this query,
0273:             * It is not neccessary to close result sets explicitly, since
0274:             * {@link #close()} closes all created result sets.
0275:             * However, statement (e.g. query) remains open.
0276:             */
0277:            public void close(ResultSet rs) {
0278:                if (rs == null) {
0279:                    return;
0280:                }
0281:                if (resultSets.remove(rs) == false) {
0282:                    throw new DbSqlException(
0283:                            "Provided ResultSet is not created by this query and can not be closed.");
0284:                }
0285:                try {
0286:                    totalOpenResultSetCount--;
0287:                    rs.close();
0288:                } catch (SQLException sex) {
0289:                    // ignore
0290:                }
0291:            }
0292:
0293:            /**
0294:             * Returns {@link jodd.db.DbSession} used for creating this query.
0295:             * If no session used, returns <code>null</code>.
0296:             */
0297:            public DbSession getSession() {
0298:                return session;
0299:            }
0300:
0301:            // ---------------------------------------------------------------- behaviour
0302:
0303:            protected int fetchSize;
0304:
0305:            /**
0306:             * Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when
0307:             * more rows are needed. The number of rows specified affects only result sets created using this statement.
0308:             * If the value specified is zero, then the hint is ignored. The default value is zero.
0309:             * @see Statement#setFetchSize(int)
0310:             */
0311:            public DbQuery setFetchSize(int rows) {
0312:                this .fetchSize = rows;
0313:                try {
0314:                    statement.setFetchSize(fetchSize);
0315:                } catch (SQLException sex) {
0316:                    throw new DbSqlException("Unable to set fetch size of '"
0317:                            + fetchSize + '\'', sex);
0318:                }
0319:                return this ;
0320:            }
0321:
0322:            /**
0323:             * Sets the limit for the maximum number of rows that any ResultSet object can contain to the given number.
0324:             * If the limit is exceeded, the excess rows are silently dropped.
0325:             * @see Statement#setMaxRows(int)
0326:             */
0327:            public DbQuery setMaxRows(int maxRows) {
0328:                try {
0329:                    statement.setMaxRows(maxRows);
0330:                } catch (SQLException sex) {
0331:                    throw new DbSqlException("Unable to set max rows of '"
0332:                            + maxRows + '\'', sex);
0333:                }
0334:                return this ;
0335:            }
0336:
0337:            // ---------------------------------------------------------------- execute
0338:
0339:            /**
0340:             * Executes query. If this method is invoked at least once, the query or
0341:             * all created resultsets must be explicitly closed at the end of query usage.
0342:             * This can be done explicitly by calling {@link #close(java.sql.ResultSet)}
0343:             * оr implicitly, during {@link #close()}.
0344:             * The fetch size is set before execution.
0345:             * @see Statement#execute(String)
0346:             */
0347:            public ResultSet execute() {
0348:                checkActive();
0349:                ResultSet rs = null;
0350:                try {
0351:                    if (preparedStatement == null) {
0352:                        rs = statement.executeQuery(query.sql);
0353:                    } else {
0354:                        rs = preparedStatement.executeQuery();
0355:                    }
0356:                    rs.setFetchSize(fetchSize);
0357:                } catch (SQLException sex) {
0358:                    if (rs != null) {
0359:                        try {
0360:                            rs.close();
0361:                        } catch (SQLException sex2) {
0362:                            // ignore
0363:                        }
0364:                    }
0365:                    throw new DbSqlException("Unable to execute query.", sex);
0366:                }
0367:                if (resultSets == null) {
0368:                    resultSets = new HashSet<ResultSet>();
0369:                }
0370:                resultSets.add(rs);
0371:                totalOpenResultSetCount++;
0372:                return rs;
0373:            }
0374:
0375:            /**
0376:             * Returns number of created result sets that are still not explicitly closed.
0377:             * @see #getTotalOpenResultSetCount() 
0378:             */
0379:            public int getOpenResultSetCount() {
0380:                return resultSets == null ? 0 : resultSets.size();
0381:            }
0382:
0383:            /**
0384:             * Executes UPDATE, INSERT or DELETE queries.
0385:             * @see Statement#executeUpdate(String)
0386:             */
0387:            public int executeUpdate() {
0388:                return executeUpdate(false);
0389:            }
0390:
0391:            /**
0392:             * Executes UPDATE, INSERT or DELETE queries and closes query afterwards.
0393:             * @see Statement#executeUpdate(String)
0394:             */
0395:            public int executeUpdateAndClose() {
0396:                return executeUpdate(true);
0397:            }
0398:
0399:            /**
0400:             * Executes UPDATE, INSERT or DELETE queries and optionally
0401:             * closes the query.
0402:             * @see Statement#executeUpdate(String)
0403:             */
0404:            public int executeUpdate(boolean closeQuery) {
0405:                checkActive();
0406:                int result;
0407:                try {
0408:                    if (preparedStatement == null) {
0409:                        result = statement.executeUpdate(query.sql);
0410:                    } else {
0411:                        result = preparedStatement.executeUpdate();
0412:                    }
0413:                } catch (SQLException sex) {
0414:                    throw new DbSqlException("Unable to execute query.", sex);
0415:                }
0416:                if (closeQuery) {
0417:                    close();
0418:                }
0419:                return result;
0420:            }
0421:
0422:            /**
0423:             * Special {@link #execute()} for 'select count(*)' queries.
0424:             * Query <b>is not</b> closed after the execution.
0425:             * <p>
0426:             * It doesn't check if query is really a count query.
0427:             * Before execution it sets fetch size to <code>1</code>.
0428:             * If resultset returns zero rows, (what is very unlikely),
0429:             * it returs <code>-1</code>.
0430:             */
0431:            public long executeCount() {
0432:                return executeCount(false);
0433:            }
0434:
0435:            /**
0436:             * Executes count queries and closes afterwards.
0437:             */
0438:            public long executeCountAndClose() {
0439:                return executeCount(true);
0440:            }
0441:
0442:            public long executeCount(boolean close) {
0443:                checkActive();
0444:                setFetchSize(1);
0445:                ResultSet rs = null;
0446:                try {
0447:                    if (preparedStatement == null) {
0448:                        rs = statement.executeQuery(query.sql);
0449:                    } else {
0450:                        rs = preparedStatement.executeQuery();
0451:                    }
0452:                    totalOpenResultSetCount++;
0453:                    rs.setFetchSize(fetchSize);
0454:                    if (rs.next() == true) {
0455:                        return rs.getLong(1);
0456:                    }
0457:                } catch (SQLException sex) {
0458:                    throw new DbSqlException("Unable to execute count query.",
0459:                            sex);
0460:                } finally {
0461:                    if (rs != null) {
0462:                        try {
0463:                            totalOpenResultSetCount--;
0464:                            rs.close();
0465:                        } catch (SQLException sex) {
0466:                            // ignore
0467:                        }
0468:                    }
0469:                    if (close) {
0470:                        close();
0471:                    }
0472:                }
0473:                return -1;
0474:            }
0475:
0476:            // ---------------------------------------------------------------- methods for setting statement parameters
0477:
0478:            private void throwSetError(int index, SQLException sex) {
0479:                throw new DbSqlException("Unable to set parameter with index '"
0480:                        + index + "'.", sex);
0481:            }
0482:
0483:            // ---------------------------------------------------------------- null
0484:
0485:            public void setNull(int index, int type) {
0486:                try {
0487:                    preparedStatement.setNull(index, type);
0488:                } catch (SQLException sex) {
0489:                    throw new DbSqlException("Unable to set parameter '"
0490:                            + index + "' with null value.", sex);
0491:                }
0492:            }
0493:
0494:            public void setNull(String param, int value) {
0495:                IntArrayList positions = query
0496:                        .getExistingNamedParameterPositions(param);
0497:                for (int i = 0; i < positions.size(); i++) {
0498:                    setNull(positions.get(i), value);
0499:                }
0500:            }
0501:
0502:            // ---------------------------------------------------------------- int
0503:
0504:            public void setInteger(int index, int value) {
0505:                try {
0506:                    preparedStatement.setInt(index, value);
0507:                } catch (SQLException sex) {
0508:                    throwSetError(index, sex);
0509:                }
0510:            }
0511:
0512:            public void setInteger(String param, int value) {
0513:                IntArrayList positions = query
0514:                        .getExistingNamedParameterPositions(param);
0515:                for (int i = 0; i < positions.size(); i++) {
0516:                    setInteger(positions.get(i), value);
0517:                }
0518:            }
0519:
0520:            // ---------------------------------------------------------------- Integer
0521:
0522:            public void setInteger(int index, Number value) {
0523:                if (value == null) {
0524:                    setNull(index, Types.INTEGER);
0525:                    return;
0526:                }
0527:                setInteger(index, value.intValue());
0528:            }
0529:
0530:            public void setInteger(String param, Number value) {
0531:                if (value == null) {
0532:                    setNull(param, Types.INTEGER);
0533:                    return;
0534:                }
0535:                setInteger(param, value.intValue());
0536:            }
0537:
0538:            // ---------------------------------------------------------------- boolean
0539:
0540:            public void setBoolean(int index, boolean value) {
0541:                try {
0542:                    preparedStatement.setBoolean(index, value);
0543:                } catch (SQLException sex) {
0544:                    throwSetError(index, sex);
0545:                }
0546:            }
0547:
0548:            public void setBoolean(String param, boolean value) {
0549:                IntArrayList positions = query
0550:                        .getExistingNamedParameterPositions(param);
0551:                for (int i = 0; i < positions.size(); i++) {
0552:                    setBoolean(positions.get(i), value);
0553:                }
0554:            }
0555:
0556:            // ---------------------------------------------------------------- Boolean
0557:
0558:            public void setBoolean(int index, Boolean value) {
0559:                if (value == null) {
0560:                    setNull(index, Types.BOOLEAN);
0561:                    return;
0562:                }
0563:                setBoolean(index, value.booleanValue());
0564:            }
0565:
0566:            public void setBoolean(String param, Boolean value) {
0567:                if (value == null) {
0568:                    setNull(param, Types.BOOLEAN);
0569:                    return;
0570:                }
0571:                setBoolean(param, value.booleanValue());
0572:            }
0573:
0574:            // ---------------------------------------------------------------- long
0575:
0576:            public void setLong(int index, long value) {
0577:                try {
0578:                    preparedStatement.setLong(index, value);
0579:                } catch (SQLException sex) {
0580:                    throwSetError(index, sex);
0581:                }
0582:            }
0583:
0584:            public void setLong(String param, long value) {
0585:                IntArrayList positions = query
0586:                        .getExistingNamedParameterPositions(param);
0587:                for (int i = 0; i < positions.size(); i++) {
0588:                    setLong(positions.get(i), value);
0589:                }
0590:            }
0591:
0592:            // ---------------------------------------------------------------- Long
0593:
0594:            public void setLong(int index, Number value) {
0595:                if (value == null) {
0596:                    setNull(index, Types.INTEGER);
0597:                    return;
0598:                }
0599:                setLong(index, value.longValue());
0600:            }
0601:
0602:            public void setLong(String param, Number value) {
0603:                if (value == null) {
0604:                    setNull(param, Types.INTEGER);
0605:                    return;
0606:                }
0607:                setLong(param, value.longValue());
0608:            }
0609:
0610:            // ---------------------------------------------------------------- byte
0611:
0612:            public void setByte(int index, byte value) {
0613:                try {
0614:                    preparedStatement.setByte(index, value);
0615:                } catch (SQLException sex) {
0616:                    throwSetError(index, sex);
0617:                }
0618:            }
0619:
0620:            public void setByte(String param, byte value) {
0621:                IntArrayList positions = query
0622:                        .getExistingNamedParameterPositions(param);
0623:                for (int i = 0; i < positions.size(); i++) {
0624:                    setByte(positions.get(i), value);
0625:                }
0626:            }
0627:
0628:            // ---------------------------------------------------------------- Byte
0629:
0630:            public void setByte(int index, Number value) {
0631:                if (value == null) {
0632:                    setNull(index, Types.SMALLINT);
0633:                    return;
0634:                }
0635:                setByte(index, value.byteValue());
0636:            }
0637:
0638:            public void setByte(String param, Number value) {
0639:                if (value == null) {
0640:                    setNull(param, Types.SMALLINT);
0641:                    return;
0642:                }
0643:                setByte(param, value.byteValue());
0644:            }
0645:
0646:            // ---------------------------------------------------------------- bytes[]
0647:
0648:            private void setBytesNoEx(int index, byte[] value) {
0649:                try {
0650:                    preparedStatement.setBytes(index, value);
0651:                } catch (SQLException sex) {
0652:                    throwSetError(index, sex);
0653:                }
0654:            }
0655:
0656:            public void setBytes(int index, byte[] value) {
0657:                if (value == null) {
0658:                    setNull(index, Types.ARRAY);
0659:                    return;
0660:                }
0661:                setBytesNoEx(index, value);
0662:            }
0663:
0664:            public void setBytes(String param, byte[] value) {
0665:                if (value == null) {
0666:                    setNull(param, Types.ARRAY);
0667:                    return;
0668:                }
0669:                IntArrayList positions = query
0670:                        .getExistingNamedParameterPositions(param);
0671:                for (int i = 0; i < positions.size(); i++) {
0672:                    setBytesNoEx(positions.get(i), value);
0673:                }
0674:            }
0675:
0676:            // ---------------------------------------------------------------- double
0677:
0678:            public void setDouble(int index, double value) {
0679:                try {
0680:                    preparedStatement.setDouble(index, value);
0681:                } catch (SQLException sex) {
0682:                    throwSetError(index, sex);
0683:                }
0684:            }
0685:
0686:            public void setDouble(String param, double value) {
0687:                IntArrayList positions = query
0688:                        .getExistingNamedParameterPositions(param);
0689:                for (int i = 0; i < positions.size(); i++) {
0690:                    setDouble(positions.get(i), value);
0691:                }
0692:            }
0693:
0694:            // ---------------------------------------------------------------- Double
0695:
0696:            public void setDouble(int index, Number value) {
0697:                if (value == null) {
0698:                    setNull(index, Types.DOUBLE);
0699:                    return;
0700:                }
0701:                setDouble(index, value.doubleValue());
0702:            }
0703:
0704:            public void setDouble(String param, Number value) {
0705:                if (value == null) {
0706:                    setNull(param, Types.DOUBLE);
0707:                    return;
0708:                }
0709:                setDouble(param, value.doubleValue());
0710:            }
0711:
0712:            // ---------------------------------------------------------------- float
0713:
0714:            public void setFloat(int index, float value) {
0715:                try {
0716:                    preparedStatement.setFloat(index, value);
0717:                } catch (SQLException sex) {
0718:                    throwSetError(index, sex);
0719:                }
0720:            }
0721:
0722:            public void setFloat(String param, float value) {
0723:                IntArrayList positions = query
0724:                        .getExistingNamedParameterPositions(param);
0725:                for (int i = 0; i < positions.size(); i++) {
0726:                    setFloat(positions.get(i), value);
0727:                }
0728:            }
0729:
0730:            // ---------------------------------------------------------------- Float
0731:
0732:            public void setFloat(int index, Number value) {
0733:                if (value == null) {
0734:                    setNull(index, Types.FLOAT);
0735:                    return;
0736:                }
0737:                setFloat(index, value.floatValue());
0738:            }
0739:
0740:            public void setFloat(String param, Number value) {
0741:                if (value == null) {
0742:                    setNull(param, Types.FLOAT);
0743:                    return;
0744:                }
0745:                setFloat(param, value.floatValue());
0746:            }
0747:
0748:            // ---------------------------------------------------------------- short
0749:
0750:            public void setShort(int index, short value) {
0751:                try {
0752:                    preparedStatement.setShort(index, value);
0753:                } catch (SQLException sex) {
0754:                    throwSetError(index, sex);
0755:                }
0756:            }
0757:
0758:            public void setShort(String param, short value) {
0759:                IntArrayList positions = query
0760:                        .getExistingNamedParameterPositions(param);
0761:                for (int i = 0; i < positions.size(); i++) {
0762:                    setShort(positions.get(i), value);
0763:                }
0764:            }
0765:
0766:            // ---------------------------------------------------------------- Short
0767:
0768:            public void setShort(int index, Number value) {
0769:                if (value == null) {
0770:                    setNull(index, Types.SMALLINT);
0771:                    return;
0772:                }
0773:                setShort(index, value.shortValue());
0774:            }
0775:
0776:            public void setShort(String param, Number value) {
0777:                if (value == null) {
0778:                    setNull(param, Types.SMALLINT);
0779:                    return;
0780:                }
0781:                setShort(param, value.shortValue());
0782:            }
0783:
0784:            // ---------------------------------------------------------------- string
0785:
0786:            private void setStringNoEx(int index, String value) {
0787:                try {
0788:                    preparedStatement.setString(index, value);
0789:                } catch (SQLException sex) {
0790:                    throwSetError(index, sex);
0791:                }
0792:            }
0793:
0794:            public void setString(int index, String value) {
0795:                if (value == null) {
0796:                    setNull(index, Types.VARCHAR);
0797:                    return;
0798:                }
0799:                setStringNoEx(index, value);
0800:            }
0801:
0802:            public void setString(String param, String value) {
0803:                if (value == null) {
0804:                    setNull(param, Types.VARCHAR);
0805:                    return;
0806:                }
0807:                IntArrayList positions = query
0808:                        .getExistingNamedParameterPositions(param);
0809:                for (int i = 0; i < positions.size(); i++) {
0810:                    setStringNoEx(positions.get(i), value);
0811:                }
0812:            }
0813:
0814:            // ---------------------------------------------------------------- date
0815:
0816:            private void setDateNoEx(int index, java.sql.Date value) {
0817:                try {
0818:                    preparedStatement.setDate(index, value);
0819:                } catch (SQLException sex) {
0820:                    throwSetError(index, sex);
0821:                }
0822:            }
0823:
0824:            public void setDate(int index, java.sql.Date value) {
0825:                if (value == null) {
0826:                    setNull(index, Types.DATE);
0827:                    return;
0828:                }
0829:                setDateNoEx(index, value);
0830:            }
0831:
0832:            public void setDate(String param, java.sql.Date value) {
0833:                if (value == null) {
0834:                    setNull(param, Types.DATE);
0835:                    return;
0836:                }
0837:                IntArrayList positions = query
0838:                        .getExistingNamedParameterPositions(param);
0839:                for (int i = 0; i < positions.size(); i++) {
0840:                    setDateNoEx(positions.get(i), value);
0841:                }
0842:            }
0843:
0844:            // ---------------------------------------------------------------- time
0845:
0846:            private void setTimeNoEx(int index, java.sql.Time value) {
0847:                try {
0848:                    preparedStatement.setTime(index, value);
0849:                } catch (SQLException sex) {
0850:                    throwSetError(index, sex);
0851:                }
0852:            }
0853:
0854:            public void setTime(int index, java.sql.Time value) {
0855:                if (value == null) {
0856:                    setNull(index, Types.TIME);
0857:                    return;
0858:                }
0859:                setTimeNoEx(index, value);
0860:            }
0861:
0862:            public void setTime(String param, java.sql.Time value) {
0863:                if (value == null) {
0864:                    setNull(param, Types.TIME);
0865:                    return;
0866:                }
0867:                IntArrayList positions = query
0868:                        .getExistingNamedParameterPositions(param);
0869:                for (int i = 0; i < positions.size(); i++) {
0870:                    setTimeNoEx(positions.get(i), value);
0871:                }
0872:            }
0873:
0874:            // ---------------------------------------------------------------- timestamp
0875:
0876:            private void setTimestampNoEx(int index, java.sql.Timestamp value) {
0877:                try {
0878:                    preparedStatement.setTimestamp(index, value);
0879:                } catch (SQLException sex) {
0880:                    throwSetError(index, sex);
0881:                }
0882:            }
0883:
0884:            public void setTimestamp(int index, java.sql.Timestamp value) {
0885:                if (value == null) {
0886:                    setNull(index, Types.TIMESTAMP);
0887:                    return;
0888:                }
0889:                setTimestampNoEx(index, value);
0890:            }
0891:
0892:            public void setTimestamp(String param, java.sql.Timestamp value) {
0893:                if (value == null) {
0894:                    setNull(param, Types.TIMESTAMP);
0895:                    return;
0896:                }
0897:                IntArrayList positions = query
0898:                        .getExistingNamedParameterPositions(param);
0899:                for (int i = 0; i < positions.size(); i++) {
0900:                    setTimestampNoEx(positions.get(i), value);
0901:                }
0902:            }
0903:
0904:            // ---------------------------------------------------------------- big decimal
0905:
0906:            private void setBigDecimalNoEx(int index, BigDecimal value) {
0907:                try {
0908:                    preparedStatement.setBigDecimal(index, value);
0909:                } catch (SQLException sex) {
0910:                    throwSetError(index, sex);
0911:                }
0912:            }
0913:
0914:            public void setBigDecimal(int index, BigDecimal value) {
0915:                if (value == null) {
0916:                    setNull(index, Types.DECIMAL);
0917:                    return;
0918:                }
0919:                setBigDecimalNoEx(index, value);
0920:            }
0921:
0922:            public void setBigDecimal(String param, BigDecimal value) {
0923:                if (value == null) {
0924:                    setNull(param, Types.DECIMAL);
0925:                    return;
0926:                }
0927:                IntArrayList positions = query
0928:                        .getExistingNamedParameterPositions(param);
0929:                for (int i = 0; i < positions.size(); i++) {
0930:                    setBigDecimalNoEx(positions.get(i), value);
0931:                }
0932:            }
0933:
0934:            // ---------------------------------------------------------------- big integer
0935:
0936:            public void setBigInteger(int index, BigInteger value) {
0937:                if (value == null) {
0938:                    setNull(index, Types.NUMERIC);
0939:                    return;
0940:                }
0941:                setLong(index, value.longValue());
0942:            }
0943:
0944:            public void setBigInteger(String param, BigInteger value) {
0945:                if (value == null) {
0946:                    setNull(param, Types.NUMERIC);
0947:                    return;
0948:                }
0949:                IntArrayList positions = query
0950:                        .getExistingNamedParameterPositions(param);
0951:                for (int i = 0; i < positions.size(); i++) {
0952:                    setLong(positions.get(i), value.longValue());
0953:                }
0954:            }
0955:
0956:            // ---------------------------------------------------------------- URL
0957:
0958:            private void setURLNoEx(int index, URL value) {
0959:                try {
0960:                    preparedStatement.setURL(index, value);
0961:                } catch (SQLException sex) {
0962:                    throwSetError(index, sex);
0963:                }
0964:            }
0965:
0966:            public void setURL(int index, URL value) {
0967:                if (value == null) {
0968:                    setNull(index, Types.NULL);
0969:                    return;
0970:                }
0971:                setURLNoEx(index, value);
0972:            }
0973:
0974:            public void setURL(String param, URL value) {
0975:                if (value == null) {
0976:                    setNull(param, Types.NULL);
0977:                    return;
0978:                }
0979:                IntArrayList positions = query
0980:                        .getExistingNamedParameterPositions(param);
0981:                for (int i = 0; i < positions.size(); i++) {
0982:                    setURLNoEx(positions.get(i), value);
0983:                }
0984:            }
0985:
0986:            // ---------------------------------------------------------------- BLOB
0987:
0988:            private void setBlobNoEx(int index, Blob value) {
0989:                try {
0990:                    preparedStatement.setBlob(index, value);
0991:                } catch (SQLException sex) {
0992:                    throwSetError(index, sex);
0993:                }
0994:            }
0995:
0996:            public void setBlob(int index, Blob value) {
0997:                if (value == null) {
0998:                    setNull(index, Types.BLOB);
0999:                    return;
1000:                }
1001:                setBlobNoEx(index, value);
1002:            }
1003:
1004:            public void setBlob(String param, Blob value) {
1005:                if (value == null) {
1006:                    setNull(param, Types.BLOB);
1007:                    return;
1008:                }
1009:                IntArrayList positions = query
1010:                        .getExistingNamedParameterPositions(param);
1011:                for (int i = 0; i < positions.size(); i++) {
1012:                    setBlobNoEx(positions.get(i), value);
1013:                }
1014:            }
1015:
1016:            // ---------------------------------------------------------------- CLOB
1017:
1018:            private void setClobNoEx(int index, Clob value) {
1019:                try {
1020:                    preparedStatement.setClob(index, value);
1021:                } catch (SQLException sex) {
1022:                    throwSetError(index, sex);
1023:                }
1024:            }
1025:
1026:            public void setClob(int index, Clob value) {
1027:                if (value == null) {
1028:                    setNull(index, Types.CLOB);
1029:                    return;
1030:                }
1031:                setClobNoEx(index, value);
1032:            }
1033:
1034:            public void setClob(String param, Clob value) {
1035:                if (value == null) {
1036:                    setNull(param, Types.CLOB);
1037:                    return;
1038:                }
1039:                IntArrayList positions = query
1040:                        .getExistingNamedParameterPositions(param);
1041:                for (int i = 0; i < positions.size(); i++) {
1042:                    setClobNoEx(positions.get(i), value);
1043:                }
1044:            }
1045:
1046:            // ---------------------------------------------------------------- Array
1047:
1048:            private void setArrayNoEx(int index, Array value) {
1049:                try {
1050:                    preparedStatement.setArray(index, value);
1051:                } catch (SQLException sex) {
1052:                    throwSetError(index, sex);
1053:                }
1054:            }
1055:
1056:            public void setArray(int index, Array value) {
1057:                if (value == null) {
1058:                    setNull(index, Types.ARRAY);
1059:                    return;
1060:                }
1061:                setArrayNoEx(index, value);
1062:            }
1063:
1064:            public void setArray(String param, Array value) {
1065:                if (value == null) {
1066:                    setNull(param, Types.ARRAY);
1067:                    return;
1068:                }
1069:                IntArrayList positions = query
1070:                        .getExistingNamedParameterPositions(param);
1071:                for (int i = 0; i < positions.size(); i++) {
1072:                    setArrayNoEx(positions.get(i), value);
1073:                }
1074:            }
1075:
1076:            // ---------------------------------------------------------------- Ref
1077:
1078:            private void setRefNoEx(int index, Ref value) {
1079:                try {
1080:                    preparedStatement.setRef(index, value);
1081:                } catch (SQLException sex) {
1082:                    throwSetError(index, sex);
1083:                }
1084:            }
1085:
1086:            public void setRef(int index, Ref value) {
1087:                if (value == null) {
1088:                    setNull(index, Types.REF);
1089:                    return;
1090:                }
1091:                setRefNoEx(index, value);
1092:            }
1093:
1094:            public void setRef(String param, Ref value) {
1095:                if (value == null) {
1096:                    setNull(param, Types.REF);
1097:                    return;
1098:                }
1099:                IntArrayList positions = query
1100:                        .getExistingNamedParameterPositions(param);
1101:                for (int i = 0; i < positions.size(); i++) {
1102:                    setRefNoEx(positions.get(i), value);
1103:                }
1104:            }
1105:
1106:            // ---------------------------------------------------------------- beans
1107:
1108:            /**
1109:             * Sets bean parameters from bean. Non-existing bean properties are ignored.
1110:             */
1111:            public void setBean(String beanName, Object bean) {
1112:                beanName += '.';
1113:                Iterator it = query.iterateNamedParameters();
1114:                while (it.hasNext()) {
1115:                    String paramName = (String) it.next();
1116:                    if (paramName.startsWith(beanName) == true) {
1117:                        String propertyName = paramName.substring(beanName
1118:                                .length());
1119:                        if (BeanUtil.hasDeclaredProperty(bean, propertyName) == true) {
1120:                            Object value = BeanUtil.getDeclaredProperty(bean,
1121:                                    propertyName);
1122:                            setObject(paramName, value);
1123:                        }
1124:                    }
1125:                }
1126:            }
1127:
1128:            /**
1129:             * Sets bean parameters from map. Non existing bean properties are ignored.
1130:             */
1131:            public void setBean(String beanName, Map parameters) {
1132:                beanName += '.';
1133:                Iterator it = query.iterateNamedParameters();
1134:                while (it.hasNext()) {
1135:                    String paramName = (String) it.next();
1136:                    if (paramName.startsWith(beanName) == true) {
1137:                        String propertyName = paramName.substring(beanName
1138:                                .length());
1139:                        if (parameters.containsKey(propertyName)) {
1140:                            setObject(paramName, parameters.get(propertyName));
1141:                        }
1142:                    }
1143:                }
1144:            }
1145:
1146:            // ---------------------------------------------------------------- objects
1147:
1148:            private void setObjectNoEx(int index, Object value) {
1149:                try {
1150:                    preparedStatement.setObject(index, value);
1151:                } catch (SQLException sex) {
1152:                    throwSetError(index, sex);
1153:                }
1154:            }
1155:
1156:            public void setObject(int index, Object value) {
1157:                if (value == null) {
1158:                    setNull(index, Types.NULL);
1159:                    return;
1160:                }
1161:                Class type = value.getClass();
1162:                if (type == String.class)
1163:                    setString(index, (String) value);
1164:                else if (type == Integer.class)
1165:                    setInteger(index, (Integer) value);
1166:                else if (type == MutableInteger.class)
1167:                    setInteger(index, (MutableInteger) value);
1168:                else if (type == Long.class)
1169:                    setLong(index, (Long) value);
1170:                else if (type == MutableLong.class)
1171:                    setLong(index, (MutableLong) value);
1172:                else if (type == Double.class)
1173:                    setDouble(index, (Double) value);
1174:                else if (type == MutableDouble.class)
1175:                    setDouble(index, (MutableDouble) value);
1176:                else if (type == Float.class)
1177:                    setFloat(index, (Float) value);
1178:                else if (type == MutableFloat.class)
1179:                    setFloat(index, (MutableFloat) value);
1180:
1181:                else if (type == Byte.class)
1182:                    setByte(index, (Byte) value);
1183:                else if (type == MutableByte.class)
1184:                    setByte(index, (MutableByte) value);
1185:                else if (type == Short.class)
1186:                    setShort(index, (Short) value);
1187:                else if (type == MutableShort.class)
1188:                    setShort(index, (MutableShort) value);
1189:                else if (type == Boolean.class)
1190:                    setBoolean(index, (Boolean) value);
1191:                else if (type == BigInteger.class)
1192:                    setBigInteger(index, (BigInteger) value);
1193:                else if (type == BigDecimal.class)
1194:                    setBigDecimal(index, (BigDecimal) value);
1195:                else if (type == byte[].class)
1196:                    setBytes(index, (byte[]) value);
1197:
1198:                else if (value instanceof  Date)
1199:                    setDate(index, (Date) value);
1200:                else if (value instanceof  Timestamp)
1201:                    setTimestamp(index, (Timestamp) value);
1202:                else if (value instanceof  Time)
1203:                    setTime(index, (Time) value);
1204:                else if (value instanceof  java.util.Date)
1205:                    setTimestamp(index, new Timestamp(((java.util.Date) value)
1206:                            .getTime()));
1207:
1208:                else if (type == URL.class)
1209:                    setURL(index, (URL) value);
1210:                else if (value instanceof  Blob)
1211:                    setBlob(index, (Blob) value);
1212:                else if (value instanceof  Clob)
1213:                    setClob(index, (Clob) value);
1214:                else if (value instanceof  Array)
1215:                    setArray(index, (Array) value);
1216:                else if (value instanceof  Ref)
1217:                    setRef(index, (Ref) value);
1218:                else {
1219:                    setObjectNoEx(index, value);
1220:                }
1221:            }
1222:
1223:            public void setObject(String param, Object value) {
1224:                if (value == null) {
1225:                    setNull(param, Types.NULL);
1226:                    return;
1227:                }
1228:                Class type = value.getClass();
1229:                if (type == String.class)
1230:                    setString(param, (String) value);
1231:                else if (type == Integer.class)
1232:                    setInteger(param, (Integer) value);
1233:                else if (type == MutableInteger.class)
1234:                    setInteger(param, (MutableInteger) value);
1235:                else if (type == Long.class)
1236:                    setLong(param, (Long) value);
1237:                else if (type == MutableLong.class)
1238:                    setLong(param, (MutableLong) value);
1239:                else if (type == Double.class)
1240:                    setDouble(param, (Double) value);
1241:                else if (type == MutableDouble.class)
1242:                    setDouble(param, (MutableDouble) value);
1243:                else if (type == Float.class)
1244:                    setFloat(param, (Float) value);
1245:                else if (type == MutableFloat.class)
1246:                    setFloat(param, (MutableFloat) value);
1247:
1248:                else if (type == Byte.class)
1249:                    setByte(param, (Byte) value);
1250:                else if (type == MutableByte.class)
1251:                    setByte(param, (MutableByte) value);
1252:                else if (type == Short.class)
1253:                    setShort(param, (Short) value);
1254:                else if (type == MutableShort.class)
1255:                    setShort(param, (MutableShort) value);
1256:                else if (type == Boolean.class)
1257:                    setBoolean(param, (Boolean) value);
1258:                else if (type == BigInteger.class)
1259:                    setBigInteger(param, (BigInteger) value);
1260:                else if (type == BigDecimal.class)
1261:                    setBigDecimal(param, (BigDecimal) value);
1262:                else if (type == byte[].class)
1263:                    setBytes(param, (byte[]) value);
1264:
1265:                else if (value instanceof  Date)
1266:                    setDate(param, (Date) value);
1267:                else if (value instanceof  Timestamp)
1268:                    setTimestamp(param, (Timestamp) value);
1269:                else if (value instanceof  Time)
1270:                    setTime(param, (Time) value);
1271:                else if (value instanceof  java.util.Date)
1272:                    setTimestamp(param, new Timestamp(((java.util.Date) value)
1273:                            .getTime()));
1274:
1275:                else if (type == URL.class)
1276:                    setURL(param, (URL) value);
1277:                else if (value instanceof  Blob)
1278:                    setBlob(param, (Blob) value);
1279:                else if (value instanceof  Clob)
1280:                    setClob(param, (Clob) value);
1281:                else if (value instanceof  Array)
1282:                    setArray(param, (Array) value);
1283:                else if (value instanceof  Ref)
1284:                    setRef(param, (Ref) value);
1285:                else {
1286:                    IntArrayList positions = query
1287:                            .getExistingNamedParameterPositions(param);
1288:                    for (int i = 0; i < positions.size(); i++) {
1289:                        setObjectNoEx(positions.get(i), value);
1290:                    }
1291:                }
1292:            }
1293:
1294:            // ---------------------------------------------------------------- maps
1295:
1296:            public void setParameters(Map<String, ?> parameters) {
1297:                if (parameters == null) {
1298:                    return;
1299:                }
1300:                for (String pname : parameters.keySet()) {
1301:                    setObject(pname, parameters.get(pname));
1302:                }
1303:            }
1304:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.