May I offer a more efficient process?

  String tmpDateStr = request.getParameter("releaseDate");
  SimpleDateFormat sdfTemplate = new SimpleDateFormat("yyyy-MM-dd");
  java.util.Date date = null;
  try {
    date = sdfTemplate.parse(tmpDateString);
  }
  catch (ParseException e) {
    // Deal with it
  }

To use the date as a parameter value in a PreparedStatement, you can
then use either:

  ps.setObject(index, date);

or
  // Convert the java.util.Date to a java.sql.Date
  ps.setDate(index, new java.sql.Date(date.getTime());

I have also added a few comments about your code, see intermixed below.

I would also suggest that you use a custom tag library for database access
instead of putting so much code in your JSP pages. You don't even have to
develop one yourself; there are plenty of tag libraries available, even for
free, that deals with database access. For instance, you could use the one
I describe in my book and do what you want like this:

  <ora:sqlUpdate dataSource="myDataSource">
    INSERT INTO MyTable (MyDateColumn)
    <ora:sqlDateValue param="releaseDate" pattern="yyyy-MM-dd" />
  </ora:sqlUpdate>

Examples and source code are available at <http://TheJSPBook.com/>

You can find tag libraries with similar features on a number of other
sites, for instance the Jakarta Taglibs site:

  <http://jakarta.apache.org/taglibs/>

or one of the resource sites listed on the main JSP site:

  <http://java.sun.com/products/jsp/>

Another alternative is of course to use a servlet for the database
access (maybe you already do this), and use JSP only to render the
response. For more about this, see for instance the Jakarta Struts
site: <http://jakarta.apache.org/struts/>

Hans

James Childers wrote:
>
> I spent most of yesterday afternoon getting a string pulled in via a
> request.getParameter properly typed so that it could be updated or
> inserted into our Sybase backend. I searched the archives and wasn't
> able to find anything that specifically talked about how to do this,
> so I wanted to share.
>
> // Initialize to Jan 1, 1901
> java.sql.Date sqlDate = new java.sql.Date(1, 0, 1);

There no reason to assign a dummy Date object to the variable when you
declare it. You can just assign it a null value:

  java.sql.Date sqlDate = null;

In this example, you never use the Date object you assign so it just
adds overhead (creating and getting rid of objects is pretty expensive;
only create the objects you really need).

> String tmpDateStr = request.getParameter("releaseDate");
>
> try {
>         // executeUpdate fails on date fields unless they are in
>         // yyyy-MM-dd format (at least on our setup. YMMV.)
>         SimpleDateFormat sdfTemplate = new SimpleDateFormat("yyyy-MM-dd");
>
>         // Going from string to java.util.Date because that is what
>         // format() expects
>         java.util.Date nptDate = new java.util.Date(tmpDateStr);
>
>         // Format returns a string, which is what we want for our next
>         // statment.
>         tmpDateStr = sdfTemplate.format(nptDate);
>
>         // Finally put it into the form needed by our
>         // preparedStatement.setDate()
>         sqlDate = java.sql.Date.valueOf(tmpDateStr);

The above three statements can be reduced to just one: sdfTemplate.parse(),
as shown in my example above.

> }
> catch (Exception ex) {
>         // Exception parsing code....
> }
> finally {
>         // Whatever
> }
>
> So the pattern is String --> java.util.Date --> String -->
> java.sql.Date. Hope this helps someone.
>
> --
>
> ===========================================================================
> 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://archives.java.sun.com/jsp-interest.html
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.jsp
>  http://www.jguru.com/faq/index.jsp
>  http://www.jspinsider.com

--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
Author of JavaServer Pages (O'Reilly), http://TheJSPBook.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://archives.java.sun.com/jsp-interest.html
 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.jsp
 http://www.jguru.com/faq/index.jsp
 http://www.jspinsider.com

Reply via email to