If you're using System.out.println() in your program to output debug messages, you should really take a look at log4j. Basically, you use it like:
package com.test;If you need to see the debug messages, you can enable an option in a config file. Otherwise, disable it in the config file. It means you can (and should) leave the debug statements in the production version. When something is wrong and you need to trace it, just enable the option in the config file. To learm more about log4j, here is an easy-to-follow tutorial.
import org.apache.log4j.*;
public class Foo {
private static Logger logger = Logger.getLogger(Foo.class);
public static void main(String[] args) {
logger.debug("message 1");
logger.debug("message 2");
}
}
Suppose that you have a class:
public class Product {
private int id;
private String name;
private double price;
}
To save a Product object to the database using Hibernate, all you need
is a single statement:Session session = ...;A Hibernate session is like a JDBC connection. When you run the code above, the Hibernate initialization code will create a table named "Product" that has three fields: id (integer), name (varchar(256)) and price (float). When you call save(), it will store "p" into a new record into the table.
Product p = new Product();
session.save(p);
How does it know where database is? You tell it in a config file. If you'd like to specify the table name and field names (e.g., map the instance variable "price" to a table field "unit_price"), you can also do it in the config file.
Retrieving objects from the database is equally easy. That's why Hibernate has become the de-facto standard tool for database access in Java. To learn more, you can join our Reducing your database access effort with Hibernate course.Test Driven Development (TDD) is an important practice in eXtreme Programming (XP). If you'd like to learn how to do TDD in a .NET project, you may check out the book "Test-Driven Development in Microsoft .NET" by James Newkirk published by Microsoft. It's available at the Cyber-Lab Library for borrowing.
To learn XP, join our upcoming XP
course.
Have any questions, ideas or experiences regarding software development? Contact me at 781313 or kent at cpttm dot org dot mo.
Until next time,
Kent Tong