Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

to enter Date in Oracle from Servlets

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i want to code so that the date should be entered in the database when entered in a textbox at the client side like 12/3/05 then how should I do it
if I tke request.getParameter("from date");
then in pst.setDate(fieldnumber,from date);
how should i code this
I m also giving the code
Please help


import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class schemeservlet extends HttpServlet
{
String fromdate1String schcost1;

Connection con=null;
PreparedStatement pst=null;
Statement stmt=null;
ResultSet rs=null;


public void init(ServletConfig config) throws ServletException
{
super.init(config);

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc ata");

}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}

public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
doPost(req,res);
}

public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
int j;
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
HttpSession s=req.getSession(true);
String str = req.getQueryString();

try
{

int add=str.lastIndexOf("ad.x");





if(add>0)
{

schfromdate1=req.getParameter("schfromdate");
schtodate1=req.getParameter("schtodate");
doinsert(schno1,prodno1,schname1,schfromdate1,schtodate1,schdetail1,schcost1,schbenefit1,invvalue1,schregion1,schcategory1,schapplied1,schbenqty1,schper1,schtype1,pw);
res.sendRedirect("http://localhost:8080/examples/schemeR.jsp";);
{
public void doinsert(String fromdate,{
System.out.println("insert");
String sqlint="insert into Scheme" +
sch_from_date)"
+ "values(?)";

try {
pst=con.prepareStatement(sqlint);
}
catch(SQLException e)
{
System.out.println(e);
}


int i=0;

try
{
System.out.println("inside insert executed");
pst=con.prepareStatement(sqlint);
pst.setString(4,schfromdate);
System.out.println("after insert executed");
i=pst.executeUpdate(sqlint);

catch(Exception e)
{
System.out.println(e);
}




}
 
Sheriff
Posts: 67389
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the JDBC forum.
 
Marshal
Posts: 25479
67
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bit you are missing is something like this:
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this
, i made some additions in your code


try
{

//added line
SimpleDateFormat sdf = new SimpleDateFormat("MM.dd.yyyy");

int add=str.lastIndexOf("ad.x");





if(add>0)
{

schfromdate1=req.getParameter("schfromdate");
schtodate1=req.getParameter("schtodate");
doinsert(schno1,prodno1,schname1,schfromdate1,schtodate1,schdetail1,schcost1,
schbenefit1,invvalue1,schregion1,schcategory1,schapplied1,
schbenqty1,schper1,schtype1,pw);
res.sendRedirect("http://localhost:8080/examples/schemeR.jsp";);
{
public void doinsert(String fromdate, {
System.out.println("insert");
String sqlint="insert into Scheme" +
"(sch_from_date)"
+ "values(?)";

try {
pst=con.prepareStatement(sqlint);
}
catch(SQLException e)
{
System.out.println(e);
}


int i=0;

try
{
System.out.println("inside insert executed");
pst=con.prepareStatement(sqlint);

////added line
pst.setDate(1,new java.sql.Date( sdf.parse(schfromdate()).getTime()));


System.out.println("after insert executed");
i=pst.executeUpdate(sqlint);

catch(Exception e)
{
System.out.println(e);
}




}
[ December 08, 2005: Message edited by: Babar Qadri ]
 
priyanshu bhardwaj
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot many my problem is resolved
bye
 
So you made a portal in time and started grabbing people. This tiny ad thinks that's rude:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic