KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > jdbc > JDBCUtil


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.jdbc;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.Statement JavaDoc;
26 import java.sql.Timestamp JavaDoc;
27 import java.util.Calendar JavaDoc;
28
29 /**
30  * Utilities useful when working with JDBC databases.
31  *
32  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
33  */

34 public class JDBCUtil {
35     /**
36      * Validates if the supplied value is null from the result set. This is
37      * useful for primitive type checking.
38      *
39      * @param resultSet
40      * @param columnName
41      * @return true if the column was null.
42      * @throws SQLException
43      */

44     public static boolean isNull(ResultSet JavaDoc resultSet, String JavaDoc columnName) throws SQLException JavaDoc {
45         return null == resultSet.getObject(columnName);
46     }
47
48     /**
49      * Transforms a given column from the java.sql.ResultSet from a
50      * java.sql.Timestamp to a java.util.Calendar.
51      *
52      * @param resultSet
53      * @param columnName
54      * @return java.util.Calendar
55      * @throws SQLException
56      */

57     public static Calendar JavaDoc getCalendar(ResultSet JavaDoc resultSet, String JavaDoc columnName) throws SQLException JavaDoc {
58         return getCalendar(resultSet.getTimestamp(columnName));
59     }
60
61     /**
62      * Transforms the supplied java.sql.Timestamp into a java.util.Calendar.
63      *
64      * @param timestamp
65      * @return java.util.Calendar
66      */

67     public static Calendar JavaDoc getCalendar(Timestamp JavaDoc timestamp) {
68         Calendar JavaDoc calendar = Calendar.getInstance();
69         calendar.setTimeInMillis(timestamp == null ? System.currentTimeMillis() : timestamp.getTime());
70         return calendar;
71     }
72     
73     /**
74      * Closes the supplied Connection handling any exceptions that may be thrown.
75      * @param connection
76      */

77     public static void cleanup(Connection JavaDoc connection) {
78         if (connection != null) {
79             try {
80                 connection.close();
81             } catch (SQLException JavaDoc e) {
82                 // ignore
83
}
84         }
85     }
86     
87     /**
88      * Closes the supplied Statement handling any exceptions that may be thrown.
89      * @param statement
90      */

91     public static void cleanup(Statement JavaDoc statement) {
92         if(statement != null) {
93             try {
94                 statement.close();
95             } catch (SQLException JavaDoc e) {
96                 // ignore
97
}
98         }
99     }
100     
101     /**
102      * Closes the supplied JDBCPreparedStatement handling any exceptions that may be thrown.
103      * @param statement
104      */

105     public static void cleanup(JDBCPreparedStatement statement) {
106         if(statement != null) {
107             try {
108                 statement.releasePreparedStatement();
109             } catch (SQLException JavaDoc e) {
110                 // ignore
111
}
112         }
113     }
114
115     /**
116      * Closes the supplied ResultSet handling any exceptions that may be thrown.
117      * @param resultSet
118      */

119     public static void cleanup(ResultSet JavaDoc resultSet) {
120         if (resultSet != null) {
121             try {
122                 resultSet.close();
123             } catch (SQLException JavaDoc e) {
124                 // ignored
125
}
126         }
127     }
128 }
Popular Tags