package entities; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Subscriber implements Serializable { @Id private String uname; private String pwd; public Subscriber() { } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }
<?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> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property> <property name="connection.username">hib</property> <property name="connection.password">hib</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!--create the database schema on startup if required --> <property name="hbm2ddl.auto">update</property> <mapping class="entities.Subscriber"/> </session-factory> </hibernate-configuration>
<%@page contentType="text/html"%> <%@page 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>Registration</title> </head> <body> <h1>Registration</h1> <form action="register" method="post"> <table cellpadding="3pt"> <tr> <td>Username : </td> <td><input type="text" name="uname" size="20"/> </td> </tr> <tr> <td>Password : </td> <td><input type="password" name="pwd" size="20"/> </td> </tr> <tr> <td>Confirm Password : </td> <td><input type="password" name="pwd2" size="20"/> </td> </tr> </table> <p/> <input type="submit" value="Register"/> </form> </body> </html>
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import org.hibernate.Session; import org.hibernate.SessionFactory; import entities.*; import org.hibernate.cfg.AnnotationConfiguration; public class RegisterServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String uname = request.getParameter("uname"); String pwd = request.getParameter("pwd"); try { SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory(); Session s = sf.openSession(); Subscriber sub = new Subscriber(); sub.setUname(uname); sub.setPwd(pwd); s.beginTransaction(); s.save(sub); s.getTransaction().commit(); s.close(); sf.close(); out.println( "<h3>[" + uname + "] has been registered successfully!</h3>"); } catch(Exception ex) { out.println("<h3>[" + uname + "] could NOT be registered!</h3>"); System.out.println(ex.getMessage()); } } }
<?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="entities.Subscriber" table="SUBSCRIBER"> <id name="uname" column="uname"> <generator class="assigned"/> </id> <property name="pwd"/> </class> </hibernate-mapping>
<?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> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:XE</property> <property name="connection.username">hib</property> <property name="connection.password">hib</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping resource="entities/Subscriber.hbm.xml"/> </session-factory> </hibernate-configuration>
try { SessionFactory sf = new Configuration().configure().buildSessionFactory(); Session s = sf.openSession(); ... }