DBUtils { public static void setValueToPreparedStatement(int index, final PreparedStatement preparedStatement, Object value) throws SQLException { if (value instanceof Date) { preparedStatement.setLong(index, ((Date) value).getTime()); }else if(value instanceof CharSequence){ preparedStatement.setString(index, CharSequences.toString((CharSequence)value)); }else{ preparedStatement.setObject(index, value); } }  static Closeable wrapAsCloseable(final ResultSet resultSet); static boolean setAutoCommit(final Connection connection, final boolean value); static void commit(final Connection connection); static boolean rollback(final Connection connection); static void closeQuietly(java.sql.ResultSet resultSet); static void closeQuietly(Statement statement); static void closeQuietly(Connection connection); static String getDBTypeForClass(final Class<?> valueType); static void setValueToPreparedStatement(int index, final PreparedStatement preparedStatement, Object value); static int setValuesToPreparedStatement(final int startIndex, final PreparedStatement preparedStatement, final Iterable values); @SuppressWarnings("unchecked") static T getValueFromResultSet(int index, final ResultSet resultSet, final Class<T> type); static String sanitizeForTableName(String input);  }
@Test public void testSetValueToPreparedStatement() throws Exception { PreparedStatement statement = mock(PreparedStatement.class); final long now = System.currentTimeMillis(); DBUtils.setValueToPreparedStatement(1,statement, new java.util.Date(now)); verify(statement).setLong(1, now); DBUtils.setValueToPreparedStatement(2,statement, new java.sql.Date(now)); verify(statement).setLong(2, now); DBUtils.setValueToPreparedStatement(3,statement, new Time(now)); verify(statement).setLong(3, now); DBUtils.setValueToPreparedStatement(4,statement, new Timestamp(now)); verify(statement).setLong(4, now); final String str = "This is a string"; final StringBuilder stringBuilder = new StringBuilder("This is a CharSequence"); DBUtils.setValueToPreparedStatement(5,statement, str); verify(statement).setString(5, "This is a string"); DBUtils.setValueToPreparedStatement(6,statement, stringBuilder); verify(statement).setString(6, "This is a CharSequence"); DBUtils.setValueToPreparedStatement(7,statement, Boolean.FALSE); verify(statement).setObject(7, Boolean.FALSE); DBUtils.setValueToPreparedStatement(7,statement, Long.MAX_VALUE); verify(statement).setObject(7, Long.MAX_VALUE); DBUtils.setValueToPreparedStatement(8,statement, "bytes".getBytes()); verify(statement).setObject(8, "bytes".getBytes()); }