Steps for Starting the database and populating the tables


Step 1: Start the database


● 3 Different options
– At the command line
– Through an IDE
– As windows service (Not all databases support this)
● Derby within the IDE
– From NetBeans, select Tools->Java DB Database-
>Start Java DB Server
● HSQLDB at the command line
– Create a directory to host a database
● mkdir c:\hsqldb\sampledb
– Create server.properties under c:\hsqldb\sampledb with the following two lines
● server.database.0=file:/hsqldb/sampledb/
● server.dbname.0=sampledb
– Run the database server at the command line
● java -classpath ..\lib\hsqldb.jar org.hsqldb.Server


Step 2: Create Database Schema (Optional Step)


● 2 different options
– Manually
– Use SchemaExport utility tool that comes with Hibernate
● You can generate the database schema from the mapping files
● For this sample application, we will not create the schema, instead we will create or populate the database table either manually or programmatically (within Java program)


Step 3: Create and Populate the database tables


● 4 different options
– Running Schema (SQL commands) at the command line
– Running SQL command within the IDE
– Create the table manually using database manager tool or within the IDE
– From the Java program


Steps to follow for Writing files
1.Write domain classes (as POJO classes)
➢ Person.java
2.Write or generate Hibernate mapping files for the domain classes
➢ Person.hbm.xml
3.Write Hibernate configuration file
➢ hibernate.cfg.xml


Step 1: Write Domain Classes (Person.java)



public class Person implements Serializable {
private int id;
private String name;
protected Person() {
}
public Person(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}



Step 2: Write Mapping Files for Domain Classes (Person.hbm.xml)



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Person" table="person">
<id name="id" type="int">
<generator class="increment"/>
</id>
<property name="name" column="cname" type="string"/>
</class>
</hibernate-mapping>



Step 3: Write Hibernate configuration file (hibernate.cfg.xml) – Derby



<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property
name="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property
name="connection.url">jdbc:derby://localhost:1527/mydatabase</property>
<property name="connection.username">app</property>
<property name="connection.password">app</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.DerbyDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<!-- Mapping files -->
<mapping resource="Person.hbm.xml"/>
</session-factory>



Step 3: Write Hibernate configuration file (hibernate.cfg.xml) – MySQL



<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="myeclipse.connection.profile">mysql</property>
<mapping resource="Person.hbm.xml" />
</session-factory>
</hibernate-configuration>



Steps to follow for Building and Running the application


1.Add Hibernate library files to the classpath
2.Add the database driver to the classpath
3.Compile and run the application performing database operation


Step 1: Copy Hibernate Library Files to the classpath


● Hibernate library files from Hibernate distribution
– hibernate3.jar
– other dependency files


Step 2: Add the database driver to the classpath


● The application as a database client needs to have database-specific database driver
– derbyclient.jar (for Derby)
– hsqldb.jar (for HSQLDB)
– mysql-connector-java-5.0.3-bin.jar (for MySql)


Step 3: Compile and run the application


● As a standalone application or Web application
● In this example, we will run it as a standalone application (that has a main() method)

Leave a Reply

Subscribe to Posts | Subscribe to Comments

About This Site

Howdy! My name is Suersh Rohan and I am the developer and maintainer of this blog. It mainly consists of my thoughts and opinions on the technologies I learn,use and develop with.

Blog Archive

Powered by Blogger.

- Copyright © My Code Snapshots -Metrominimalist- Powered by Blogger - Designed by Suresh Rohan -