RE: Setting year column from PreparedStatement give truncation exception
"Neil Aggarwal" <[email protected]> Mon, 12 Feb 2007 19:44:22 -0600
| Newsgroups | gmane.comp.db.mysql.java |
|---|---|
| Organization | JAMM Consulting, Inc. |
| Message-ID | <000001c74f10$7ce60970$0501a8c0@neilhp> |
Mark: I will try that. Thanks, Neil -- Neil Aggarwal, (214)986-3533, www.JAMMConsulting.com FREE! Eliminate junk email and reclaim your inbox. Visit http://www.spammilter.com for details. -----Original Message----- From: Mark Matthews [mailto:[email protected]] Sent: Monday, February 12, 2007 6:47 PM To: 'Neil Aggarwal'; [email protected] Subject: RE: Setting year column from PreparedStatement give truncation exception -----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] -- MySQL Java Mailing List For list archives: http://lists.mysql.com/java To unsubscribe: http://lists.mysql.com/[email protected]