Source Code Cross Referenced for DbUtils.java in  » Database-JDBC-Connection-Pool » Database-Utilities » org » apache » commons » dbutils » 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 JDBC Connection Pool » Database Utilities » org.apache.commons.dbutils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.dbutils;
018:
019:        import java.io.PrintWriter;
020:        import java.sql.Connection;
021:        import java.sql.ResultSet;
022:        import java.sql.SQLException;
023:        import java.sql.Statement;
024:
025:        /**
026:         * A collection of JDBC helper methods.  This class is thread safe.
027:         */
028:        public final class DbUtils {
029:
030:            /**
031:             * Close a <code>Connection</code>, avoid closing if null.
032:             *
033:             * @param conn Connection to close.
034:             * @throws SQLException if a database access error occurs
035:             */
036:            public static void close(Connection conn) throws SQLException {
037:                if (conn != null) {
038:                    conn.close();
039:                }
040:            }
041:
042:            /**
043:             * Close a <code>ResultSet</code>, avoid closing if null.
044:             *
045:             * @param rs ResultSet to close.
046:             * @throws SQLException if a database access error occurs
047:             */
048:            public static void close(ResultSet rs) throws SQLException {
049:                if (rs != null) {
050:                    rs.close();
051:                }
052:            }
053:
054:            /**
055:             * Close a <code>Statement</code>, avoid closing if null.
056:             *
057:             * @param stmt Statement to close.
058:             * @throws SQLException if a database access error occurs
059:             */
060:            public static void close(Statement stmt) throws SQLException {
061:                if (stmt != null) {
062:                    stmt.close();
063:                }
064:            }
065:
066:            /**
067:             * Close a <code>Connection</code>, avoid closing if null and hide
068:             * any SQLExceptions that occur.
069:             *
070:             * @param conn Connection to close.
071:             */
072:            public static void closeQuietly(Connection conn) {
073:                try {
074:                    close(conn);
075:                } catch (SQLException e) {
076:                    // quiet
077:                }
078:            }
079:
080:            /**
081:             * Close a <code>Connection</code>, <code>Statement</code> and 
082:             * <code>ResultSet</code>.  Avoid closing if null and hide any 
083:             * SQLExceptions that occur.
084:             *
085:             * @param conn Connection to close.
086:             * @param stmt Statement to close.
087:             * @param rs ResultSet to close.
088:             */
089:            public static void closeQuietly(Connection conn, Statement stmt,
090:                    ResultSet rs) {
091:
092:                try {
093:                    closeQuietly(rs);
094:                } finally {
095:                    try {
096:                        closeQuietly(stmt);
097:                    } finally {
098:                        closeQuietly(conn);
099:                    }
100:                }
101:
102:            }
103:
104:            /**
105:             * Close a <code>ResultSet</code>, avoid closing if null and hide any
106:             * SQLExceptions that occur.
107:             *
108:             * @param rs ResultSet to close.
109:             */
110:            public static void closeQuietly(ResultSet rs) {
111:                try {
112:                    close(rs);
113:                } catch (SQLException e) {
114:                    // quiet
115:                }
116:            }
117:
118:            /**
119:             * Close a <code>Statement</code>, avoid closing if null and hide
120:             * any SQLExceptions that occur.
121:             *
122:             * @param stmt Statement to close.
123:             */
124:            public static void closeQuietly(Statement stmt) {
125:                try {
126:                    close(stmt);
127:                } catch (SQLException e) {
128:                    // quiet
129:                }
130:            }
131:
132:            /**
133:             * Commits a <code>Connection</code> then closes it, avoid closing if null.
134:             *
135:             * @param conn Connection to close.
136:             * @throws SQLException if a database access error occurs
137:             */
138:            public static void commitAndClose(Connection conn)
139:                    throws SQLException {
140:                if (conn != null) {
141:                    try {
142:                        conn.commit();
143:                    } finally {
144:                        conn.close();
145:                    }
146:                }
147:            }
148:
149:            /**
150:             * Commits a <code>Connection</code> then closes it, avoid closing if null 
151:             * and hide any SQLExceptions that occur.
152:             *
153:             * @param conn Connection to close.
154:             */
155:            public static void commitAndCloseQuietly(Connection conn) {
156:                try {
157:                    commitAndClose(conn);
158:                } catch (SQLException e) {
159:                    // quiet
160:                }
161:            }
162:
163:            /**
164:             * Loads and registers a database driver class.
165:             * If this succeeds, it returns true, else it returns false.
166:             *
167:             * @param driverClassName of driver to load
168:             * @return boolean <code>true</code> if the driver was found, otherwise <code>false</code>
169:             */
170:            public static boolean loadDriver(String driverClassName) {
171:                try {
172:                    Class.forName(driverClassName).newInstance();
173:                    return true;
174:
175:                } catch (ClassNotFoundException e) {
176:                    return false;
177:
178:                } catch (IllegalAccessException e) {
179:                    // Constructor is private, OK for DriverManager contract
180:                    return true;
181:
182:                } catch (InstantiationException e) {
183:                    return false;
184:
185:                } catch (Throwable e) {
186:                    return false;
187:                }
188:            }
189:
190:            /**
191:             * Print the stack trace for a SQLException to STDERR.
192:             *
193:             * @param e SQLException to print stack trace of
194:             */
195:            public static void printStackTrace(SQLException e) {
196:                printStackTrace(e, new PrintWriter(System.err));
197:            }
198:
199:            /**
200:             * Print the stack trace for a SQLException to a 
201:             * specified PrintWriter. 
202:             *
203:             * @param e SQLException to print stack trace of
204:             * @param pw PrintWriter to print to
205:             */
206:            public static void printStackTrace(SQLException e, PrintWriter pw) {
207:
208:                SQLException next = e;
209:                while (next != null) {
210:                    next.printStackTrace(pw);
211:                    next = next.getNextException();
212:                    if (next != null) {
213:                        pw.println("Next SQLException:");
214:                    }
215:                }
216:            }
217:
218:            /**
219:             * Print warnings on a Connection to STDERR.
220:             *
221:             * @param conn Connection to print warnings from
222:             */
223:            public static void printWarnings(Connection conn) {
224:                printWarnings(conn, new PrintWriter(System.err));
225:            }
226:
227:            /**
228:             * Print warnings on a Connection to a specified PrintWriter. 
229:             *
230:             * @param conn Connection to print warnings from
231:             * @param pw PrintWriter to print to
232:             */
233:            public static void printWarnings(Connection conn, PrintWriter pw) {
234:                if (conn != null) {
235:                    try {
236:                        printStackTrace(conn.getWarnings(), pw);
237:                    } catch (SQLException e) {
238:                        printStackTrace(e, pw);
239:                    }
240:                }
241:            }
242:
243:            /**
244:             * Rollback any changes made on the given connection.
245:             * @param conn Connection to rollback.  A null value is legal.
246:             * @throws SQLException if a database access error occurs
247:             */
248:            public static void rollback(Connection conn) throws SQLException {
249:                if (conn != null) {
250:                    conn.rollback();
251:                }
252:            }
253:
254:            /**
255:             * Performs a rollback on the <code>Connection</code> then closes it, 
256:             * avoid closing if null.
257:             *
258:             * @param conn Connection to rollback.  A null value is legal.
259:             * @throws SQLException if a database access error occurs
260:             * @since DbUtils 1.1
261:             */
262:            public static void rollbackAndClose(Connection conn)
263:                    throws SQLException {
264:                if (conn != null) {
265:                    try {
266:                        conn.rollback();
267:                    } finally {
268:                        conn.close();
269:                    }
270:                }
271:            }
272:
273:            /**
274:             * Performs a rollback on the <code>Connection</code> then closes it, 
275:             * avoid closing if null and hide any SQLExceptions that occur.
276:             *
277:             * @param conn Connection to rollback.  A null value is legal.
278:             * @since DbUtils 1.1
279:             */
280:            public static void rollbackAndCloseQuietly(Connection conn) {
281:                try {
282:                    rollbackAndClose(conn);
283:                } catch (SQLException e) {
284:                    // quiet
285:                }
286:            }
287:
288:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.