cancel
Showing results for 
Search instead for 
Did you mean: 

SQL error save date and time in sql database

Former Member
0 Kudos

hi,

When I try to save a date in to a date field I get the following error:

Cannot assign an object of type java.lang.String to host variable 3 which has JDBC type DATE.

The solution I used worked fine but we I changed my application to use DAO and datasources I get to error.

following is my code i use.

public void createTitle(int title_id, String description, String user)

throws SQLException {

// TODO Auto-generated method stub

Date date = new Date();

String dateFormat = "yyyy-MM-dd";

String timeFormat = "HH:mm:ss";

SimpleDateFormat ddf = new SimpleDateFormat(dateFormat);

SimpleDateFormat tdf = new SimpleDateFormat(timeFormat);

String creationDate = ddf.format(date);

String creationTime = tdf.format(date);

PreparedStatement newTitle = conn.prepareStatement("INSERT INTO BE_TITLE (TITLE_ID, DESCRIPTION, DATE_CREATE, USER_CREATE) VALUES (?,?,?,?,?)");

try{

newTitle.setInt(1,title_id);

newTitle.setString(2,description);

newTitle.setString(3,creationDate);

newTitle.setString(4,creationTime);

newTitle.setString(5, user);

newTitle.executeUpdate();

}finally{

newTitle.close();

}

}

What is wrong and please give me a solotion.

Kind Regards,

Richard

Accepted Solutions (0)

Answers (6)

Answers (6)

Former Member
0 Kudos

Problem is solved.

the solution is:

newTitle.setDate(3, java.sql.Date.valueOf(creationDate));

newTitle.setTime(4, java.sql.Time.valueOf(creationTime));

Richard

former_member182372
Active Contributor
0 Kudos

Richard, please, mark thread as "Problem solved" for further users.

Former Member
0 Kudos

hi Maksim,

This is the following error which I get when I changed the code.

Invalid object 11/21/05 10:32 AM of type java.sql.Date assigned to host variable 3.

When I look at the Java forum they come with the following solution:

I d'not understand what I have to do, It seen there is a bug.

maybe your can tell me?

Richard

former_member182372
Active Contributor
0 Kudos

Richard, ask Detlev in a mentioned forum about OSS ticket number, may be solution is described there.

Former Member
0 Kudos

Hi,

I using the type Date and time in my database table

How can I past the current Date and Time to the type date and time in my database by using JDBC.

Richard

former_member182372
Active Contributor
0 Kudos

Hm...really confusing.

Doesn`t this work?


...
newTitle.setDate(3, new java.sql.Date(date.getTime()));
newTitle.setTime(4, new java.sql.Time(date.getTime()));
...

Could you post your new SQL insert statment?

Former Member
0 Kudos

Hi Richard,

I guess your in your database DATE_CREATE field type is Date and you are passing string value.

So, thats why it is giving this error.

regards,

Bhavik

Former Member
0 Kudos

Hi Maksim,

Now I get the following error:

Invalid object 11/21/05 9:32 AM of type java.sql.Date assigned to host variable 3.

In my database table I have a fields date en time.

I show already the mistake in the sql statement, but thanks for notice me.

Richard

former_member182372
Active Contributor
0 Kudos

Hello Richard,

What is exactly type for parameter number 3? DATE or something else?

BTW, Why did you post question in Dynpro forum? I think Java Forum is more appropriate place for this.

Best regards, Maksim Rashchynski.

former_member182372
Active Contributor
0 Kudos

Hello Richard,

Why don`t you use setDate method? Like


...
newTitle.setDate(3, new java.sql.Date(date.getTime()));
...

BTW, SQL is not correct. You have only 4 parameters defined (TITLE_ID, DESCRIPTION, DATE_CREATE, USER_CREATE) but 5 placeholders (?).

Best regards, Maksim Rashchynski.