Posts in the “java” category

Java Date format example - Format a Date so humans can read it

Java Date format FAQ: Can you show me how to format a Java Date object in a human-readable format?

It's pretty easy to get a Date in a Java program, but it takes a little more work to print a Date in a human-readable format. This article shows how to print a date in a human-readable format.

Java Date format example

Here's a snippet of Java date formatting code I'm working on right now:

Debug hibernate - how to see the SQL being run by Hibernate

Java Hibernate debug faq: How can I see the SQL that is actually being run/executed when I'm using Hibernate?

To debug Hibernate output, just configure this parameter in your hibernate.properties file:

hibernate.show_sql=true

This Hibernate configuration parameter will cause Hibernate to show the actual SQL that is being generated/used when your queries are run. Of course it can be a little verbose, but hopefully it will also give you all the SQL you need to troubleshoot your problem.

Generate Java code from your database design

I don't write much in the way of business application code any more, but if I did, I would generate Java code like crazy.

Think about it, whenever you write database-driven applications, one common denominator is that every database driven project has the database design completed before you start coding. And then, once you start coding, I'll bet 80% of the code is related to what I call CRUD ("create, read, update, delete") functionality.

And you know what -- you can generate this Java database code, either statically, or more powerfully, dynamically.

java.lang.UnsupportedClassVersionError (UnsupportedClassVersionError)

You know what will really screw with your mind? When there is a 1.4.2 version of a java.exe file in the C:\Windows\System32 directory of your Windows XP system, and you're trying to compile and run a Java 1.5 program from the command line. I kept getting this error message and couldn't figure it out, even though I knew what it meant(!):

DOS batch files to compile and run a Java program (and create a jar file)

This isn't the most high-tech way to do things, but I thought I'd share these Windows (DOS) shell scripts that I'm currently using to compile a Java application, create a Jar file to distribute the application, and finally run the application. I ended up creating these scripts because of configuration problems on my Windows PC, but I thought they might be useful samples for others.

Java profiling with JRat

Writing software is a funny thing. You do something one day and you think it's really cool, then come back another day and go "WTF was I thinking?", so you tear it apart and rewrite it.

Today I ran the JRat Java profiler (Runtime Analysis Toolkit) on some code I wrote about 10 days ago, and it showed that I was looking for a pixel 10,000,000+ times on a 1024x768 resolution image.

Debug Ant problems with diagnostics

I just had a problem using Ant on Mac OS X 10.4.x. As I was trying to get the jarbundler task to run, the jarbundler help text said to copy the jarbundler.jar file to the /Developer/Java/Ant/lib directory. Turns out this isn't right for my system, but I didn't know where Ant was getting its libraries from.

My exact error was:

taskdef class net.sourceforge.jarbundler.JarBundler cannot be found

Fortunately the following Ant command gave me all the output I needed, and much more:

A Java String to int conversion example

Here's a little Java code sample that shows how to try to perform a Java String to int conversion. In this example, if the conversion works, the variable pageNum will hold the int version of the pageNumString. If the conversion doesn't work a NumberFormatException will be thrown, and you'll need to deal with that.

Java PreparedStatement - a SQL UPDATE example

Java SQL FAQ: Can you provide a Java PreparedStatement example that shows how to use a SQL UPDATE?

Sure. I have quite a few examples on this website, just see the "Related" section for those. But for now, here's a short sample method that performs a JDBC SQL UPDATE using a Java PreparedStatement:

Product review: IntelliJ IDEA from JetBrains

Develop with pleasure. That's the IntelliJ IDEA slogan, and at least through Version 5 I think they nailed it on the head.

So this product review is simple: If you're a Java developer go get an eval copy of IntelliJ IDEA right now and give it an honest eval. You won't be sorry.

A Java LinkedList example

I find that I learn a lot — especially initially — when I can see source code examples. To that end, here’s some sample code showing how to use a Java LinkedList. This uses Java syntax prior to Java 5:

Java if else example

Question: Can you show me an example of the Java if/then syntax?

Answer: Sure, here you go:

Java while loop example

Java while loop FAQ: Can you show me an example of a Java while loop?

Sure, here's a Java while loop example:

package com.devdaily.javasamples;

public class WhileTest
{
  public static void main(String[] args)
  {
    int i = 0;
    while (i < 5)
    {
      System.out.println("i = " + i);
      i++;
    }
  }
}

This example prints out the value of the variable i until i gets to 5, at which point the loop stops.

Java for loop example syntax

Java for loop FAQ: Can you show me an example of a Java for loop?

Answer: Sure. Here's an example of a Java for loop using the original Java syntax:

What is a Java IOException?

Java Exception FAQ: What is a Java IOException?

An IOException can occur in a variety of ways when you try to access the local filesystem. In the following Java code segment an IOException can be thrown by the br.readLine() method, so a try/catch wrapper is used around that portion of code to trap (and arguably deal with) the potential problem:

Java “static” - What does it mean when a method or field is "static"?

In short, static Java variables and methods are instantiated only once for a class. Because of this you’ll hear these referred to as class variables, as opposed to the more common instance variables. That’s because you can deal with these directly at the class level, without needing to have an instance of the class instantiated.