Link to home
Start Free TrialLog in
Avatar of EricE
EricE

asked on

sql:dateParam problem

Hi,
I am trying to take a parameter that is passed from an earlier page:

<jsp:forward page="pnRequestNewResult.jsp">
     <jsp:param name="dateAdd" value="<%= now.toString() %>"/>
</jsp:forward>

and do a query on a MySQL database on the new page:

<%
//     SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     DateFormat df = new DateFormat();
%>

<sql:query var="rs" scope="request" dataSource="${conn}">
     SELECT * FROM partnumber WHERE sNameUser = ? AND tDateRqst = ?
     <sql:param value="${param.txtNameLogin}" />
     <sql:dateParam value="<% df.parse(${request.getParameter("dateAdd")}); %>" type="timestamp" />
</sql:query>

I'm getting an error:

org.apache.jasper.JasperException: /pnRequestNewResult.jsp(20,66) equal symbol expected

which I assume means I'm not using the correct syntax to provide the sql:dateParam value.  This worked when I wasn't using WHERE in the SELECT (no parameters).

All the examples I can find just use a request parameter directly.  Need help with how to format this.

Thanks,
Eric
PS I am assuming that the position (20,66) in the error message is zero based, that is the upper left corner of the jsp page is row zero, column zero?
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Should be something like:

<sql:query var="rs" scope="request" dataSource="${conn}">
    SELECT * FROM partnumber WHERE sNameUser = ? AND tDateRqst = ?
    <sql:param value="${param.txtNameLogin}" />
    <sql:dateParam value="<%= df.parse(request.getParameter("dateAdd")) %>" type="timestamp" />
</sql:query>


Though I'm not sure what you intend with:
${conn}
${param.txtNameLogin}

> DateFormat df = new DateFormat();

That won't work either as DateFormat is an abstract class.
Use one of it's static methods, or SimpleDateFormat instead.
Avatar of EricE
EricE

ASKER

Hi,
Made the change and now have another error message:

org.apache.jasper.JasperException: Unable to compile class for JSP
An error occurred at line: 55 in the jsp file: /pnRequestNewResult.jsp
Generated servlet error:
    [javac] Compiling 1 source file
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\accuray\pnRequestNewResult_jsp.java:104: cannot resolve symbol
symbol  : method setValue (java.util.Date)
location: class org.apache.taglibs.standard.tag.el.sql.DateParamTag
            _jspx_th_sql_dateParam_0.setValue( sdf.parse(request.getParameter("dateAdd")) );
                                    ^
1 error

Here is the code, only changed to SimpleDateFormat and the <sql:dateParam statement.

<%
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//     DateFormat df = new DateFormat();
%>

<sql:query var="rs" scope="request" dataSource="${conn}">
     SELECT * FROM partnumber WHERE sNameUser = ? AND tDateRqst = ?
     <sql:param value="${param.txtNameLogin}" />
     <sql:dateParam value="<%= sdf.parse(request.getParameter("dateAdd")) %>" type="timestamp" />
</sql:query>

Is "yyyy-MM-dd HH:mm:ss" necessary when converting from a string to a Date?  I got the same error with and without it.

Thanks,
Eric
PS ${conn} was set up as the datasource in some earlier code.  param.txtNameLogin is the name of a text input field on the data entry page
Avatar of EricE

ASKER

Hi,
Need to clarify the error message since the caret might not be pointing to the right spot.  It is pointing at the period between _jspx_th_sql_dateParam_0 and setValue(sdf...).

Eric

Try:

<sql:dateParam value="<%= new java.sql.Date(sdf.parse(request.getParameter("dateAdd")).getTime()) %>" type="timestamp" />
Avatar of EricE

ASKER

Hi,
Looks like the same error message:

An error occurred at line: 55 in the jsp file: /pnRequestNewResult.jsp
Generated servlet error:
    [javac] Compiling 1 source file
C:\Program Files\Apache Group\Tomcat 4.1\work\Standalone\localhost\accuray\pnRequestNewResult_jsp.java:104: cannot resolve symbol
symbol  : method setValue (java.sql.Date)
location: class org.apache.taglibs.standard.tag.el.sql.DateParamTag
            _jspx_th_sql_dateParam_0.setValue( new java.sql.Date(sdf.parse(request.getParameter("dateAdd")).getTime()) );
                                    ^
1 error

Code is:

<sql:query var="rs" scope="request" dataSource="${conn}">
     SELECT * FROM partnumber WHERE sNameUser = ? AND tDateRqst = ?
     <sql:param value="${param.txtNameLogin}" />
     <sql:dateParam value="<%= new java.sql.Date(sdf.parse(request.getParameter("dateAdd")).getTime()) %>" type="timestamp" />
</sql:query>

I cleared out the work subdirectory and restarted Tomcat to make sure it wasn't using the cached files.

Thanks,
Eric
try this:

<fmt:parseDate value="${param.dateAdd}" var="parsedAddDate"
  pattern="yyyy-MM-dd" />

<sql:query var="rs" scope="request" dataSource="${conn}">
    SELECT * FROM partnumber WHERE sNameUser = ? AND tDateRqst = ?
    <sql:param value="${param.txtNameLogin}" />
    <sql:dateParam value="${parsedAddDate}" type="timestamp" />
</sql:query>



Avatar of EricE

ASKER

Hi,
Seem to be stuck in an infinite loop...  When I made the change you suggested I get an error:

org.apache.jasper.JasperException: In <parseDate>, value attribute can not be parsed: "Mon Mar 03 21:36:41 GMT-08:00 2003"

So I go back to the page that stuffs the parameter with:

<jsp:forward page="pnRequestNewResult.jsp">``
     <jsp:param name="dateAdd" value="${parsedAddDate}"/>
</jsp:forward>

and try to use fmt:parseDate to put the string date in a simpiler format.  I was able to do that according to:

<% out.println(sdf.format(fmtDate)); %>

but I still end up trying to put Java code into a value statement, in this case in the fmt:parseDate:

<fmt:parseDate value=" ${<% sdf.format(fmtDate)); %>} " var="parsedAddDate" pattern="yyyy-MM-dd HH:mm:ss" /> (one of my failed attempts)

so I can pass it to jsp:forward.  The fundamental problem seems to be how do you pass a variable you have defined in regular Java code into the parameter value of a taglib??

Gotta crash now, getting old...  Appreciate any futher thoughts.

Thanks,
Eric
PS I have the book and have that page open in Opera, unfortunately it doesn't get any more complex than the examples at Sun, just uses request parameters.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would suggest you to user request scope bean to pass your date, in forwarding page:

<%request.setAttribute( "dateAdd", now );%>
<jsp:forward page="pnRequestNewResult.jsp" />

or if your prefer tags:

<jsp:useBean id="dateAdd" class="java.util.Date", scope="request" />
<jsp:forward page="pnRequestNewResult.jsp" />

in pnRequestNewResult.jsp:

<sql:query var="rs" scope="request" dataSource="${conn}">
    SELECT * FROM partnumber WHERE sNameUser = ? AND tDateRqst = ?
    <sql:param value="${param.txtNameLogin}" />
    <sql:dateParam value="${dateAdd}" type="timestamp" />
</sql:query>
Avatar of EricE

ASKER

Hallelujah,
I'd swear I tried that syntax for passing in the value to the forward tag but I was flailing around pretty badly.

Thanks for the alternative Kenneth, I've copied it to comments on the page and will remember for future use.

Thanks,
Eric