hello everyone,
I am stuck with this problem.If anyone could help me, it would be a great favour.
I am working on a leave application.
Here the leaves are been applied by employees and accordingly the leaves are been approved or cancelled.
so the problem is that when i am retrieving values such as from date and to date from another jsp i am using

[B]String[] leavefrom;
String[] until;

leavefrom=request.getParameterValues("leavefrom");
for(int counter=0; counter < leavefrom.length; counter++) 
{ 

} 

until=request.getParameterValues("until");
for(int counter=0; counter < until.length; counter++) 
{ 

}[/B]

so that i get all the values of different employees.but when i am comparing it with the database values i am facing an error such as ,
only the dates which are from 12 to 30/31 .rest all the dates from 1 to 11 are not been updated in the database.i am just giving the code where the problem exists.I know how to do the connectivity and everything is working fine except for this one.

[B]ps=con.prepareStatement("update emp_leave_application set status='"+approved[counter]+"' where approver='"+username+"' and  m_emp_no='"+userid[counter]+"' and leave_type='"+leavetype[counter]+"'and from_date=? and to_date=?");
ps.setString(1,leavefrom[counter]);
ps.setString(2,until[counter]);

ps.executeUpdate();[/B]

here i am using [B]ps.setString() [/B] wherein, i have to use [B]ps.setDate()[/B] instead to compare it with the database, but how to write it when i am using an array .It is giving me an error saying that cant convert string to date.
Pls help me

Thanks in advance.

Recommended Answers

All 9 Replies

First of all what type of argument does ps.setDate() have and what type are the to_date, from_date at your database?

Also if you need to convert a String to a Date try this:

String sDate = 01/04/2009;  // 1st of April
String format = "dd/MM/yyyy"

SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setLenient(false);

Date date = sdf.parse(sDate);

Check the API for more: SimpleDateFormat

commented: Simple and effective solution +18

First of all what type of argument does ps.setDate() have and what type are the to_date, from_date at your database?

Also if you need to convert a String to a Date try this:

String sDate = 01/04/2009;  // 1st of April
String format = "dd/MM/yyyy"

SimpleDateFormat sdf = new SimpleDateFormat(format);
sdf.setLenient(false);

Date date = sdf.parse(sDate);

Check the API for more: SimpleDateFormat

hi,
Thanks for your reply.
the code which i have written is given above in the previous post
i.e

ps.setString(1,leavefrom[counter]);
     ps.setString(2,until[counter]);

leavefrom and until are in String type.
and i am comparing this with the database which is in

Date type 
i.e to_date, from_date

and because of this i am facing error while updating i.e some of them are updated and some are not. so i want to use ps.setDate instead of ps.setString to compare it with the database and update all the values properly and i dont know what arguments i should pass in ps.setDate().because i tried using

ps.setDate(1,leavefrom[counter]);
     ps.setDate(2,until[counter]);

to compare all the values but getting error because leavefrom and until are in string type.Pls have a look at the whole code which i have sent previously.
If there is a problem to understand the query then pls let me know.I will clarify it.It is very urgent and important for me.
Pls find a solution.
Thanks in advance.

Hello,

Just remember that SimpleDateFormat.parse(String source) returns java.util.Date

and PreparedStatement.setDate(int i, Date d) takes java.sql.Date .
So, while passing the date parameter here, casting is required.

2nd thing keep in mind that while converting a String to Date - take care of format according to your database. Some may take as mm/dd/yyyy or dd/mm/yyyy. So, accordingly pass the parameter to SimpleDateFormat constructor.

Hope it solves your problem. And do let me know.

2nd thing keep in mind that while converting a String to Date - take care of format according to your database. Some may take as mm/dd/yyyy or dd/mm/yyyy. So, accordingly pass the parameter to SimpleDateFormat constructor.

Hope it solves your problem. And do let me know.

If the column in the DB is declared to be type Date, the format is irrelevant. Because the column will contain Date not a Varchar. The data int the column doesn't have a format nor it needs one because it is type data. You can put data inside it in any way you want as long it is type Date.

Check for these sql functions:
to_date(), to_char()

If the column in the DB is declared to be type Date, the format is irrelevant. Because the column will contain Date not a Varchar. The data int the column doesn't have a format nor it needs one because it is type data. You can put data inside it in any way you want as long it is type Date.

Check for these sql functions:
to_date(), to_char()

According to me, MS-Access database require format before entering date field for more accuracy. Correct me, if I am wrong. This forum helps me in learning.
Thanks

hi,
Thanks for the reply.
I do understand that SimpleDateFormat.parse(String source) returns java.util.Date

and PreparedStatement.setDate(int i, Date d) takes java.sql.Date .
but i am using arrays in which a counter is set , so will that make any difference because the error that i am recieving is

Error: 500
Internal Servlet Error:

org.apache.jasper.JasperException: Unable to compile class for JSPC:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:191: Incompatible type for method. Can't convert java.lang.String to java.sql.Date.
ps.setDate(1,leavefrom[counter]);
^
C:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:192: Incompatible type for method. Can't convert java.lang.String to java.sql.Date.
ps.setDate(2,until[counter]);
^
2 errors

at org.apache.jasper.compiler.Compiler.compile(Compiler.java:248)
at org.apache.jasper.runtime.JspLoader$2.run(JspLoader.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.jasper.runtime.JspLoader.loadJSP(JspLoader.java:270)
at org.apache.jasper.runtime.JspServlet$JspServletWrapper.loadIfNecessary(JspServlet.java:137)
at org.apache.jasper.runtime.JspServlet$JspServletWrapper.service(JspServlet.java:148)
at org.apache.jasper.runtime.JspServlet.serviceJspFile(JspServlet.java:247)
at org.apache.jasper.runtime.JspServlet.service(JspServlet.java:352)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:865)
at org.apache.tomcat.core.ServiceInvocationHandler.method(ServletWrapper.java:626)
at org.apache.tomcat.core.ServletWrapper.handleInvocation(ServletWrapper.java:534)
at org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:378)
at org.apache.tomcat.core.Context.handleRequest(Context.java:644)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:440)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:144)
at org.apache.tomcat.service.TcpConnectionThread.run(TcpEndpoint.java:310)
at java.lang.Thread.run(Thread.java:484)

when i am using the code to convert string to sql.Date

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date d = df.parse(leavefrom);
String dfrom=df.format(d);

java.sql.Date dd = new java.sql.Date(d.getTime());

Date dt = df.parse(until);
String dto=df.format(dt);

java.sql.Date ddt = new java.sql.Date(dt.getTime());

I am facing the error given below because of the array so how should i code to convert it into sql.Date when the string[] is to be converted to sql.Date.Pls suggest me.
Thanks in advance.

Error: 500
Internal Servlet Error:

org.apache.jasper.JasperException: Unable to compile class for JSPC:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:184: Incompatible type for method. Can't convert java.lang.String[] to java.lang.String.
Date d = df.parse(leavefrom);
^
C:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:189: Incompatible type for method. Can't convert java.lang.String[] to java.lang.String.
Date dt = df.parse(until);
^
C:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:201: [] can only be applied to arrays. It can't be applied to java.sql.Date.
ps.setDate(1,dd[counter]);
^
C:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:202: [] can only be applied to arrays. It can't be applied to java.sql.Date.
ps.setDate(2,ddt[counter]);
^
4 errors

at org.apache.jasper.compiler.Compiler.compile(Compiler.java:248)
at org.apache.jasper.runtime.JspLoader$2.run(JspLoader.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.jasper.runtime.JspLoader.loadJSP(JspLoader.java:270)
at org.apache.jasper.runtime.JspServlet$JspServletWrapper.loadIfNecessary(JspServlet.java:137)
at org.apache.jasper.runtime.JspServlet$JspServletWrapper.service(JspServlet.java:148)
at org.apache.jasper.runtime.JspServlet.serviceJspFile(JspServlet.java:247)
at org.apache.jasper.runtime.JspServlet.service(JspServlet.java:352)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:865)
at org.apache.tomcat.core.ServiceInvocationHandler.method(ServletWrapper.java:626)
at org.apache.tomcat.core.ServletWrapper.handleInvocation(ServletWrapper.java:534)
at org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java:378)
at org.apache.tomcat.core.Context.handleRequest(Context.java:644)
at org.apache.tomcat.core.ContextManager.service(ContextManager.java:440)
at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:144)
at org.apache.tomcat.service.TcpConnectionThread.run(TcpEndpoint.java:310)
at java.lang.Thread.run(Thread.java:484)

when i am using the code to convert string to sql.Date

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date d = df.parse(leavefrom);
String dfrom=df.format(d);

java.sql.Date dd = new java.sql.Date(d.getTime());

Date dt = df.parse(until);
String dto=df.format(dt);

java.sql.Date ddt = new java.sql.Date(dt.getTime());

I am facing the error given below because of the array so how should i code to convert it into sql.Date when the string[] is to be converted to sql.Date.Pls suggest me.
Thanks in advance.

Error: 500
Internal Servlet Error:

org.apache.jasper.JasperException: Unable to compile class for JSPC:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:184: Incompatible type for method. Can't convert java.lang.String[] to java.lang.String.
Date d = df.parse(leavefrom);
^
C:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:189: Incompatible type for method. Can't convert java.lang.String[] to java.lang.String.
Date dt = df.parse(until);
^
C:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:201: [] can only be applied to arrays. It can't be applied to java.sql.Date.
ps.setDate(1,dd[counter]);
^
C:\j2sdkee1.2.1\repository\shiju-f70efc8fa\web\_0005cupdate_0002ejspupdate_jsp_11.java:202: [] can only be applied to arrays. It can't be applied to java.sql.Date.
ps.setDate(2,ddt[counter]);
^
4 errors

These errors help to understand the problem -

For 1st Error - use Date d = df.parse(leavefrom[counter]);
For 2nd error - use Date dt = df.parse(until[counter]);

and then in last 2 errors why are you using ps.setDate(1,dd[counter])
instead it should be ps.setDate(1,dd);

Thanks for the reply deepalihanand,
I am very thankful to you that you have solved my query.I wont ever forget your help.
Thanks once again.
Bye take care.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.