Blog of Wonders

java

Ran into another bit of trouble today that cost me another sizable chunk of time. When using Spring’s NamedParameterJdbcTemplate to insert into a DATETIME column, the following doesn’t work:

1

2

3

4

5

6

7

8

9

10

publicclassExampleDao{

protectedNamedParameterJdbcTemplate jdbcTemplate;

publicvoidinsert(ExampleModel model){

jdbcTemplate.update(

"INSERT INTO `sometable` (`some_datetime_column`) VALUES(:datetime)",

newMapSqlParameterSource().addValue("datetime",newDate(),Types.DATE)

);

}

}

Your date will be happily insert, but you’ll be sad to know that the time component has been truncated to something like “2012-08-28 00:00:00.” Using Types.TIMESTAMP has the same effect.

Using Types.TIMESTAMP is actually correct, but the secret is that you have to wrap your java.util.Date in a java.sql.Date.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

publicclassExampleDao{

protectedNamedParameterJdbcTemplate jdbcTemplate;

publicvoidinsert(ExampleModel model){

jdbcTemplate.update(

"INSERT INTO `sometable` (`some_datetime_column`) VALUES(:datetime)",

newMapSqlParameterSource().addValue(

"datetime",

newjava.sql.Date(newDate().getTime()),

Types.TIMESTAMP

)

);

}

}

If you’re reaction is one of disappointment, then you’re feeling the same way I did.

For simple JdbcTemplates, there is sufficient behind the scenes magic that you don’t run into this kind of problem. The following works without any translations or other such boilerplate (making the above that much more … dissatisfying).

Configure JBoss

First, to enable JBoss to allow remote debugging, edit your <jboss install location>/bin/standalone.conf. Now, look for # JAVA_OPTS="$JAVA_OPTS -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n" and uncomment it. It should be near the bottom.

If you are on Windows, you’ll need to edit standalone.conf.bat instead and uncomment rem set "JAVA_OPTS=%JAVA_OPTS% -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"

Start Jboss. You should now see something like this at the top of the output

Configure Eclipse

Configuring Eclipse is easy. First start by creating a new debug configuration by going to Run > Debug Configurations… In the left side panel, select Remote Java Application, then in the toolbar up top, click on the new icon. This will give you a new debug configuration. On the right side, name it however you like. Then, change the port to 8787 and the host to where ever your JBoss installation is running. I’m running mine on the same machine that I am debugging on, so I leave it as localhost. Hit the Debug button.

Lastly, you’ll need to switch to the debug view to see all of your tools. To do so, go to Window > Show View > Other…, type in “debug” in the filter box and select the debug view. You should now see a JVM with a list of a bunch threads. If you don’t see any threads, make sure that JBoss is running and that you followed the steps above correctly.

If all went well, you can now set break points in your code and they should show up automatically in the debugger.