Hibernate: Creating a Table

The next step is to create a database table. Since, MySQL is one of the most popular open-source database systems available today, we shall use it to demonstrate hibernate. We assume that a MySQL instance is running in the host having IP address 172.16.5.81 and the user name and password to access this database are root and nbuser respectively. We also assume that a database test is already created and configured. If it does not exist, create it using the following command at the MySQL command prompt:

mysql> create database test;

Query OK, 1 row affected (0.00 sec)

Now, connect to test database using the following SQL command:

mysql> connect test;

Connection id: 88

Current database: test

Our database is now ready. We can create tables in it. For each class, there should exist a database table which will hold object’s state (attribute values). Since, a Book object will have three fields (b_id, b_titie and b_price), let us create a table tbi_book having three columns using the following SQL command:

create table tbl_book (

no int auto_increment,

name varchar(80),

pr int,

primary key (no)

);

Here no, name and pr columns correspond to id, title and price fields of the Book class respectively.

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *