entities/Songs.Java
package entities;
public class Song {
private int songid;
private String title,singer;
public String getSinger() {
return singer;
}
public void setSinger(String singer) {
this.singer = singer;
}
public int getSongid() {
return songid;
}
public void setSongid(int songid) {
this.songid = songid;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
dao/SongsDAO.java
SongsDAO is used to perform database operations using Hibernate. It is designed in
such a way, if at all any changes to data access API is to be done, it is
incorporated here, so that JSPs (view) that interact with it do not change at
all.
package dao;
import entities.Song;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class SongsDAO {
private static SessionFactory sf = null;
static {
sf = new Configuration().configure().buildSessionFactory();
}
public static boolean addSong(String title, String singer) {
Session session = sf.openSession();
Transaction tx = session.beginTransaction();
Song s = new Song();
s.setTitle(title);
s.setSinger(singer);
try {
session.save(s);
tx.commit();
return true;
} catch (Exception ex) {
System.out.println(ex.getMessage());
tx.rollback();
return false;
} finally {
session.close();
}
}
public static List getSongs() {
Session session = sf.openSession();
List songs = (List) session.createQuery("from Song").list();
session.close();
return songs;
}
}
hibernate.cfg.xml
Place hibernate configuration file in default package (no package).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">music</property>
<property name="hibernate.connection.password">music</property>
<mapping resource="song.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Song.hbm.xml
Place Hibernate mapping file in the default package (no package).
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="entities.Song" table="Songs">
<id name="songid">
<generator class="sequence">
<param name="sequence">songid</param>
</generator>
</id>
<property length="50" name="title"/>
<property length="30" name="singer"/>
</class>
</hibernate-mapping>
index.jsp
Place index.jsp in the root directory of the application. It provides links to other pages.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Songs Database</title>
</head>
<body>
<h1>Songs Database</h1>
<a href="addsong.jsp">Add New Song</a>
<p/>
<a href="listsongs.jsp">List Songs</a>
<p/>
</body>
</html>
addsong.jsp
This JSP is used to take required information from user and passes it to addSong() method of SongsDAO.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Add Song</title>
</head>
<body>
<h1>Add Song</h1>
<form action="addsong.jsp" method="post">
Song Title : <input type="text" size="30" name="title"/>
Singer Name : <input type="text" size="30" name="singer"/>
<p/>
<input type="submit" value="Add Song" />
</form>
<%
String title = request.getParameter("title");
String singer = request.getParameter("singer");
if ( title != null) {
if ( dao.SongsDAO.addSong(title, singer) )
out.println("<p/>Song has been added!");
else
out.println("<p/>Song was not added successfully");
}
%>
<p/>
<a href="index.jsp">Home Page </a>
<a href="listsongs.jsp">List Songs </a>
</body>
</html>
listsongs.jsp
This page is used to display the list of songs.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>List Of Songs</title>
</head>
<body>
<h1>List Songs</h1>
<%
pageContext.setAttribute("songs", dao.SongsDAO.getSongs());
%>
<table border="1">
<tr>
<th>Song ID </th>
<th>Title </th>
<th>Singer</th>
</tr>
<c:forEach items="${songs}" var="song">
<tr>
<td>${song.songid}</td>
<td>${song.title}</td>
<td>${song.singer}</td>
</tr>
</c:forEach>
</table>
<p/>
<a href="index.jsp">Home Page </a>
</body>
</html>
That's All
Well that's all that you have to do to get songs added and listed.
Comments
|
|
Posted By mohan On 10-May-11 12:43:48 PM
its very cool and useful in my new project...
im using spring with hibernate....
thanQ Sir...
Hav a Nice Day...
|
|
|
Posted By Suman On 27-Jun-11 05:25:47 PM
Hi Srikanth,
Nice to see a good practical guide, It would be still better if you could provide the project structure in the IDE because beginners are confused with the location of the files.
waiting for your update
Regards,
Suman
|
|
|
Posted By Suman On 28-Jun-11 11:14:13 AM
Hi Srikanth,
I tried to run the above application.Below are the steps
1)Created Dynamic Web Project
2)Under src folder created packages i)entities and ii) dao and out the Song and SangsDAO in to the packages.
3)created jsp folder under WEB-INF and put the jsp files into the folder
4) Build the project
4) Deployed the War file
When i try to run the application, i get the home page with he links "Add Songs"
"List Songs".
When i click on any AddSongs link, i get the error
SongsHibernate/addSongs.jsp not available.
Can you please help if i am missing any thing
Suman
|
|
|
Posted By Shabeer On 12-Dec-11 05:11:06 PM
Sir,
Please give me a tutorial about hibernate....
|