日期:2014-05-18  浏览次数:20779 次

字符串转换时间问题!!
我现在要把前台接受到的字符串转换成时间格式,保存到数据库,但是控制台老是报“SQLCODE:   -180,   SQLSTATE:   22007”的错,其中22007代表:   检测到无效的日期时间格式;即指定了无效的字符串表示法或值。  
<%  
      String   Usr_Id   =request.getParameter( "C_Usr_Id ");
      String   degreeName   =request.getParameter( "CNAME ");
      String   D_Birthday=request.getParameter( "D_Birthday ");
      java.text.SimpleDateFormat   sdf   =   new   java.text.SimpleDateFormat( "yyyy-MM-dd ");
      java.util.Date   DateTime1   =   null;
      try
          {     DateTime1   =   sdf.parse(D_Birthday);}
      catch   (Exception   e)  
          {
e.printStackTrace();
          }  
          String   insertResult   =   " ";
          SJUsr   usr   =   new   SJUsr();  
          insertResult   =   usr.insertUsr(Usr_Id,degreeName,DateTime1);
%>
其中insertUsr方法如下
public   String   insertUsr(String   Usr_Id,String   degreeName,Date   D_Birthday)  
{
          String   returnValue   =   " ";
          DBscm   conndb   =   new   DBscm();
          String   sql   =   "insert   into   sys_usr   values( ' "   +   Usr_Id   +   " ', ' "   +   degreeName   +   " ', '+   D_Birthday   + ') ";
if   (conndb.transExec(sql))   {
returnValue   =   "添加成功 ";
}   else   {
returnValue   =   "添加失败 ";
}
conndb.freeConnection();
return   returnValue;
}

------解决方案--------------------
你的SQL里面的时间就直接用字符串就可以了
String sql = "insert into sys_usr values( ' " + Usr_Id + " ', ' " + degreeName + " ', '+ sdf.format(D_Birthday) + ') ";


------解决方案--------------------
你在设计数据库的时候是设成DATE型 的吗>
我的是这么写的怎么行?

Date datebegin=null;
try{
datebegin =new SimpleDateFormat( "yyyy-MM-dd ").parse(userbirth);
}
catch(ParseException pe){}

forumuser.setUserBirth(datebegin);
------解决方案--------------------
使用拼接的sql语句传递二进制数据可能会有不少问题~~,你必须先把时间字符串转换为timestamp类型,再转换回字符串,然后使用数据库的字符串转二进制的函数进行语句拼接。
如果你使用sql server,可以使用convert或cast进行转换
------解决方案--------------------
http://blog.csdn.net/dazhen520/archive/2007/05/29/1629914.aspx
------解决方案--------------------
PreparedStatement ps;

ps.setTimestamp(1,new Date());

or

"insert into table1 values(to_date( '2007-02-21 ', 'yyyy-mm-dd ')) "