• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date Foramt via a jsp ppage through a servlet to Mysql Database

 
Priya Prasad
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I am trying to insert date from Jsp via servlet to Mysql.
But I have an issue over here.
The calendar of html shows the date in the format dd-mm-yyyy.

the code is


Now in my servlet I tried to change the format of date accepted by Mysql by running a query .
My servlet is as follows:-


But the values are not getting inserted in Mysql database. Neither it's shows any error. I am clueless. Help!
 
Dan Arbo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Priya,

could you please clarify - is the record inserted with the date field left null, or is the record is not inserted at all?

If there is no record inserted at all, are you sure that at least one of the if statements is executed?
I guess you have already checked because of the commented System.out.println () statements, but just making sure

Also, generally, it is safer to use PreparedStatements instead of Statements, to avoid SQL injections. The syntax is almost identical so it doesn't involve a lot of reading. And I read ( never tested myself ) that it is also better performing. This is just in case you weren't aware of it, maybe worth taking a look and then choose which one you prefer. If this was already an informed choice, apologies for this extra couple of lines
 
Priya Prasad
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hy..


The problem is that not even the other values are getting inserted now. And the SOP statement is not working. If i remove the calendar from my jsp and insert it as yyyy-mm-dd format then all the values are getting inside the database. Also all my condition are working fine without the Calendar issue.
 
Dan Arbo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok...

I can't work it out yet, so I will have to ask a few more questions I am afraid!

Have you tried adding System.out.println ( e ) in the catch statement?

Also, when you debugged it, did it skip any breakpoints?

Last question, for now at least, what datatype is the field in the database?
 
Priya Prasad
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The control is passing to the finally block so printing the error message doesn't help.

And the datatype of the Approval_Date field of database is Date.

Aain the control is not also passing to my normal query.
 
Dan Arbo
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just seen your private message, I have replied to it
 
bharat salunkhe
Greenhorn
Posts: 25
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JUST CONVERT YOUR DATE IN YY MM DD FORMAT WHILE YOU INSERTING
 
bharat salunkhe
Greenhorn
Posts: 25
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is your formatting code...in YYYY MM DD


and just edit your query



its working fine !
 
sunaina agarwal
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello am doing a similar website which takes date from the from and updates it on mysql. i have used this code below for date format but its showing The constructor Date(String) is undefined.... change type of dob to long.. Please help me resolving this.


Approval_Date=request.getParameter("Approval_Date");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String lastmod = format.format(new Date(Approval_Date));


here is my full servlet code

package com;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class RegisterServlet
* @param <Episode>
*/
@WebServlet("/RegisterServlet")
public class RegisterServlet<Episode> extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public RegisterServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Register");



String fn=request.getParameter("cust_fname");
String ln=request.getParameter("cust_lname");
String cat=request.getParameter("category");
String dob = request.getParameter("Date_of_Birth");
String a=request.getParameter("cust_age");
String g=request.getParameter("cust_gen");
String ad=request.getParameter("cust_addr");
String ct=request.getParameter("cust_ctry");
String s=request.getParameter("cust_state");
String c=request.getParameter("cust_city");
String e=request.getParameter("cust_e_mail");
String con=request.getParameter("cust_phone");


try{

Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");

SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy");
String lastmod = format.format (new Date(dob));

PreparedStatement ps=conn.prepareStatement(
"insert into register values(?,?,?,?,?,?,?,?,?,?,?,?)");

ps.setString(1,fn);
ps.setString(2,ln);
ps.setString(3,cat);
ps.setString(4,dob);
ps.setString(5,a);
ps.setString(6,g);
ps.setString(7,ad);
ps.setString(8,ct);
ps.setString(9,s);
ps.setString(10,c);
ps.setString(11,e);
ps.setString(12,con);




int i=ps.executeUpdate();
if(i>0){
out.println("Thank you for registering with us!!!");

ps.close();
}
}
catch (SQLException e2) {e2.printStackTrace();}

catch (ClassNotFoundException e3) {e3.printStackTrace();}

out.close();
}

}


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic