0

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

olgacosta
  • 1,076
  • 4
  • 15
  • 36

3 Answers3

1

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.

Arvind Sridharan
  • 3,885
  • 4
  • 29
  • 54
  • it is not exactly what I wanted. Why it doesn't work using java.util.Date()? But for me, there is no matter how to insert DATE, so I will try your offer. Thanks! – olgacosta Oct 17 '14 at 08:57
1

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.

Leo
  • 6,480
  • 4
  • 37
  • 52
0

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");
olgacosta
  • 1,076
  • 4
  • 15
  • 36