on 11-23-2005 8:14 AM
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
Problem is solved.
the solution is:
newTitle.setDate(3, java.sql.Date.valueOf(creationDate));
newTitle.setTime(4, java.sql.Time.valueOf(creationTime));
Richard
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
9 | |
7 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.