1 package sharin.util; 2 3 import java.math.BigDecimal; 4 import java.sql.Connection; 5 import java.sql.Date; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.ResultSetMetaData; 9 import java.sql.SQLException; 10 import java.sql.Statement; 11 import java.sql.Time; 12 import java.sql.Timestamp; 13 14 import javax.sql.DataSource; 15 16 public class SqlUtils { 17 18 public static Connection getConnection(DataSource ds) { 19 20 try { 21 return ds.getConnection(); 22 23 } catch (SQLException e) { 24 throw new RuntimeException(e); 25 } 26 } 27 28 public static PreparedStatement prepareStatement(Connection conn, String sql) { 29 30 try { 31 return conn.prepareStatement(sql); 32 33 } catch (SQLException e) { 34 throw new RuntimeException(e); 35 } 36 } 37 38 public static void close(Connection conn) { 39 40 try { 41 conn.close(); 42 43 } catch (SQLException e) { 44 throw new RuntimeException(e); 45 } 46 } 47 48 public static void setObject(PreparedStatement pstmt, int parameterIndex, 49 Object x) { 50 51 try { 52 pstmt.setObject(parameterIndex, x); 53 54 } catch (SQLException e) { 55 throw new RuntimeException(e); 56 } 57 } 58 59 public static ResultSet executeQuery(PreparedStatement pstmt) { 60 61 try { 62 return pstmt.executeQuery(); 63 64 } catch (SQLException e) { 65 throw new RuntimeException(e); 66 } 67 } 68 69 public static void close(Statement stmt) { 70 71 try { 72 stmt.close(); 73 74 } catch (SQLException e) { 75 throw new RuntimeException(e); 76 } 77 } 78 79 public static boolean next(ResultSet rs) { 80 81 try { 82 return rs.next(); 83 84 } catch (SQLException e) { 85 throw new RuntimeException(e); 86 } 87 } 88 89 public static void close(ResultSet rs) { 90 91 try { 92 rs.close(); 93 94 } catch (SQLException e) { 95 throw new RuntimeException(e); 96 } 97 } 98 99 public static int executeUpdate(PreparedStatement pstmt) { 100 101 try { 102 return pstmt.executeUpdate(); 103 104 } catch (SQLException e) { 105 throw new RuntimeException(e); 106 } 107 } 108 109 public static ResultSet getGeneratedKeys(Statement stmt) { 110 111 try { 112 return stmt.getGeneratedKeys(); 113 114 } catch (SQLException e) { 115 throw new RuntimeException(e); 116 } 117 } 118 119 public static ResultSetMetaData getMetaData(ResultSet rs) { 120 121 try { 122 return rs.getMetaData(); 123 124 } catch (SQLException e) { 125 throw new RuntimeException(e); 126 } 127 } 128 129 public static int getColumnCount(ResultSetMetaData metaData) { 130 131 try { 132 return metaData.getColumnCount(); 133 134 } catch (SQLException e) { 135 throw new RuntimeException(e); 136 } 137 } 138 139 public static String getColumnLabel(ResultSetMetaData metaData, int column) { 140 141 try { 142 return metaData.getColumnLabel(column); 143 144 } catch (SQLException e) { 145 throw new RuntimeException(e); 146 } 147 } 148 149 public static Object getObject(ResultSet rs, int columnIndex) { 150 151 try { 152 return rs.getObject(columnIndex); 153 154 } catch (SQLException e) { 155 throw new RuntimeException(e); 156 } 157 } 158 159 public static BigDecimal getBigDecimal(ResultSet rs, int columnIndex) { 160 161 try { 162 return rs.getBigDecimal(columnIndex); 163 164 } catch (SQLException e) { 165 throw new RuntimeException(e); 166 } 167 } 168 169 public static String getString(ResultSet rs, int columnIndex) { 170 171 try { 172 return rs.getString(columnIndex); 173 174 } catch (SQLException e) { 175 throw new RuntimeException(e); 176 } 177 } 178 179 public static boolean getBoolean(ResultSet rs, int columnIndex) { 180 181 try { 182 return rs.getBoolean(columnIndex); 183 184 } catch (SQLException e) { 185 throw new RuntimeException(e); 186 } 187 } 188 189 public static byte getByte(ResultSet rs, int columnIndex) { 190 191 try { 192 return rs.getByte(columnIndex); 193 194 } catch (SQLException e) { 195 throw new RuntimeException(e); 196 } 197 } 198 199 public static byte[] getBytes(ResultSet rs, int columnIndex) { 200 201 try { 202 return rs.getBytes(columnIndex); 203 204 } catch (SQLException e) { 205 throw new RuntimeException(e); 206 } 207 } 208 209 public static Date getDate(ResultSet rs, int columnIndex) { 210 211 try { 212 return rs.getDate(columnIndex); 213 214 } catch (SQLException e) { 215 throw new RuntimeException(e); 216 } 217 } 218 219 public static double getDouble(ResultSet rs, int columnIndex) { 220 221 try { 222 return rs.getDouble(columnIndex); 223 224 } catch (SQLException e) { 225 throw new RuntimeException(e); 226 } 227 } 228 229 public static float getFloat(ResultSet rs, int columnIndex) { 230 231 try { 232 return rs.getFloat(columnIndex); 233 234 } catch (SQLException e) { 235 throw new RuntimeException(e); 236 } 237 } 238 239 public static int getInt(ResultSet rs, int columnIndex) { 240 241 try { 242 return rs.getInt(columnIndex); 243 244 } catch (SQLException e) { 245 throw new RuntimeException(e); 246 } 247 } 248 249 public static long getLong(ResultSet rs, int columnIndex) { 250 251 try { 252 return rs.getLong(columnIndex); 253 254 } catch (SQLException e) { 255 throw new RuntimeException(e); 256 } 257 } 258 259 public static short getShort(ResultSet rs, int columnIndex) { 260 261 try { 262 return rs.getShort(columnIndex); 263 264 } catch (SQLException e) { 265 throw new RuntimeException(e); 266 } 267 } 268 269 public static Time getTime(ResultSet rs, int columnIndex) { 270 271 try { 272 return rs.getTime(columnIndex); 273 274 } catch (SQLException e) { 275 throw new RuntimeException(e); 276 } 277 } 278 279 public static Timestamp getTimestamp(ResultSet rs, int columnIndex) { 280 281 try { 282 return rs.getTimestamp(columnIndex); 283 284 } catch (SQLException e) { 285 throw new RuntimeException(e); 286 } 287 } 288 289 public static boolean wasNull(ResultSet rs) { 290 291 try { 292 return rs.wasNull(); 293 294 } catch (SQLException e) { 295 throw new RuntimeException(e); 296 } 297 } 298 }