File: Main.java
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
public class Main {
public static void main(String[] args) throws Exception {
HibernateUtil hibernateUtil = new HibernateUtil();
hibernateUtil
.executeSQLCommand("create table CD(id varchar,title varchar,artist varchar,purchaseDate date,cost decimal(6,2))");
Session session = hibernateUtil.getSession();
CD cd = new CD();
session.save(cd);
session.flush();
session.close();
hibernateUtil.checkData("select * from CD");
}
}
File: CD.hbm.xml
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
File: CD.java
import java.util.Date;
public class CD {
String id;
String title;
String artist;
Date purchaseDate;
double cost;
public CD() {
}
public CD(String title, String artist, Date purchaseDate, double cost) {
this.title = title;
this.artist = artist;
this.purchaseDate = purchaseDate;
this.cost = cost;
}
public void setId(String id) {
this.id = id;
}
public String getId(){
return id;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getArtist() {
return artist;
}
public void setPurchasedate(Date purchaseDate) {
this.purchaseDate = purchaseDate;
}
public Date getPurchasedate() {
return purchaseDate;
}
public void setCost(double cost) {
this.cost = cost;
}
public double getCost() {
return cost;
}
}
File: hibernate.cfg.xml
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
org.hsqldb.jdbcDriver
jdbc:hsqldb:data/tutorial
sa
1
org.hibernate.dialect.HSQLDialect
org.hibernate.context.ManagedSessionContext
false
false
org.hibernate.cache.NoCacheProvider
true
File: HibernateUtil.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
Session session;
Statement st;
Configuration config;
public HibernateUtil() throws Exception{
config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
// Load the JDBC driver.
Class.forName("org.hsqldb.jdbcDriver");
System.out.println("Driver Loaded.");
// Establish the connection to the database.
String url = "jdbc:hsqldb:data/tutorial";
Connection conn = DriverManager.getConnection(url, "sa", "");
System.out.println("Got Connection.");
st = conn.createStatement();
}
public Configuration getConfiguration(){
return config;
}
public Session getSession(){
return session;
}
public void executeSQLCommand(String sql) throws Exception {
st.executeUpdate(sql);
}
public void checkData(String sql) throws Exception {
ResultSet rs = st.executeQuery(sql);
ResultSetMetaData metadata = rs.getMetaData();
for (int i = 0; i < metadata.getColumnCount(); i++) {
System.out.print("\t"+ metadata.getColumnLabel(i + 1));
}
System.out.println("\n----------------------------------");
while (rs.next()) {
for (int i = 0; i < metadata.getColumnCount(); i++) {
Object value = rs.getObject(i + 1);
if (value == null) {
System.out.print("\t ");
} else {
System.out.print("\t"+value.toString().trim());
}
}
System.out.println("");
}
}
}