1 package com.diasparsoftware.jdbc;
2
3 import java.sql.*;
4 import java.util.*;
5 import java.util.Date;
6
7 import org.easymock.MockControl;
8
9 import com.diasparsoftware.java.util.*;
10
11 public class JdbcUtil {
12
13 /***
14 * Provide a human-readable view of a JDBC result set.
15 *
16 * Here is sample output, showing two rows and the column names.
17 *
18 * <pre>
19 * [[EMPLOYEE_NUMBER, NAME, PHONE], [019, Joe, 416 555-1212], [092, Sarah, 416 555-1212]] * </pre>
20 *
21 * @param resultSet
22 * @return A string representation that looks like a <code>List</code>
23 * of <code>List</code>s.
24 * @throws SQLException
25 */
26 public static List resultSetAsTable(ResultSet resultSet)
27 throws SQLException {
28
29 List rows = new LinkedList();
30
31 ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
32 int columnCount = resultSetMetaData.getColumnCount();
33
34 List columnNames = new LinkedList();
35 for (int i = 1; i <= columnCount; i++) {
36 String columnName = resultSetMetaData.getColumnName(i);
37 columnNames.add(columnName);
38 }
39
40 rows.add(columnNames);
41 int rowCount = 1;
42
43 while (resultSet.next()) {
44 List rowData = new LinkedList();
45 for (int i = 1; i <= columnCount; i++) {
46 Object columnData = resultSet.getObject(i);
47 rowData.add(columnData);
48 }
49
50 rows.add(rowData);
51 rowCount++;
52 }
53
54 return rows;
55 }
56
57 /***
58 * Creates a <code>Timestamp</code> object from the specified
59 * year, month, day, but at 12 noon local time.
60 *
61 * @param year
62 * @param month
63 * @param day
64 * @return
65 */
66 public static Timestamp makeTimestamp(int year, int month, int day) {
67
68
69
70 return new Timestamp(DateUtil.makeDate(year, month, day).getTime());
71 }
72
73 public static Timestamp makeTimestamp(Date date) {
74 return new Timestamp(date.getTime());
75 }
76
77 /***
78 * A way to make timestamps for normal people — none of this
79 * "year minus 1900" and "month minus 1" nonsense.
80 *
81 * @param year
82 * @param month
83 * @param day
84 * @param hour
85 * @param minute
86 * @param second
87 * @param millisecond
88 * @return
89 */
90 public static Timestamp makeTimestamp(int year, int month, int day,
91 int hour, int minute, int second, int millisecond) {
92
93 Calendar calendar = new GregorianCalendar();
94 calendar.set(Calendar.YEAR, year);
95 calendar.set(Calendar.MONTH, month - 1);
96 calendar.set(Calendar.DATE, day);
97 calendar.set(Calendar.HOUR_OF_DAY, hour);
98 calendar.set(Calendar.MINUTE, minute);
99 calendar.set(Calendar.SECOND, second);
100 calendar.set(Calendar.MILLISECOND, millisecond);
101
102 return new Timestamp(calendar.getTimeInMillis());
103 }
104
105 public static Date toJavaUtilDate(Timestamp timestamp) {
106 return new Date(timestamp.getTime());
107 }
108
109 public static void setPreparedStatementParameters(
110 PreparedStatement preparedStatement, List parameters) throws SQLException {
111
112 int index = 1;
113 for (Iterator i = parameters.iterator(); i.hasNext(); ) {
114 Object each = (Object) i.next();
115 preparedStatement.setObject(index, each);
116 index++;
117 }
118 }
119
120 public static ResultSet createFakeResultSet(Map rowData,
121 final MockControl resultSetControl) {
122
123 final ResultSet resultSet = (ResultSet) resultSetControl.getMock();
124
125 CollectionUtil.forEachDo(rowData, new MapEntryClosure() {
126 public void eachMapEntry(Object key, Object value) {
127 try {
128 resultSet.getObject((String) key);
129 resultSetControl.setReturnValue(value,
130 MockControl.ONE_OR_MORE);
131 }
132 catch (SQLException e) {
133 throw new RuntimeException(
134 "Unable to create fake ResultSet", e);
135 }
136 }
137 });
138
139 return resultSet;
140 }
141
142 public static java.sql.Date makeDate(int year, int month, int day) {
143 return new java.sql.Date(DateUtil.makeDate(year, month, day).getTime());
144 }
145 }