Coding category archives

Java stack trace with line numbers

Often in Java program stack trace, you will see some stack frames include a source line number, while some only say “compiled code”. This is because the stack frames without line numbers have been compiled by the JIT compiler, and the JIT compiler obliterates line number information when it compiles code.

Spy on a Java program

While a Java program is running in a console window, you can can get a stack dump of the program at any time, while it is running. This is very useful for debugging, especially for heavily multithreaded programs.

Java versus Daylight savings time

Depending on the version of the Java runtime and the location of the host, the Java Date and Calendar routines may take daylight savings time into account. For example:

Don’t tolerate compiler warnings

All code must compile without warnings. All warnings must be fixed before checking code in to a version control system. This applies not only to compiled languages: for example, validation of HTML or CSS code should also be warning-free for the same reasons given here.

Oracle testing with the DUAL table

This feature is a hack intended for testing, but it’s built in to every Oracle installation.
DUAL is a table automatically created by Oracle and accessible to all users. It has one column, DUMMY, containing one row. It’s useful for selecting a constant or expression, because the table always exists (so the query will succeed) and [...]

java.sql.Date is not a real date

java.sql.Date stores only date information, not times. Simply converting a java.util.Date into a java.sql.Date will silently set the time to midnight. So, to store date/times to be manipulated as java.util.Date objects, don’t do this:

// BUG: loses time of day
preparedStatement.setDate(1, new java.sql.Date(date.getTime()));

do this instead:

preparedStatement.setTimestamp(1, new java.sql.Timestamp(date.getTime()));

java.sql.Timestamp extends java.util.Date, but it should not be used as a [...]

Data access layer should be separate

In a multi-layered architecture, the data access layer should hide database foibles. For example, Oracle stores empty strings as null – this should be hidden in the data access layer by specifying function return values appropriately. For example, ensure that the layer never returns a null string: null strings returned from the database should be [...]

Oracle silently truncates dates

According to the documentation, the Oracle DATE type does not store fractions of a second. So if you store a date/time value including fractions of a second and read it back, you’ll get back a slightly different date/time. This will cause exact date matching code in your appplication to fail.

Oracle empty string = null

In Oracle 8, there is no such thing as a zero-length string. Any zero-length string, either from a function call or the literal ”, is treated as null.

Don’t use automatically generated unique IDs

Think carefully before using an automatically-generated unique ID as a primary key in a SQL table. Using such automatically-generated IDs introduces extra implementation detail. This is a pain to manage, especially if they are used as primary keys or worse, foreign keys in tables.

Close
E-mail It