Just to add to Meghana's suggestion, have an errorPage directive in your jsp
page so that if a error is thrown from your getRecords() method, the user is
forwarded to the Error Page.
This way your jsp is not cluttered with the try{} catch {} blocks.
Have a nice day.
With regards,
Sachin S. Khanna.
www.emailanorder.com
----- Original Message -----
From: Meghana <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, January 05, 2001 11:25 AM
Subject: Re: Executing SQL query in bean
> I think the way to do this would be the method getRecords should be
designed
> to throw the exception to the calling JSP. And when u r calling this
method
> in the JSP, make it a point to use a try-catch block around it.So whenever
> there is an exception in this method u r JSP wud be able to print the
error
> message thrown.
>
> the syntax for defining this method wud be : Inside the bean:
>
> say
>
> public ResultSet getRecords() throw SQLException
> {
> |
> |
> try
> {
> ResultSet rs = stmt.executeQuery(SQLStatement);
> return rs;
> }
> catch(SQLException e)
> {
> throw e;
> }
> }
>
> and in the calling JSP,
>
> try
> {
> rs = <bean instance name>.getRecords()
> }
> catch(SQLException e)
> {
> out.println("Error : " + e.getMessage());
> }
>
> So with normal operation, the method returns the resultset and in case of
exception it throws the e. I think this should work.
>
> Meghana.
>
> -----Original Message-----
> From: A mailing list about Java Server Pages specification and reference
> [mailto:[EMAIL PROTECTED]]On Behalf Of Hariharan N
> Sent: Friday, January 05, 2001 10:57 AM
> To: [EMAIL PROTECTED]
> Subject: Re: Executing SQL query in bean
>
>
> Thanks! for the clarification.
> but my second query was... suppose u have a method getRecords in a bean
> which should(assume) return resultset.. now if any exception occurs in
this
> method i want to display a string (a custom error message) back in the JSP
> page. Since the mthod is defined to send resultset it can't send String so
> is it possible to invoke a method back in JSP page to print this error
> message.
> any other way apart from using errorpage!!!
>
>
>
> At 08:46 AM 1/4/01 -0800, you wrote:
> >>From: Hariharan N <[EMAIL PROTECTED]>
> >>Reply-To: A mailing list about Java Server Pages specification and
> >> reference <[EMAIL PROTECTED]>
> >>To: [EMAIL PROTECTED]
> >>Subject: Re: Executing SQL query in bean
> >>Date: Thu, 4 Jan 2001 07:54:51 -0800
> >>
> >>Hi!
> >> Are u sure the System.out.println will display the records in the JSP
> >>page, think we have to use the JSP printWriter implicit object "out" to
> >>display something in the JSP page.
> >
> > No, the System.out.println() does not print anything in the jsp file.
You
> >are right you need the out.println() instead. The use of the System... is
> >for informative reasons. I just put it there in order to show you how it
> >should be done. Actually the System.out would not print anything on the
> >screen (unless you have a main method defined) but all the output will go
> >into the default output files under the log directory of your JSP engine
you
> >are using.
> >
> >>Now this gives rise to..
> >>1. Suppose we want to display something from a bean like
System.out.println
> >>in the JSP page,
> >>do we have to pass the implicit out object to the bean or which way?
> >>can we invoke a method say.. write() in the JSP page from a bean??
> >
> > If you want to display the records you can declare the method of type
> >String (or Vector or ResultSet or anything you want) and return a
specific
> >value. Then call this method from inside the jsp file and all the data
you
> >want will be displayed in the jsp page. For example you can have.
> >
> > public void String printThis()
> > {
> > return "hello from printThis";
> > }
> >
> >Inside the jsp you can have (if assume the reference to the class is
myRef)
> >
> > out.println(myRef.printThis());
> >
> > This will print the "hello from printThis".
> >
> >
> >>a jdbc question too..
> >>what exactly is class.forName does....
> >
> > It loads the appropriate database drivers. In order to make the
database
> >to work you need the right drivers. The class.forName loads the proper
> >classes (which are the drivers for the database). Every database has
> >different drivers, so it depends on the database you are using what
drivers
> >you need to load.
> >
> >>
> >>
> >>At 03:15 AM 1/4/01 -0800, you wrote:
> >> > In the bean put all the relevant commands in order to open the
> >>connection
> >> >with the database. Lets say that you have a method called
> >> >
> >> >public void openConnectio
> n()
> >> >{
> >> > try
> >> > {
> >> > // Load the database's drivers.
> >> > Class.forName("database's drivers here");
> >> >
> >> > // Obtain connection with the database.
> >> > Connection dbcon = DriverManager.getConnection("databse
address
> >> >here", "username here", "password here");
> >> >
> >> > // create new statemtn here.
> >> > Statement stmt = dbcon.createStatement();
> >> >
> >> > String select = "SELECT * FROM <table name here>";
> >> >
> >> > // get the whole result set
> >> > ResultSet rs = stmt.executeQuery(retrieve);
> >> >
> >> > // go through all rows
> >> > while (rs.next())
> >> > {
> >> > System.out.println(rs.toString());
> >> > }
> >> >
> >> > } // end try
> >> > // If driver not found.
> >> > catch (ClassNotFoundException cnfe)
> >> > {
> >> > System.out.println("Driver not found. " + cnfe);
> >> > }
> >> > catch (SQLException sqle)
> >> > {
> >> > System.out.println("Connection to the database could not be
> >>established.
> >> >" + sqle);
> >> > }
> >> >}
> >> >
> >> >Then define a bean in the jsp page using the <jsp:useBean> tag.
> >> >
> >> > Call the openConnection using the beans reference name (lets say
that
> >>the
> >> >reference to the bean is called myBean) myBean.openConnection();
> >> >this will open the connection with the database and will display all
the
> >> >data in the specified table.
> >> >
> >> > You can have two diffreent methods if you want. One to open the
> >> >copnnection and the other to get the data. You can call them with the
> >>same
> >> >way. Do not forget to close the connection with the databse after you
> >> >finish. Use the close() method in order to do it.
> >> >
> >> > Hope this helps.
> >> >
> >> >
> >> >
> >> >
> >> >>From: Deepak Kumar <[EMAIL PROTECTED]>
> >> >>Reply-To: A mailing list about Java Server Pages specification and
> >> >> reference <[EMAIL PROTECTED]>
> >> >>To: [EMAIL PROTECTED]
> >> >>Subject: Executing SQL query in bean
> >> >>Date: Thu, 4 Jan 2001 02:48:54 -0800
> >> >>
> >> >>Hi Friends,
> >> >>
> >> >>I want to write the code of JDBC connectivity in a
> >> >>bean and then use the resultset in JSP page how can i
> >> >>accomplish this.
> >> >>
> >> >>Thanks in advance.
> >> >>Deepak Kumar
> >> >>
> >> >>__________________________________________________
> >> >>Do You Yahoo!?
> >> >>Yahoo! Photos - Share your holiday photos online!
> >> >>http://photos.yahoo.com/
> >> >>
> >>
>
>>==========================================================================
> =
> >> >>To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> >> >>JSP-INTEREST".
> >> >>For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
> >> >>DIGEST".
> >> >>Some relevant FAQs on JSP/Servlets can be found at:
> >> >>
> >> >> http://java.sun.com/products/jsp/faq.html
> >> >> http://www.esperanto.org.nz/jsp/jspfaq.html
> >> >> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> >> >> http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
> >> >
> >>
> >_________________________________________________________________________
> >> >Get Your Private, Free E-mail from MSN Hotmail at
> http://www.hotmail.com.
> >> >
> >>
>
>===========================================================================
> >> >To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> >>JSP-INTEREST".
> >> >For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
> >>DIGEST".
> >> >Some relevant FAQs on JSP/Servlets can be found at:
> >> >
> >> > http://java.sun.com/products/jsp/faq.html
> >> > http://www.esperanto.org.nz/jsp/jspfaq.html
> >> > http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> >> > http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
> >> >
> >>
>
>>==========================================================================
> =
> >>To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> >>JSP-INTEREST".
> >>For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
> >>DIGEST".
> >>Some relevant FAQs on JSP/Servlets can be found at:
> >>
> >> http://java.sun.com/products/jsp/faq.html
> >> http://www.esperanto.org.nz/jsp/jspfaq.html
> >> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> >> http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
> >
> >_________________________________________________________________________
> >Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.
> >
>
>===========================================================================
> >To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> >For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
> DIGEST".
> >Some relevant FAQs on JSP/Servlets can be found at:
> >
> > http://java.sun.com/products/jsp/faq.html
> > http://www.esperanto.org.nz/jsp/jspfaq.html
> > http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> > http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
> >
>
>
===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
> DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>
>
===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
> For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST
DIGEST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets