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

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
Password:

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 java.sun.com) if you don't already have one. Next, assuming you have a program named MyProgram.java that you want to compile, you compile it like this with the javac:

javac MyProgram.java

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:

MySQL “users” FAQ: How do I add a new user to a MySQL database (i.e., how do I create a MySQL user account, and then grant database permissions to that new user account)?

Solution: MySQL add user and grant syntax

Here’s an example of what I did recently to (a) create a new MySQL database and then (b) add a new MySQL user account to work with that database.

Step 1: Log in to the database

First, from my Unix prompt, I logged into my MySQL database server with the mysql command line client:

MySQL backup FAQ - How do I backup/dump a MySQL database schema?

Answer: Use the mysqldump database utility.

MySQL dump examples using the mysqldump utility

On a DOS/Windows pc with no name/password protection, you can dump a database named my_db with the following command, but don't do this just yet:

mysqldump my_db

Note that this gets you not only the database schema, but also the current data in the table.

CVS Login FAQ: Can you show me an example CVS login command?

Sure. Here's CVS login command example:

cvs -d :pserver:al@cvs.devdaily.com:/usr/local/cvs login

This cvs login example assumes the following:

Assuming you are using Postgres (Postgresql), and:

  • you have *just* done an INSERT into a table named order,
  • the name of the sequence for that table is order_order_id_seq
  • you are using the Java programming language
  • you are still using the same database connection that you used with your INSERT statement

to get the value of the Postgres serial value just created after your INSERT statement, use some code like this:

Java JDBC / database FAQ: Where can I get a JDBC driver for Postgres/Postgresql?

Templates are code snippets that can be easily included in your Java code. The basic idea is that you type in a shortcut code, hit [Ctrl][J], and JBuilder fills in the remainder of the code. You type in a few characters, and JBuilder does the hard work for you.

CVS command FAQ: How do I know if all the changes in my CVS sandbox have been committed?

One way to do this is to move to the directory just above the module you have checked out, then run the command cvs release command. Let's look at an example:

Java FAQ: How do I multiple two integers (int) in Java?

Here's a quick example:

int i = 3;
int j = 4;
int k = i * j;
System.out.println("k = " + k);

The value of k here will be 12.