Alvin Alexander | Java, Scala, Unix, Perl, Mac OS X

Java Swing FAQ: How do I set my Java/Swing (GUI) application to use the default look and feel of the current system/platform?

In a Java / Swing application, to use the default look and feel of the current operating system (platform), first import the necessary class:

import javax.swing.UIManager;

Then use this code:


Java Swing FAQ: How do I set my Java look and feel to the "metal" look and feel?

To set your Java / Swing application to use the metal look and feel, first import the necessary classes:

import javax.swing.UIManager;
import javax.swing.plaf.metal.MetalLookAndFeel;

Then put this in your code:

UIManager.setLookAndFeel( new MetalLookAndFeel() );

Question: I don't like the default JBuilder keyboard key mappings -- can I change them?

When using JBuilder, you can set bookmarks in your open files to easily move back-and-forth between these files. For example, suppose you have five files open at one time, but for a little while you just want to move back and forth between and Bookmarks let you do this. You set one bookmark in, and another bookmark in

Because you set bookmarks at specific line numbers, you also go right back to the same line each time you move back and forth.

Okay, so how do I set a bookmark?

When you have more than one file open at a time with JBuilder, use the keystroke [Ctrl][F6] to move between your open files.

They have many different pages, but here is a high-level URL for Sun's online Java documentation.

Java PreparedStatement FAQ: Can you share an example of a Java PreparedStatement that executes a SQL INSERT query?

Yes ... I just realized I don't have a Java PreparedStatement INSERT example out here, so ... (searching, searching) ... here you go. Here's the source code for a Java/JDBC PreparedStatement "INSERT" query example:

Java/JDBC connection FAQ: How do I connect to a database using Java and JDBC?

Let's take a look at a JDBC database connection example. In this example I'll connect to a Postgresql database, but as you'll see from the code and other examples on this web site, the steps are always very similar.

A Java/JDBC Postgresql database connection example

To get a JDBC connection to a PostgreSQL database do this:

Java Date FAQ: Can you show me how to get today's date in Java?

Getting a Java Date object that represents "today" or "now" is fairly simple. The following sample code shows how to do this:

Date date = Calendar.getInstance().getTime();

If you haven't worked with the Java Date class before, I should point out that I'm referring to a java.util.Date class here, and not a java.sql.Date class. To make this a little more clear, I could write that same code like this:

Postgresql Backup FAQ: How do I create a backup of a Postgresql database?

Solution: I create a Postgresql backup (a Postgresql dump) for this website using the pg_dump command, like this:

pg_dump -h db_server -U db_user -W db_name > db.20070725.dump

With this Postgres / pg_dump command I'm doing the following:

Here's a quick "SQL select where date" example, showing how to select all orders from an example orders table where the order_date is greater than a date we specify.

The SQL "select where date" example

With these assumptions:

  • You have a database table named orders
  • This table has two fields named order_id and order_date

This SQL will give you information about orders placed in the last two days with PostreSQL:

Here is a JUnit template I use with JBuilder:

To indent a block of code using JBuilder, just do this:

To compile a Java program with JBuilder, you must create a Project first. Select File | New Project..., then follow the instructions.

Java String to float FAQ: How do I convert a Java String to a Java float?

Answer: Here's an example that demonstrates how to convert a Java String to a float value. This example code segment converts the String "100.00" to a Java float:

Java compile FAQ: How do I compile my first Java program?

To compile a Java program you first need to download a Java development kit (see if you don't already have one. Next, assuming you have a program named that you want to compile, you compile it like this with the javac:


Assuming that there are no errors, this will create a file named MyProgram.class. This file contains the Java bytecode representing the instructions of your program.

Java String FAQ: How do I merge/combine two Java String fields?

You can merge/concatenate/combine two Java String fields using the + operator, as shown in this example code:

MySQL FAQ - how do I list all the databases in a MySQL database?

Simple. From the mysql command line, use this command:

show databases;


MySQL table/schema FAQ: How do I show a database table schema in a MySQL database?

Short answer: To show the schema for a MySQL database table, use the MySQL desc command. You issue this command from the MySQL client command line prompt, like this:

MySQL FAQ: How do I show/list the tables in a MySQL database (using the mysql command line client)?

To list/show the tables in a MySQL database: