0

I managed to connect to the Oracle database for express edition. I created a table :

CREATE TABLE out_patient 
( 
     patient_id    NUMBER(8) PRIMARY KEY, 
     first_name    VARCHAR2(20), 
     last_name     VARCHAR2(20), 
     gender        VARCHAR2(10), 
     mobile_number NUMBER(12), 
     address       VARCHAR2(20), 
     date_of_birth DATE, 
     date_of_entry DATE 
); 

Now I have a form to fill up all these details but, I'm getting an sqlexception:

ORA-01861:literals does not match format string

Can you tell what's causing it. Am I entering data in wrong format? I guess it might be due to 2 dates Date_Of_Birth and Date_Of_Entry. In my JDBC code I'm doing this:

String t1=text_dob.getText(); 
String t2=text_doe.getText(); 

and then,

st.setStrin(7,t1); 
st.setString(8,t2); 

Please let me know what I'm doing wrong.

Tiny
  • 27,221
  • 105
  • 339
  • 599
user3367768
  • 17
  • 1
  • 6

2 Answers2

0

You need to call setDate() instead of setString on PreparedStatement:

st.setDate(7, new java.sql.Date(System.currentTimeMillis())); 
st.setDate(8, new java.sql.Date(System.currentTimeMillis()));
st.setTimestamp(1, new Timestamp(System.currentTimeMillis()));

You problem happens because your DDL column is set to Date so Oracle expects Date value but you are sending VARCHAR value.

Timestamp: see types mapping

java.sql.Date for SQL DATE information. The hour, minute, second, and millisecond fields of the java.util.Date base class should be set to zero. If the number of milliseconds supplied to the java.sql.Date constructor is negative, the driver will compute the date as the number of milliseconds before January 1, 1970. Otherwise, the date is computed as the specified number of milliseconds after January 1, 1970.

java.sql.Time for SQL TIME information. The year, month, and day fields of the java.util.Date base class are set to 1970, January, and 1. This is the "zero" date in the Java epoch.

java.sql.Timestamp for SQL TIMESTAMP information. This class extends java.util.Date by adding a nanoseconds field.

Leos Literak
  • 8,805
  • 19
  • 81
  • 156
0

This assumes your date entered in the GUI field is in the "dd/MM/yyyy" format. Adjust the pattern in SimpleDateFormat accordingly if it is different..

SimpleDateFormat sdf = new SimpleDateFomat("dd/MM/yyyy");
st.setDate(7,new java.sql.Date(sdf.parse(text_dob.getText()).getTime()));
st.setDate(8,new java.sql.Date(sdf.parse(text_doe.getText()).getTime()));

In case your GUI field contains date and time like "31/01/2014 23:59:59", the corresponding pattern in SimpleDateFormat is "dd/MM/yyyy HH:mm:ss". You can read the SimpleDateFormat docs for full pattern format.

http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Leos Literak
  • 8,805
  • 19
  • 81
  • 156
anonymous
  • 1,317
  • 8
  • 7