-1

I am trying to write a code in Servlet for DOB and DOJ using Netbeans, MySQL and Tomcat.

Issue is when I am saving the date it goes into MySQL table but date automatically changes. I am using 3 drop down box for - dd mm yyyy and suppose I select 24 12 1973 and save this but the date has been changed by MySQl table like - 1974-01-24. Here what I have done (may be wrong)

From servlet1.java

First fetching the date from Date type DOB column from MySQL table

java.sql.Date Dob=rno.getDate(5);
String Dob1=Dob.toString();

Then extracting the day month and year in array

char number;
int  a1[];
a1=new int[3];
a1[0]=0;
a1[1]=0;
a1[2]=0;
for( j=0;j<Dob1.length();j++)
{
   number=Dob1.charAt(j);
   if(Character.isDigit(number))
   {
      a1[i]=(a1[i]*10)+Integer.parseInt(new Character(number).toString());
   }
   else
   {
      i++;
   }    
}

Display the extracted value in drop down with options for modification if needed a1[2] for day

pw.println("<th>&nbsp;&nbsp;&nbsp;Date Of Birth&nbsp;</th>");
pw.println("<td>&nbsp&nbsp;&nbsp;&nbsp<select name=day1><option value="+a1[2]+" >"+a1[2]+ "");
pw.println("<option value=1>1</option><option value=2>2</option><option value=3>3</option>"
   + "<option value=4>4</option><option value=5>5</option><option value=6>6</option>"
   + "<option value=7>7</option><option value=8>8</option><option value=9>9</option>"....
pw.println("</select>");

a1[1] for month

pw.println("<select name=month1><option value="+a1[1]+" >"+a1[1]+ "<option   value=1>1</option>");
pw.println("<option value=2>2</option><option value=3>3</option><option value=4>4</option>"
   + "<option value=5>5</option><option value=6>6</option><option value=7>7</option>"
   + "<option value=8>8</option><option value=9>9</option><option value=10>10</option>"
   + "<option value=11>11</option><option value=12>12</option> "
   + "</select>");

a1[0] for year

pw.println("<select name=year1><option value="+a1[0]+" >"+a1[0]);
pw.println("<option value=77>1977</option><option value=78>1978</option><option value=79>1979</option><option value=80>1980</option>"+"<option value=81>1981</option><option value=82>1982</option><option value=83>1983</option><option value=84>1984</option>"
 +"<option value=85>1985</option><option value=86>1986</option><option value=87>1987</option><option value=88>1988</option>"
 +"<option value=89>1989</option><option value=90>1990</option><option value=91>1991</option><option value=92>1992</option>"
 +"<option value=93>1993</option><option value=94>1994</option><option value=95>1995</option><option value=96>1996</option>"
 +"<option value=97>1997</option><option value=98>1998</option><option value=99>1999</option><option value=100>2000</option>"
 +"<option value=101>2001</option><option value=102>2002</option><option value=103>2003</option><option value=104>2004</option><option value=105>2005</option>");
pw.println("</select>(dd/mm/yyyy)</td></tr>");

After modification posting the data to servlet2.java for storing in table

int day1=Integer.parseInt(req.getParameter("day1"));
int month1=Integer.parseInt(req.getParameter("month1"));
int year1=Integer.parseInt(req.getParameter("year1"));
Date DOB= new Date(year1,month1,day1);
Class.forName("com.mysql.jdbc.Driver");
Connection c =   DriverManager.getConnection("jdbc:mysql://localhost:3306/xe","root", "");  
PreparedStatement ps=c.prepareStatement("Update Employee Dob=? where   Emp_id=?");
ps.setDate(5,DOB);
int j=ps.executeUpdate();

Now I lost hope after fighting with this code since last 3 to 4 hours but can't figure out why date is changing. If any one guide me then I will know my mistake.

Anoop LL
  • 1,548
  • 2
  • 21
  • 32
Amit Roy
  • 33
  • 5
  • Month is zero indexed in Java. January = 0; December = 11. Set month to selected value -1. – Alan Hay Feb 22 '16 at 10:33
  • 2
    man you should use simpleDateFormat to hugely reduce the quantity of your code – achabahe Feb 22 '16 at 10:38
  • 1
    Why don't you try a [Datepicker](https://jqueryui.com/datepicker/#dropdown-month-year) to display and change the date. It will accept the `string` format of date and can return you in `string` format again. – Vinoth Krishnan Feb 22 '16 at 10:39
  • can you show us what the data base stores like an example – achabahe Feb 22 '16 at 10:48
  • @VinothKrishnan Thank you Sir, Can you provide me some link from where I can leran how to use Datepicker in servlet or if possible give me code hint. – Amit Roy Feb 22 '16 at 10:55
  • @AlanHay And what about year? – Amit Roy Feb 22 '16 at 10:56
  • @achabahe I have already mentioned in question that if I select 24 12 1973 Database store 1974-01-24. And structure is simple DOB DATE type. – Amit Roy Feb 22 '16 at 10:59
  • Voting to close. Solution has been clearly identified in the first comment and now seems to be a general discussion about how it can be done in JSP, using JQuery.... – Alan Hay Feb 22 '16 at 12:11

2 Answers2

0

In your servlet,

String selectedDate = req.getParameter("pickedDate");

Finally you need to modify your String date to Java date format,

public Date convertStringToDate(String selectedDate) {
    SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy");
    Date date = null;
    try {
        date = formatter.parse(dateVal);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return date;
}
Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
  • Sir this is problem I have servlet pages servlet1.java and servlet2.java from servlet1.java I have to select the date and post the values to servlet3.java for storing in database. That why I am asking is there any knowledge base for using jquery in servlet. – Amit Roy Feb 22 '16 at 11:15
  • Means I need that html page code in my servlet1.java then almost the job is done. Also I need to display the fetched date from table to the datepicker control before modifying the DOB – Amit Roy Feb 22 '16 at 11:17
  • Okay, you've tagged `JSP`. So I thought you're using jsp too. – Vinoth Krishnan Feb 22 '16 at 11:18
  • No Sir its pure servlet and I am learning servlet. Is it possible in servlet? – Amit Roy Feb 22 '16 at 11:19
  • Instead of three drop down's how about adding one text box for get the date value? `` So you can directly get the values without datepicker. – Vinoth Krishnan Feb 22 '16 at 11:23
0

the problem is in the Date class , the year section start from 1990 so to have year 2000 you should put year 2000-1990 and the month starts from 0 not 1, so long story short is leave the calss java.sql.Date constructor alone and use simpleDateFormat like this

SimpleDateFormat format= new SimpleDateFormat("y-M-d");
java.util.Date date =format.parse(year1+"-"+month1+"-"+day1);

and you are set to go

Edit

the prepared statement needs an java.sql.Date in its Date argument you can try this code i think i will work

    java.sql.Date date = java.sql.Date.valueOf(year1+"-"+month1+"-"+day1);
    statement.setDate(1, date);//not setDate(5,date);
achabahe
  • 2,445
  • 1
  • 12
  • 21
  • Sir How can I insert the date in MySQL prepared statement showing error. I have done as you suggested `int bday1=Integer.parseInt(req.getParameter("bday"));int bmonth1 = Integer.parseInt( req.getParameter("bmonth")); `int byear1=Integer.parseInt(req.getParameter("byear")); `SimpleDateFormat format= new SimpleDateFormat("y-M-d"); `java.util.Date date1 =format.parse(byear1+"-"+bmonth1+"-"+bday1); `String DOB_date1=date1.toString(); `ps.setDate(5,DOB_date1); – Amit Roy Feb 22 '16 at 13:51
  • @AmitRoy i will change my answer try this new answer – achabahe Feb 22 '16 at 14:32
  • Its same Sir no change – Amit Roy Feb 22 '16 at 14:38
  • is it the same error as before .it makes dates change ?? – achabahe Feb 22 '16 at 14:39
  • i have tested it and it didn't change date – achabahe Feb 22 '16 at 14:40
  • can you tell what happens in your side – achabahe Feb 22 '16 at 14:46