RE: Setting year column from PreparedStatement give truncation exception

"Mark Matthews" <[email protected]> Mon, 12 Feb 2007 18:47:02 -0600
Newsgroups gmane.comp.db.mysql.java
Message-ID <00f601c74f08$7bac2380$b91da8c0@marksworkbox>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



> -----Original Message-----
> From: Neil Aggarwal [mailto:[email protected]]
> Sent: Monday, February 12, 2007 1:21 PM
> To: [email protected]
> Subject: Setting year column from PreparedStatement give truncation
> exception
> 
> Hello:
> 
> In my MySQL database, I have a column set as a YEAR type.
> 
> When I try to set the value from a prepared statement,
> I get a truncation exception.
> 
> What is the proper way to set a year colum from a prepared
> statement?
> 
> Here is my code:
> 
> public static final DateFormat YEAR_FORMAT = new SimpleDateFormat("yyyy");
> 
> ...
> 
> PreparedStatement ps = connection.prepareStatemnt(...);
> ps.setDate(MY_YEAR_INDEX,new
> java.sql.Date(YEAR_FORMAT.parse("2007").getTime());
> 
> When I do ps.executeUpdate, I get this exception:
> java.sql.SQLException: Data truncated for column 'myYear' at row 1
> 
> Any ideas?

Hi Neil,

You're getting a truncation exception, because the month and day from your java.sql.Date instance are being stripped away. If you want to set just the year, use PreparedStatement.setShort().

	-Mark
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFF0QqFtvXNTca6JD8RAsSRAJ9ceVRfoArWZXe/r1c9MmuY+cx4vACfW5o4
A7ApdLRbbnDaxD9shlOF/Ho=
=8RlY
-----END PGP SIGNATURE-----


-- 
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe:    http://lists.mysql.com/[email protected]