File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

to enter Date in Oracle from Servlets

 
priyanshu bhardwaj
Greenhorn
Posts: 20
  • 0
  • 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);
}




}
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the JDBC forum.
 
Paul Clapham
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The bit you are missing is something like this:
 
Babar Qadri
Ranch Hand
Posts: 51
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks alot many my problem is resolved
bye
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic