Solved

Hibernate error.....

Posted on 2006-06-26
50
1,376 Views
Last Modified: 2013-11-24
Hi Guys,

Here is my Code:

______CD.hbm.xml_______
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
http://www.sourceforae.net/hibernate-mapping-2.0.dtd>
<hibernate-mapping>
<class name="CD"
table="cd">
<id name="id"
type="int"
unsaved-value="null">
<column name="ID"
sql-type="int"
not-null="true"/>
<generator class="hilo"/>
</id>
<property name="title"/>
<property name="artist"/>
<property name="purchasedate" type="date"/>
<property name="cost" type="double"/>
</class>
</hibernate-mapping>

________CDViewer.java_______
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import java.util.*;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

public class CDViewer extends HttpServlet {
      private SessionFactory sessionFactory;
      
      public void init(ServletConfig config) throws ServletException {
            super.init(config);
            
            try {
                  sessionFactory = new Configuration().addClass(CD.class)
                  .buildSessionFactory();
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
      private void displayAll(PrintWriter out, Session session) {
          try {
            out.println("<html>");
            out.println("<table border='1'>");
            out.println("<tr><td>Title</td><td>Artist</td><td>cost</td></tr>");
            List cds = session.find("from CD");

            Iterator iter = cds.iterator();
            while (iter.hasNext()) {
              CD cd = (CD)iter.next();
              out.println("<tr><td>");
              out.println(cd.getTitle());
              out.println("</td><td>");
              out.println(cd.getArtist());
              out.println("</td><td>");
              out.println(cd.getCost());
              out.println("</td></tr>");
            }
          } catch(Exception e) {}

          out.println("</table>");
          out.println("</html>");
        }
      
      public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
            
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            
            Session session = null;
            
            try {
                  session = sessionFactory.openSession();
                  
                  String action = request.getParameter("submit");
            
                  if (action.equals("Pull All CDs")) {
                        displayAll(out, session);
                  } else {
                        out.println("Bad Input");
                  }
                  
                  session.flush();
                  session.close();
                  
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
      
      public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
            doGet(request, response);
      }
}


_______CD.java___________

import java.io.*;
import java.util.*;

public class CD {
int id;
String title;
String artist;
Date purchaseDate;
double cost;
public CD() {
}
public CD(String title,
String artist,
Date purchaseDate,
double cost) {
this.title = title;
this.artist = artist;
this.purchaseDate = purchaseDate;
this.cost = cost;
}
public void setId(int id) {
this.id = id;
}
public int getId(){
return id;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getArtist() {
return artist;
}
public void setPurchasedate(Date purchaseDate) {
this.purchaseDate = purchaseDate;
}
public Date getPurchasedate() {
return purchaseDate;
}
public void setCost(double cost) {
this.cost = cost;
}
public double getCost() {
return cost;
}
}

_______Error Messages i get when i run the code __________


net.sf.hibernate.MappingException: Error reading resource: CD.hbm.xml
      at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:358)
      at CDViewer.init(CDViewer.java:18)
      at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1029)
      at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:687)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:144)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
      at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Thread.java:595)
Caused by: net.sf.hibernate.MappingException: org.dom4j.DocumentException: Error on line 4 of document  : The system identifier must begin with either a single or double quote character. Nested exception: The system identifier must begin with either a single or double quote character.
      at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:297)
      at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:355)
      ... 25 more
Caused by: org.dom4j.DocumentException: Error on line 4 of document  : The system identifier must begin with either a single or double quote character. Nested exception: The system identifier must begin with either a single or double quote character.
      at org.dom4j.io.SAXReader.read(SAXReader.java:355)
      at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:287)
      ... 26 more
java.lang.NullPointerException
      at CDViewer.doGet(CDViewer.java:56)
      at CDViewer.doPost(CDViewer.java:76)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
      at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Thread.java:595)
-------------------------------------------------------------------------------------------------------------------------------------------

>>>please help me to rectify the error.

Very Urgent......!!!

Thank You,
___Jags.
0
Comment
Question by:jagadeesh_motamarri
  • 27
  • 13
  • 10
50 Comments
 
LVL 30

Expert Comment

by:mayankeagle
ID: 16989920
Might not be related to your problem - but going by Java bean standards:

>> Date purchaseDate;

the getter for this should be getPurchaseDate () with a capital D and not getPurchasedate (). Same for the setter.
0
 
LVL 3

Expert Comment

by:runa_paathak
ID: 16990000
Also, you have misspelled sourceforge in ur DTD declaration of CD.hbm.xml. In fact change the whole DOCTYPE line, it should read:

<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 16990001
Hi mayankeagle,

Thank you for a quick response..

Now i've this -

_____CD.java____
import java.io.*;
import java.util.*;

public class CD {
int id;
String title;
String artist;
Date purchaseDate;
double cost;
public CD() {
}
public CD(String title,
String artist,
Date purchaseDate,
double cost) {
this.title = title;
this.artist = artist;
this.purchaseDate = purchaseDate;
this.cost = cost;
}
/**
 * @return Returns the artist.
 */
public String getArtist() {
      return artist;
}
/**
 * @param artist The artist to set.
 */
public void setArtist(String artist) {
      this.artist = artist;
}
/**
 * @return Returns the cost.
 */
public double getCost() {
      return cost;
}
/**
 * @param cost The cost to set.
 */
public void setCost(double cost) {
      this.cost = cost;
}
/**
 * @return Returns the id.
 */
public int getId() {
      return id;
}
/**
 * @param id The id to set.
 */
public void setId(int id) {
      this.id = id;
}
/**
 * @return Returns the purchaseDate.
 */
public Date getPurchaseDate() {
      return purchaseDate;
}
/**
 * @param purchaseDate The purchaseDate to set.
 */
public void setPurchaseDate(Date purchaseDate) {
      this.purchaseDate = purchaseDate;
}
/**
 * @return Returns the title.
 */
public String getTitle() {
      return title;
}
/**
 * @param title The title to set.
 */
public void setTitle(String title) {
      this.title = title;
}




}


-----------Error Message ---------

net.sf.hibernate.MappingException: Error reading resource: CD.hbm.xml
      at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:358)
      at CDViewer.init(CDViewer.java:18)
      at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1029)
      at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:687)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:144)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
      at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Thread.java:595)
Caused by: net.sf.hibernate.MappingException: org.dom4j.DocumentException: Error on line 4 of document  : The system identifier must begin with either a single or double quote character. Nested exception: The system identifier must begin with either a single or double quote character.
      at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:297)
      at net.sf.hibernate.cfg.Configuration.addClass(Configuration.java:355)
      ... 25 more
Caused by: org.dom4j.DocumentException: Error on line 4 of document  : The system identifier must begin with either a single or double quote character. Nested exception: The system identifier must begin with either a single or double quote character.
      at org.dom4j.io.SAXReader.read(SAXReader.java:355)
      at net.sf.hibernate.cfg.Configuration.addInputStream(Configuration.java:287)
      ... 26 more
java.lang.NullPointerException
      at CDViewer.doGet(CDViewer.java:56)
      at CDViewer.doPost(CDViewer.java:76)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
      at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Thread.java:595)



Lemme me know if i need to provide any more info

Thank You,
____Jags.
0
 
LVL 3

Expert Comment

by:runa_paathak
ID: 16990008
Like I said, this is because of errors in your CD.hbm.xml. Make the changes that I suggested.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 16990011
You need to correct the XML file too, as you've been told by runa_pathak.
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 16990049
Well....after changing that...i simply get his error without any anticipated result...

java.lang.NullPointerException
      at CDViewer.doGet(CDViewer.java:56)
      at CDViewer.doPost(CDViewer.java:76)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
      at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Thread.java:595)


____Jags.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 16990056
>> at CDViewer.doGet(CDViewer.java:56)

Which line is that?
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 16990070
     session = sessionFactory.openSession();
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 16990072
and since i'm using mysql....i've added this part to server.xml in Tomcat 5.5

 
       <Context path="/cdviewer" docBase="/cdviewer">
       <Resource name="jdbc/pullcd"
       scope="Shareable"
       type="javax.sql.DataSource"/>
       <ResourceParams name="jdbc/pullcd">
       <parameter>
       <name>factory</name>
       <value>
       org.apache.commons.dbcp.BasicDataSourceFactory
       </value>
       </parameter>
       <parameter>
       <name>url</name>
       <value>jdbc:mysgl://localhost/CD</value>
       </parameter>
       <parameter>
       <name>driverClassName</name>
       <value>com.mysql.jdbc.Driver</value>
       </parameter>
       <parameter>
       <name>username</name>
       <value>root</value>
       </parameter>
       <parameter>
       <name>password</name>
       <value>jags</value>
       </parameter>
       <parameter>
       <name>maxWait</name>
       <value>2000</value>
       </parameter>
       <parameter>
       <name>maxIdle</name>
       <value>250</value>
       </parameter>
       <parameter>
       <name>maxActive</name>
       <value>5</value>
       </parameter>
       </ResourceParams>
       </Context>
      

0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 16990075
>> sessionFactory = new Configuration().addClass(CD.class).buildSessionFactory();

Is sessionFactory null? Can you debug/ print that value on the server console and check?
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 16990076
even i replaced <value>jdbc:mysgl://localhost/CD</value> with proper spelling.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 16990090
<value>
      org.apache.commons.dbcp.BasicDataSourceFactory
      </value>

I could be wrong but to be on the safe side, I'd prefer not to have new-lines and rely on the server to trim them:

<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 16990097
<value>jdbc:mysgl://localhost/CD</value> with proper spelling.

You mean jdbc:mysql://localhost/CD ?
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 16990107
yea..
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 16990131
Have you checked if the session-factory is null?
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 16990149
welll....can u let me know how check that...
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 16990155
System.out.println ( sessionFactory ) ; // ?

Then check the server console.
0
 
LVL 3

Expert Comment

by:runa_paathak
ID: 16990159
Just print out (System.out) sessionFactory in your init() method.
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 16993671
it is not printing anything from init method.....the control is going into doPost method...
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 16993687
and where should i place the hibernate.cfg.xml file....

and ofcourse my CD.hbm.xml file is in the tomcat directory - in my projects classes folder.


0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 16993720
i've placed the hibernate.cfg.xml file in classes folder of the project.
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 16994148
Ok...here is a good news... now everything seems to be set....except that i'm unable to retrieve the date from the database.

Here is a complete set of code -

_____CD.java_____
import java.io.*;
import java.util.*;
public class CD {
  int id;
  String title;
  String artist;
  Date purchaseDate;
  double cost;

  public CD() {
  }

  public CD(String title,
            String artist,
            Date purchaseDate,
            double cost) {
    this.title = title;
    this.artist = artist;
    this.purchaseDate = purchaseDate;
    this.cost = cost;
  }

  public void setId(int id) {
    this.id = id;
  }

  public int getId(){
    return id;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public String getTitle() {
    return title;
  }

  public void setArtist(String artist) {
    this.artist = artist;
  }

  public String getArtist() {
    return artist;
  }

  public void setPurchasedate(Date purchaseDate) {
    this.purchaseDate = purchaseDate;
  }

  public Date getPurchasedate() {
    return purchaseDate;
  }

  public void setCost(double cost) {
    this.cost = cost;
  }

  public double getCost() {
    return cost;
  }
}

_____CDViewer.java_____
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import java.util.*;

import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;

public class CDViewer extends HttpServlet {
      private SessionFactory sessionFactory;
      
      public void init(ServletConfig config) throws ServletException {
            super.init(config);
            System.out.println("Now entered init in CDViewer");
            try {
                  sessionFactory = new Configuration().addClass(CD.class).buildSessionFactory();
                  System.out.println("SessionFactory : "+sessionFactory);
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
      private void displayAll(PrintWriter out, Session session) {
          try {
            out.println("<html>");
            out.println("<table border='1'>");
            out.println("<tr><td>Title</td><td>Artist</td><td>cost</td></tr>");
            List cds = session.find("from CD");

            Iterator iter = cds.iterator();
            
            while (iter.hasNext()) {
                  System.out.println("Total: ");
                  CD cd = (CD)iter.next();
              out.println("<tr><td>");
              out.println(cd.getTitle());
              out.println("</td><td>");
              out.println(cd.getArtist());
              out.println("</td><td>");
              out.println(cd.getCost());
              out.println("</td></tr>");
            }
          } catch(Exception e) {}

          out.println("</table>");
          out.println("</html>");
        }
      
      public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
            System.out.println("Now entered doGet in CDViewer");
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            
            Session session = null;
            
            try {
                  session = sessionFactory.openSession();
                  
                  String action = request.getParameter("submit");
            
                  if (action.equals("Pull All CDs")) {
                        displayAll(out, session);
                  } else {
                        out.println("Bad Input");
                  }
                  
                  session.flush();
                  session.close();
                  
            } catch (Exception e) {
                  e.printStackTrace();
            }
      }
      
      public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {
            System.out.println("Now entered doPost in CDViewer");
            doGet(request, response);
      }
}

_____cd.html_____

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>CD VIEWER</TITLE>

<BODY>

<form action="/cdviewer/viewer" method="post">

<input type="submit" name="submit" value="Pull All CDs">

</form>
</BODY>

</BODY>
</HTML>

_______hibernate.cfg.xml______

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration
    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="connection.datasource">java:comp/env/jdbc/quickstart</property>
        <property name="show_sql">false</property>
        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>

        <!-- Mapping files -->
        <mapping resource="CD.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

_____CD.hbm.xml_____

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
   
   <hibernate-mapping>
<class name="CD"
table="cd">
<id name="id"
type="int"
unsaved-value="null">
<column name="ID"
sql-type="int"
not-null="true"/>
<generator class="hilo"/>
</id>
<property name="title"/>
<property name="artist"/>
<property name="purchasedate" type="date"/>
<property name="cost" type="double"/>
</class>
</hibernate-mapping>

_______server.xml in tomcat5.5______

 <Context path="/cdviewer" docBase="/cdviewer">
       <Resource name="jdbc/pullcd" scope="Shareable"       type="javax.sql.DataSource"/>
       <ResourceParams name="jdbc/pullcd">
             <parameter>
                   <name>factory</name>
                   <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
             </parameter>
             
             <parameter>
                   <name>url</name>
                   <value>jdbc:mysql://localhost/CD</value>
             </parameter>
      
             <parameter>
                   <name>driverClassName</name>
                   <value>com.mysql.jdbc.Driver</value>
             </parameter>
             
             <parameter>
                   <name>username</name>
                   <value>root</value>
             </parameter>
      
             <parameter>
                   <name>password</name>
                   <value>jags</value>
             </parameter>
      
             <parameter>
                   <name>maxWait</name>
                   <value>2000</value>
             </parameter>
      
             <parameter>
                   <name>maxIdle</name>
                   <value>250</value>
             </parameter>
      
             <parameter>
                   <name>maxActive</name>
                   <value>5</value>
             </parameter>
      
       </ResourceParams>
      
       </Context>

______ my projecct name_____

cdviewer

_______________________________

Please help find y i'm not able to retrive the information from the database.

And even the sessionFactory is not NULL...


______My console _____
Now entered init in CDViewer
SessionFactory : net.sf.hibernate.impl.SessionFactoryImpl@a2da07
Now entered doPost in CDViewer
Now entered doGet in CDViewer



_______Jags.


0
 
LVL 3

Expert Comment

by:runa_paathak
ID: 16998719
Is it displaying "Bad Input" in the browser?
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 16998899
well its not displaying anything expect the table row names.....(i'm printing them from servlet....so it is coming)
0
 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle earned 150 total points
ID: 16999508
Perhaps something wrong in the query - shoot the query in the database directly and see if any rows are being returned.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17001699
yea i can see the result when i shoot the query...!!
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17006831
Hi Guys,

I'm still struct up at the same place....please pull me up

Thank You,
____Jags
0
 
LVL 3

Expert Comment

by:runa_paathak
ID: 17007090
Perhaps it's throwing an exception, remove this line:

catch(Exception e) {}

from displayAll ()

and print out the stack trace ( e.printStackTrace()). Post it here if it's throwing some exception.
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17007144
ok...i did it..yea there is an exception...now let me  know how to rectify it...


Now entered init in CDViewer
SessionFactory : net.sf.hibernate.impl.SessionFactoryImpl@1ae6456
Now entered doPost in CDViewer
Now entered doGet in CDViewer
java.lang.UnsupportedOperationException: The user must supply a JDBC connection
      at net.sf.hibernate.connection.UserSuppliedConnectionProvider.getConnection(UserSuppliedConnectionProvider.java:32)
      at net.sf.hibernate.impl.BatcherImpl.openConnection(BatcherImpl.java:292)
      at net.sf.hibernate.impl.SessionImpl.connect(SessionImpl.java:3373)
      at net.sf.hibernate.impl.SessionImpl.connection(SessionImpl.java:3333)
      at net.sf.hibernate.impl.BatcherImpl.prepareQueryStatement(BatcherImpl.java:67)
      at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:784)
      at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
      at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
      at net.sf.hibernate.loader.Loader.doList(Loader.java:1063)
      at net.sf.hibernate.loader.Loader.list(Loader.java:1054)
      at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
      at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1554)
      at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1531)
      at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1523)
      at CDViewer.displayAll(CDViewer.java:28)
      at CDViewer.doGet(CDViewer.java:63)
      at CDViewer.doPost(CDViewer.java:79)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
      at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
      at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
      at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
      at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
      at java.lang.Thread.run(Thread.java:595)


_____Jags.
0
 
LVL 3

Expert Comment

by:runa_paathak
ID: 17007149
Looks like you have not configures hibernate.cfg.xml. Have you mentioned your database details in hibernate.cfg.xml? And is this in your classpath?
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17007152
how do i put this file in the classpath...

0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17007155
I have all the files here - E:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\cdviewer\WEB-INF\classes\

CD.class

CDViewer.class

CD.hbm.xml

hibernate.cfg.xml

____jags.
0
 
LVL 3

Expert Comment

by:runa_paathak
ID: 17007157
The same way you have CD.hbm.xml in your classpath. Try putting it in the same folder. If you are using an IDE like eclipse, you can add it from there.
0
 
LVL 3

Expert Comment

by:runa_paathak
ID: 17007159
Ok post the contents of you hibernate.cfg.xml
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17007170
<?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.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost/products</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">jags</property>
      <property name="hibernate.connection.pool_size">10</property>
      <property name="show_sql">true</property>
      <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <!-- Mapping files -->
      <mapping resource="CD.hbm.xml"/>
</session-factory>
</hibernate-configuration>





n where should i put this file...
0
 
LVL 3

Expert Comment

by:runa_paathak
ID: 17007188
It should be fine if it's in the classes folder. Is "products" the right database?

Also, try remove "hibernate" from all the properties in the hibernate.cfg.xml file. That is, change "hibernate.connection.driver_class" to "connection.driver_class" and so on for all properties.
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17007203
Yea .....products is my database name and CD and hibernate_unique_key are the tables in it.

and i made the change you suggested....still i get the same exception

____Jags
0
 
LVL 3

Expert Comment

by:runa_paathak
ID: 17007205
Is the JDBC driver in the classpath?
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17007213
Yea i just checked...yes it is...!!!

I appreciate your patience.......!
0
 
LVL 3

Expert Comment

by:runa_paathak
ID: 17007229
Thanks, I don't know how much longer I will have it :)

Remove the line  <property name="hibernate.connection.pool_size">10</property> from hibernate.cfg.xml.
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17007239
Still the same error.....

0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17007263
...well its too late for me now.

Please try let me know if i need to make any more changes.....



Thank You,
Jags.
0
 
LVL 3

Accepted Solution

by:
runa_paathak earned 350 total points
ID: 17007273
Remove this

 <Context path="/cdviewer" docBase="/cdviewer">
      <Resource name="jdbc/pullcd"
      scope="Shareable"
      type="javax.sql.DataSource"/>
      <ResourceParams name="jdbc/pullcd">
      <parameter>
      <name>factory</name>
      <value>
      org.apache.commons.dbcp.BasicDataSourceFactory
      </value>
      </parameter>
      <parameter>
      <name>url</name>
      <value>jdbc:mysgl://localhost/CD</value>
      </parameter>
      <parameter>
      <name>driverClassName</name>
      <value>com.mysql.jdbc.Driver</value>
      </parameter>
      <parameter>
      <name>username</name>
      <value>root</value>
      </parameter>
      <parameter>
      <name>password</name>
      <value>jags</value>
      </parameter>
      <parameter>
      <name>maxWait</name>
      <value>2000</value>
      </parameter>
      <parameter>
      <name>maxIdle</name>
      <value>250</value>
      </parameter>
      <parameter>
      <name>maxActive</name>
      <value>5</value>
      </parameter>
      </ResourceParams>
      </Context>
     

from your server.xml. I think this is causing it to expect you to provide the connection pool.
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17007329
hmm...still no luck...

I even tried this....http://www.jroller.com/page/jgani?entry=hibernate_oops_2


Still no use...
0
 
LVL 3

Expert Comment

by:runa_paathak
ID: 17007543
You have given " org.apache.commons.dbcp.BasicDataSourceFactory" in your server.xml. But you are not using DBCP connection pooling, are you? I think it is expecting you to provide the connections.
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 17008393
>> Yea i just checked...yes it is...!!!

Try putting it in your lib directly.
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17009784
>>>Try putting it in your lib directly.

tried it.....no difference
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17014428
ok...where should i place the jdbc jar files and where i should not...?
0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17015600
OK Guys,

Herez a good news .....After 2 days...i was able to solve it.

Here is wat i did...

1. Previosly i was using Hibernate2.1 - so replaced it with Hibernate3.jar
2. If we use Hibernate3.jar the statement  sessionFactory = new Configuration().addClass(CD.class).buildSessionFactory(); should be split into with a minor correction as follows -
    Configuration confi = new Configuration.addClass(CD.class);
    sessionFactory = confi.configure().buildSessionFactory();
3. Third importantly copied JDBC driver to WEB-INF/lib. (Never ever copy anything in Tomcat into a global directory outside of your web application or you are in classloader hell!)

Source URLs:  
http://www.hibernate.org/114.html (important for setting)
http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=78&t=000881 (most important for method)

_____Jags.


0
 
LVL 10

Author Comment

by:jagadeesh_motamarri
ID: 17015604
Runa_pathank ---- was almost with me helping to figure out many things.

ofcourse mayankeagle too (initially though)

so splitting the points.


____Jags
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Books that can get me started on JAVA 2 53
firstChar challenge 13 83
Systems talking to each other 5 108
github account with ecipse 1 16
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now