我写了一个java servlet程序但是当我运行它时,它显示错误HTTP状态500 - java.util.Date无法转换为java.sql.Date我搜索了如何以任何其他方式插入日期类型数据库,但我没有可能这样做。我的代码是:
package skypark;
import java.io.*;
import javax.servlet.*;
import java.text.*;
import javax.servlet.http.*;
import java.sql.*;
import java.sql.Date;
public class Registration extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 1L;
public static Connection prepareConnection()throws ClassNotFoundException,SQLException
{
String dcn="oracle.jdbc.driver.OracleDriver";
String url="jdbc:oracle:thin:@JamesPJ-PC:1521:skypark";
String usname="system";
String pass="tiger";
Class.forName(dcn);
return DriverManager.getConnection(url,usname,pass);
}
public void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
{
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();
try
{
String phone1,dofb,date1,month,year,uname,fname,lname,address,city,state,country,pin,email,password,gender,lang,qual,relegion,privacy,hobbies,fav;
uname=req.getParameter("uname");
fname=req.getParameter("fname");
lname=req.getParameter("lname");
date1=req.getParameter("date");
month=req.getParameter("month");
year=req.getParameter("year");
address=req.getParameter("address");
city=req.getParameter("city");
state=req.getParameter("state");
country=req.getParameter("country");
pin=req.getParameter("pin");
email=req.getParameter("email");
password=req.getParameter("password");
gender=req.getParameter("gender");
phone1=req.getParameter("phone");
lang="";
qual="";
relegion="";
privacy="";
hobbies="";
fav="";
dofb=date1 +"-"+ month +"-" + year;
int phone=Integer.parseInt(phone1);
DateFormat formatter ;
java.util.Date dob ;
formatter = new SimpleDateFormat("dd-MM-yy");
dob = formatter.parse(dofb);
Connection con=prepareConnection();
String Query="Insert into regdetails values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement ps=con.prepareStatement(Query);
ps.setString(1,uname);
ps.setString(2,fname);
ps.setString(3,lname);
ps.setDate(4,(Date) dob);
ps.setString(5,address);
ps.setString(6,city);
ps.setString(7,state);
ps.setString(8,country);
ps.setString(9,pin);
ps.setString(10,lang);
ps.setString(11,qual);
ps.setString(12,relegion);
ps.setString(13,privacy);
ps.setString(14,hobbies);
ps.setString(15,fav);
ps.setString(16,gender);
int c=ps.executeUpdate();
String query="insert into passmanager values(?,?,?,?)";
PreparedStatement ps1=con.prepareStatement(query);
ps1.setString(1,uname);
ps1.setString(2,password);
ps1.setString(3,email);
ps1.setInt(4,phone);
int i=ps1.executeUpdate();
if(c==1||c==Statement.SUCCESS_NO_INFO && i==1||i==Statement.SUCCESS_NO_INFO)
{
out.println("<html><head><title>Login</title></head><body>");
out.println("<center><h2>Skypark.com</h2>");
out.println("<table border=0><tr>");
out.println("<td>UserName/E-Mail</td>");
out.println("<form action=login method=post");
out.println("<td><input type=text name=uname></td>");
out.println("</tr><tr><td>Password</td>");
out.println("<td><input type=password name=pass></td></tr></table>");
out.println("<input type=submit value=Login>");
out.println("</form></body></html>");
}
else
{
out.println("<html><head><title>Error!</title></head><body>");
out.println("<center><b>Given details are incorrect</b>");
out.println(" Please try again</center></body></html>");
RequestDispatcher rd=req.getRequestDispatcher("registration.html");
rd.include(req,resp);
return;
}
}
catch(ClassNotFoundException cnfe) {
out.println("<html><head><title>Error!</title><body>");
out.println("<b><i>Class not found "+ cnfe +"</i></b>");
out.println("</body></html>");
} catch(SQLException sqle) {
out.println("<html><head><title>Error!</title><body>");
out.println("<b><i>Unable to process try after some time Sql error</i></b>");
out.println("</body></html>");
} catch (ParseException e) {
out.println("<html><head><title>Error!</title><body>");
out.println("<b><i>Unable to process Parseint exc " + e +"</i></b>");
out.println("</body></html>");
}
/* catch(Exception e)
{
out.println("<html><head><title>Error!</title><body>");
out.println("<b><i>Unable to process try after some time</i></b>");
out.println("</body></html>");
RequestDispatcher rd=req.getRequestDispatcher("registration.html");
rd.include(req,resp);
return;
}*/
out.flush();
out.close();
}
}
错误是
HTTP Status 500 - java.util.Date cannot be cast to java.sql.Date
type Exception report
message java.util.Date cannot be cast to java.sql.Date
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.ClassCastException: java.util.Date cannot be cast to java.sql.Date
skypark.Registration.doPost(Registration.java:67)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.33logs.
请任何人帮我解决这个问题。
答案 0 :(得分:2)
您应该创建一个java.sql.Date
java.sql.Date object
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
然后使用sqlDate
ps.setDate
答案 1 :(得分:2)
PreparedStatement.setDate()
方法需要java.sql.Date
作为参数。您的java.util.Date
不是java.sql.Date
,而是在尝试将其投放到java.sql.Date
。那不行。转换不会改变对象的类型。
要从java.sql.Date
创建java.util.Date
,请使用
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());