AndriesKeun
asked on
Date Manipulation
Hi there
Question 1)
For some reason when i am inputting data it wont let me leave a date field blank, or null. But i need to be able to do this. e.g. client walks in and we register them on our system, but one field is paid date and obviously i only want to enter that once they have actually paid. If it is default to present date then obviously it will look like they paid, or if i default it to 00-00-0000 when i click on the calendar i have a shite load of clicking to do to get to the current date. How do i allow it to stay blank?
P.S. Its not a primary key and is not set to 'not null' either.
Question 2)
See code below. What I want to do is after each block of dates i want to leave an open line satting the day of the dates to follow, e.g.
The output is Like this at the moment:
01-01-2006 ....
01-01-2006 ...
02-01-2006 ... etc.
I want:
Mon
01-01-2006 ...
01-01-2006 ...
Tue
02-01-2006 ... etc
Any Ideas?
try {
Driver DriverRecordset = (Driver)Class.forName(_dri ver).newIn stance();
con = DriverManager.getConnectio n(_url, _uid,_pwd);
stmt = con.createStatement();
rs = stmt.executeQuery("select * from bookings where embassy='French' order by consulatedate asc");
while (rs.next()){
c++;
%>
<TR>
<Td bgcolor="#ffffff" width=80><%=rs.getString(" consulated ate")%></t d>
<Td bgcolor="#ffffff"><a href="viewclient.jsp?id=<% =rs.getStr ing("id")% >"> <%=rs.getString("name")%> <%=rs.getString("surname") %> </a> </td>
<Td bgcolor="#ffffff" width=70><%=rs.getString(" tel")%></t d>
<Td bgcolor="#ffffff" width=70><%=rs.getString(" mobile")%> </td>
<Td bgcolor="#ffffff"><%=rs.ge tString("e mail")%></ td>
<Td bgcolor="#ffffff" width=60><%=rs.getString(" traveldate ")%></td>
<Td bgcolor="#ffffff" width=60><%=rs.getString(" documents" )%></td
</tr>
<%
}
con.close();
}
catch (SQLException sqle) {
out.println("SQLException" + sqle.toString());
}
Question 1)
For some reason when i am inputting data it wont let me leave a date field blank, or null. But i need to be able to do this. e.g. client walks in and we register them on our system, but one field is paid date and obviously i only want to enter that once they have actually paid. If it is default to present date then obviously it will look like they paid, or if i default it to 00-00-0000 when i click on the calendar i have a shite load of clicking to do to get to the current date. How do i allow it to stay blank?
P.S. Its not a primary key and is not set to 'not null' either.
Question 2)
See code below. What I want to do is after each block of dates i want to leave an open line satting the day of the dates to follow, e.g.
The output is Like this at the moment:
01-01-2006 ....
01-01-2006 ...
02-01-2006 ... etc.
I want:
Mon
01-01-2006 ...
01-01-2006 ...
Tue
02-01-2006 ... etc
Any Ideas?
try {
Driver DriverRecordset = (Driver)Class.forName(_dri
con = DriverManager.getConnectio
stmt = con.createStatement();
rs = stmt.executeQuery("select * from bookings where embassy='French' order by consulatedate asc");
while (rs.next()){
c++;
%>
<TR>
<Td bgcolor="#ffffff" width=80><%=rs.getString("
<Td bgcolor="#ffffff"><a href="viewclient.jsp?id=<%
<Td bgcolor="#ffffff" width=70><%=rs.getString("
<Td bgcolor="#ffffff" width=70><%=rs.getString("
<Td bgcolor="#ffffff"><%=rs.ge
<Td bgcolor="#ffffff" width=60><%=rs.getString("
<Td bgcolor="#ffffff" width=60><%=rs.getString("
</tr>
<%
}
con.close();
}
catch (SQLException sqle) {
out.println("SQLException"
}
1) Have you tried replacing the nulls with "" - if the field is blank. it might appear at the procesing formas null, sho try checking for this and replacing with "" instead.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
1) This is the error I am getting when i try to save without entering a date:
>>SQLExceptionjava.sql.SQL Exception: Incorrect date value: '' for column 'traveldate' at row 1
Its a field in my db set as a Date with no default value. Any other ideas?
2) kuldeep, yours works but was not perfect, had to change the syntax a bit and if (tempdate.equals(rs.getStr ing("consu latedate") ) is actually !if (tempdate.equals(rs.getStr ing("consu latedate") )
Thanks anyway though
>>SQLExceptionjava.sql.SQL
Its a field in my db set as a Date with no default value. Any other ideas?
2) kuldeep, yours works but was not perfect, had to change the syntax a bit and if (tempdate.equals(rs.getStr
Thanks anyway though
if you arent providing a default value in your table definition, then you'll have to explicitly provide it in your SQL staement.
The way you have it just now, your trying to write "" into the table, which isnt a valid date (and so your getting the error). Your options are to either to alter the table to use a default value, specify a default value in your sql, or allow the date field to accept null's.
The way you have it just now, your trying to write "" into the table, which isnt a valid date (and so your getting the error). Your options are to either to alter the table to use a default value, specify a default value in your sql, or allow the date field to accept null's.
ASKER
thats whats so bloody confusing. my db is set to allow null on all the fields except the primary key (id) so i just cant understand why it wont allow me to use null for the dates. all the other fields are happy with being null, except the bloody dates. IT really annoys me sometimes, sometimes things just dont work when they should.
Instead of trying to insert an empty string, try not mentioning it in your sql statement
It may be database specific.. what database you are using? I am not sure but this information might help
ASKER
A SQL database, and I access it using SQLyog and NAVICAT
Have yuou tried removing the empty strng? This might help, as an empty string != null, so when you are inputting an empty string you are attempting to input an invalid date (as opposed to null)
ASKER
Basically i have a form on my application with all the client details, including a few dates. The user inputs all the details, Name, Number, Travel date etc. If they dont input a date then the input box is left blank.
I use the following code to INSERT the new client:
PreparedStatement stmt2 = con.prepareStatement("INSE RT INTO bookings (name, tel, mobile, email, traveldate, consulatedate, paiddate, notes, documents, surname, embassy, status) values(?,?,?,?,?,?,?,?,?,? ,?,?)" );
stmt2.setString(1, request.getParameter("cfna me") );
stmt2.setString(2, request.getParameter("tel" ) );
stmt2.setString(3, request.getParameter("mobi le") );
stmt2.setString(4, request.getParameter("emai l") );
stmt2.setString(5, request.getParameter("trav eldate") );
stmt2.setString(6, request.getParameter("cons ulatedate" ) );
stmt2.setString(7, request.getParameter("paid date") );
stmt2.setString(8, request.getParameter("note s") );
stmt2.setString(9, request.getParameter("docu ments") );
stmt2.setString(10, request.getParameter("clna me") );
stmt2.setString(11, request.getParameter("emba ssy") );
stmt2.setString(12, request.getParameter("stat us") );
stmt2.executeUpdate();
So if one of the dates are left blank in the form, then surely it should go to the db as NULL?
I use the following code to INSERT the new client:
PreparedStatement stmt2 = con.prepareStatement("INSE
stmt2.setString(1, request.getParameter("cfna
stmt2.setString(2, request.getParameter("tel"
stmt2.setString(3, request.getParameter("mobi
stmt2.setString(4, request.getParameter("emai
stmt2.setString(5, request.getParameter("trav
stmt2.setString(6, request.getParameter("cons
stmt2.setString(7, request.getParameter("paid
stmt2.setString(8, request.getParameter("note
stmt2.setString(9, request.getParameter("docu
stmt2.setString(10, request.getParameter("clna
stmt2.setString(11, request.getParameter("emba
stmt2.setString(12, request.getParameter("stat
stmt2.executeUpdate();
So if one of the dates are left blank in the form, then surely it should go to the db as NULL?
Use setDate instead of setString!!!
ASKER
get this error with setDate:
An error occurred at line: 22 in the jsp file: /updateclient.jsp
Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhos t\travelvi sas\org\ap ache\jsp\u pdateclien t_jsp.java :105: cannot find symbol
symbol : method setDate(int,java.lang.Stri ng)
location: interface java.sql.PreparedStatement
stmt2.setDate(5, request.getParameter("trav eldate") );
An error occurred at line: 22 in the jsp file: /updateclient.jsp
Generated servlet error:
C:\Program Files\Apache Software Foundation\Tomcat 5.0\work\Catalina\localhos
symbol : method setDate(int,java.lang.Stri
location: interface java.sql.PreparedStatement
stmt2.setDate(5, request.getParameter("trav
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try
if(request.getParameter("t raveldate" ).trin()== "")
{
java.sql.Date d=null;
stmt2.setDate(5, d );
}
else
{
java.sql.Date d = new date(java.util.Date.parse( request.ge tParameter ("travelda te"));
stmt2.setDate(5, d );
}
if(request.getParameter("t
{
java.sql.Date d=null;
stmt2.setDate(5, d );
}
else
{
java.sql.Date d = new date(java.util.Date.parse(
stmt2.setDate(5, d );
}
ASKER
now getting this error: SQLExceptionjava.lang.Ille galArgumen tException
new code:
stmt2.setString(4, request.getParameter("emai l") );
if(request.getParameter("t raveldate" ).trim()== "") {
java.sql.Date d = null;
stmt2.setDate(5, d );
}
else
{
java.sql.Date d = new Date(java.util.Date.parse( request.ge tParameter ("travelda te")));
stmt2.setDate(5, d );
}
//stmt2.setString(5, request.getParameter("trav eldate") );
stmt2.setString(6, request.getParameter("cons ulatedate" ) )
new code:
stmt2.setString(4, request.getParameter("emai
if(request.getParameter("t
java.sql.Date d = null;
stmt2.setDate(5, d );
}
else
{
java.sql.Date d = new Date(java.util.Date.parse(
stmt2.setDate(5, d );
}
//stmt2.setString(5, request.getParameter("trav
stmt2.setString(6, request.getParameter("cons
ASKER
Ok, Step by step, let me know if you see any errors:
This is my input form:
<tr><td class='menutable' >Travel Date:</td> <td><input name=traveldate value='' style='width:100' onfocus='popUpCalendar(thi s, this, "yyyy-mm-dd");'></td></tr>
I have tried without the value=''. This is just a normal input box with no string.
I insert it with:
stmt2.setString(5, request.getParameter("trav eldate") );
Now this works when there is a date in the input box, but not when it isnt, which is confusing me.
The database is set up with traveldate as a date field, that allows null and has a default as null.
This is my input form:
<tr><td class='menutable' >Travel Date:</td> <td><input name=traveldate value='' style='width:100' onfocus='popUpCalendar(thi
I have tried without the value=''. This is just a normal input box with no string.
I insert it with:
stmt2.setString(5, request.getParameter("trav
Now this works when there is a date in the input box, but not when it isnt, which is confusing me.
The database is set up with traveldate as a date field, that allows null and has a default as null.
Before you try and put it in the DB, try and output the value to see exactly what you are getting. You mihgt be assuming that it is an empty string, but the javascript you're using might be using something else (whioch would break your code as you descrttiobe):
request.println("value:" + request.getParameter("trav eldate") );
request.println("value:" + request.getParameter("trav
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thx kuldeep, eventually sorted it by adapting your code a bit:
String temp = "";
if (temp.equals(request.getPa rameter("t raveldate" ))) {
stmt2.setNull(5, java.sql.Types.DATE );
}
else {
stmt2.setString(5, request.getParameter("trav eldate") );
}
Suppose i shouldnt be using setString in the second part, but it works perfectly. In other parts of the program i use getDate to display the date and it works perfectly, so I am happy.
String temp = "";
if (temp.equals(request.getPa
stmt2.setNull(5, java.sql.Types.DATE );
}
else {
stmt2.setString(5, request.getParameter("trav
}
Suppose i shouldnt be using setString in the second part, but it works perfectly. In other parts of the program i use getDate to display the date and it works perfectly, so I am happy.
ASKER
gave most points to kuldeep cause he came up with both solutions.
gave colr some points too as he was on the right track most of the time too.
Thx guys
gave colr some points too as he was on the right track most of the time too.
Thx guys
Glad you got it sorted ;-)
Glad to be of some help...:-)