What is the best way to save enums into a database? I know Java provides name() and valueOf() methods to convert enum values into a String and back. But are there any other (flexible) options to store these values? Is there a smart way to make enums into unique numbers (ordinal() is not safe to use)? Update Thanks for all
How to sanity check a date in Java
I find it curious that the most obvious way to create Date objects in Java has been deprecated and appears to have been “substituted” with a not so obvious to use lenient calendar. How do you check that a date, given as a combination of day, month, and year, is a valid date? For instance, 2008-02-31 (as in yyyy-mm-dd) would
How to split a string with any whitespace chars as delimiters
What regex pattern would need I to pass to java.lang.String.split() to split a String into an Array of substrings using all whitespace characters (‘ ‘, ‘t’, ‘n’, etc.) as delimiters? Answer Something in the lines of This groups all white spaces as a delimiter. So if I have the string: This should yield the strings “Hello” and “World” and omit
Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
We all know you can’t do the following because of ConcurrentModificationException: But this apparently works sometimes, but not always. Here’s some specific code: This, of course, results in: Even though multiple threads aren’t doing it. Anyway. What’s the best solution to this problem? How can I remove an item from the collection in a loop without throwing this exception? I’m
Build Eclipse Java Project from Command Line
Is there a way to compile an Eclipse-based Java project from the command line? I’m trying to automate my build (using FinalBuilder not ant), and I’m neither a Java nor Eclipse expert. I can probably figure out how to do this with straight java command line options, but then the Eclipse project feels like a lot of wasted effort. In
What is the proper way to store app’s conf data in Java?
Where do you store user-specific and machine-specific runtime configuration data for J2SE application? (For example, C:UsersUSERNAMEAppDataRoaming</em> on Windows and /home/username on Unix) How do you get these locations in the filesystem in platform-independent way? Answer That depends on your kind of J2SE Application: J2SE executable JAR file (very simple): use user.home System property to find home-dir. Then make a subdir
ResultSet: Retrieving column values by index versus retrieving by label
When using JDBC, I often come across constructs like I asked myself (and authors of code too) why not to use labels for retrieving column values: The best explanation I’ve heard is something concerning performance. But actually, does it make processing extremely fast? I don’t believe so, though I have never performed measurements. Even if retrieving by label would be
Dynamically find the class that represents a primitive Java type
I need to make some reflective method calls in Java. Those calls will include methods that have arguments that are primitive types (int, double, etc.). The way to specify such types when looking up the method reflectively is int.class, double.class, etc. The challenge is that I am accepting input from an outside source that will specify the types dynamically. Therefore,
The encoding ‘UTF-8’ is not supported by the Java runtime
Whenever I start our Apache Felix (OSGi) based application under SUN Java ( build 1.6.0_10-rc2-b32 and other 1.6.x builds) I see the following message output on the console (usually under Ubuntu 8.4): Warning: The encoding ‘UTF-8’ is not supported by the Java runtime. I’ve seen this message display occasionally when running both Tomcat and Resin as well. If java supports
How to upgrade JRE
Is there anyway to upgrade the installed JRE in the system? We are having 1.5.0_08 installed in out HP Unix system.We have to upgrade this to 1.5.0_15.Is there a way to patch up the existing JRE and upgrade to a newer version.Or can this only be achieved by installing the newer JRE and set this in the system PATH. Answer