Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DatabaseUtils |
|
| 2.6;2.6 |
1 | package org.jtheque.utils; | |
2 | ||
3 | import org.slf4j.LoggerFactory; | |
4 | ||
5 | import java.sql.Connection; | |
6 | import java.sql.ResultSet; | |
7 | import java.sql.SQLException; | |
8 | import java.sql.Statement; | |
9 | import java.util.ArrayList; | |
10 | import java.util.Collection; | |
11 | ||
12 | /* | |
13 | * This file is part of JTheque. | |
14 | * | |
15 | * JTheque is free software: you can redistribute it and/or modify | |
16 | * it under the terms of the GNU General Public License as published by | |
17 | * the Free Software Foundation, either version 3 of the License. | |
18 | * | |
19 | * JTheque is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | * | |
24 | * You should have received a copy of the GNU General Public License | |
25 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
26 | */ | |
27 | ||
28 | /** | |
29 | * An utility class for database use. | |
30 | * | |
31 | * @author Baptiste Wicht | |
32 | */ | |
33 | public final class DatabaseUtils { | |
34 | /** | |
35 | * Construct a new DatabaseUtils. This class is an utility class, it cannot be instantiated. | |
36 | */ | |
37 | private DatabaseUtils() { | |
38 | 0 | super(); |
39 | 0 | } |
40 | ||
41 | /** | |
42 | * Close the result set. | |
43 | * | |
44 | * @param rs The result set to close. | |
45 | */ | |
46 | public static void close(ResultSet rs) { | |
47 | 0 | if (rs != null) { |
48 | try { | |
49 | 0 | rs.close(); |
50 | 0 | } catch (SQLException e) { |
51 | 0 | LoggerFactory.getLogger(DatabaseUtils.class).error("Unable to close resultset", e); |
52 | 0 | } |
53 | } | |
54 | 0 | } |
55 | ||
56 | /** | |
57 | * Close the statement. | |
58 | * | |
59 | * @param statement The statement to close. | |
60 | */ | |
61 | public static void close(Statement statement) { | |
62 | 0 | if (statement != null) { |
63 | try { | |
64 | 0 | statement.close(); |
65 | 0 | } catch (SQLException e) { |
66 | 0 | LoggerFactory.getLogger(DatabaseUtils.class).error("Unable to close statement", e); |
67 | 0 | } |
68 | } | |
69 | 0 | } |
70 | ||
71 | /** | |
72 | * Close the connection. | |
73 | * | |
74 | * @param connection The connection to close. | |
75 | */ | |
76 | public static void close(Connection connection) { | |
77 | 0 | if (connection != null) { |
78 | try { | |
79 | 0 | connection.close(); |
80 | 0 | } catch (SQLException e) { |
81 | 0 | LoggerFactory.getLogger(DatabaseUtils.class).error("Unable to close connection", e); |
82 | 0 | } |
83 | } | |
84 | 0 | } |
85 | ||
86 | /** | |
87 | * Return all the results of the specified column. | |
88 | * | |
89 | * @param rs The result set. | |
90 | * @param column The column to get the data from. | |
91 | * @return A collection of Integer containing all the results of the specified column. | |
92 | */ | |
93 | public static Collection<Integer> getAllIntResults(ResultSet rs, String column) { | |
94 | 0 | Collection<Integer> results = new ArrayList<Integer>(25); |
95 | ||
96 | try { | |
97 | 0 | while (rs.next()) { |
98 | 0 | results.add(rs.getInt(column)); |
99 | } | |
100 | 0 | } catch (SQLException e) { |
101 | 0 | LoggerFactory.getLogger(DatabaseUtils.class).error("Unable to read result set", e); |
102 | 0 | } |
103 | ||
104 | 0 | return results; |
105 | } | |
106 | } |