简体   繁体   中英

How to insert DATE using jsp

I have a feedback page on my site that contains name , email and comments . Here is my code on JSP and I'm using Apache Tomcat 7.0 and Oracle DB

String query = "Insert into t_comments(name, email, comments) values('"
                                + realname
                                + "','"
                                + email
                                + "','"
                                + comments+"')";

This works great. But I decided to add DATEC column (data type DATE ) to my table t_comments . So my query should look like

String query = "Insert into t_comments(name, email, comments,datec) values('"
                                + realname
                                + "','"
                                + email
                                + "','"
                                + comments
                                + "',"
                                + "TO_DATE('"
                                + new java.util.Date()
                                + "', 'dd/mm/yyyy hh24:mi:ss'))";

And this doesn't work.

ORA-01858: a non-numeric character was found where a numeric was expected

Maybe I insert wrongly type DATE into my table. Also I have another problem. The name and comments are in Cyrillic. And when they inserted in table, they are displayed incorrect with different encoding. I have this lines in my JSP page

<%@ page language='java' contentType='text/html; charset=UTF-8' pageEncoding='UTF-8'%>

So help me please solve my two problems

  1. insert DATE to my table

  2. insert Cyrillic words correct to my table

Thanks

Let oracle do it for you instead.

String query = "Insert into t_comments(name, email, comments,datec) values('"
                                + realname
                                + "','"
                                + email
                                + "','"
                                + comments
                                + "', CURRENT_TIMESTAMP)";

See this link for more info.

You should debug your code and check if a Date object toString() matches the pattern expected by Oracle.

Potentially, if you don't want to go in Arvind's way (which I think it's a good idea actually), you can format your Date using SimpleDateFormat .

You should also consider using a PreparedStatement instead of building the statement using String concatenation.

I thank all for your answers. I'm using CURRENT_TIMESTAMP to insert DATE to my table from @Arvind Sridharan and for cyrillic characters I added the following lines in my jsp

request.setCharacterEncoding("UTF-8");
realname = new String(realname.getBytes("ISO-8859-1"),"UTF8");
comments = new String(comments.getBytes("ISO-8859-1"),"UTF8");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM