Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Servlet inserting record into postgresql through hibernate

807601Dec 21 2007 — edited Dec 21 2007
Hi

I have developed a servlet. This servlet is inserting record into postgres sql through hibernate using eclispe ide. When I run my code then I find the following exception

exception

java.lang.NullPointerException
hibernate.example.FirstExample.doGet(FirstExample.java:54)
javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)


I have developed the following code



package hibernate.example;

public class Contact {
private String firstName;
private String lastName;
private String email;
private long id;

public String getEmail() {
return email;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public void setEmail(String string) {
email = string;
}

public void setFirstName(String string) {
firstName = string;
}

public void setLastName(String string) {
lastName = string;
}

public long getId() {
return id;
}

public void setId(long l) {
id = l;
}

}


**Now the servlet is**

package hibernate.example;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class FirstExample extends HttpServlet {

Session session = null;

public String getServletInfo() {
return "Servlet connects to PostgreSQL database and displays result of a SELECT";
}


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

try{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
session = factory.openSession();

out.println("Inserting Record");

Contact contact = new Contact();
contact.setId(6);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session.save(contact);
out.println("Done");
}
catch(Exception e){
System.err.println(e.getMessage());
}
finally{

session.flush();
session.close();
}
}
}

**web.xml is**

<web-app>

<database>
<driver>
<type>org.postgresql.Driver</type>
<url>jdbc:postgresql://127.0.0.1:5432/ali</url>
<user>ali</user>
<password>ali</password>
</driver>
</database>

<servlet>
<servlet-name>FirstExample</servlet-name>
<servlet-class>hibernate.example.FirstExample</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>FirstExample</servlet-name>
<url-pattern>/hb</url-pattern>
</servlet-mapping>

</web-app>

**And contact.hbm.xml is**

<?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="hibernate.example.Contact" table="CONTACT">
<id name="id" type="long" column="ID" >
<generator class="assigned"/>
</id>

<property name="firstName">
<column name="FIRSTNAME" />
</property>
<property name="lastName">
<column name="LASTNAME"/>
</property>
<property name="email">
<column name="EMAIL"/>
</property>
</class>
</hibernate-mapping>

**And hibernate.cfg.xml is**

<?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="hibernate.connection.driver_class">com.postgressql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1:5432/ali</property>
<property name="hibernate.connection.username">ali</property>
<property name="hibernate.connection.password">ali</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Pleas tell me that where I have done the mistake and how can I resolove the error.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 18 2008
Added on Dec 21 2007
10 comments
857 views