Results 1 to 6 of 6
- 03-04-2010, 01:02 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 4
- Rep Power
- 0
Prepared statement's 'setDate' method and MySQL execption
Hi Guys...
I am new here. I was looking for a JDBC section in this forum, couldn't find one and this particular section seems the closest to what I was looking for, so I am posting here.
I am listing down the full source code here:
However when I run this servlet, I am getting the following errorJava Code:import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.annotation.Resource; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; import java.sql.Date; public class JDBCUpdateServlet extends HttpServlet { @Resource(name = "jdbc/DMIT") private DataSource dataSource; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String insertBookingSql = "insert into booking (idbooking, bookingdate, FK_idfacility, FK_iddate) values (?,?,?,?)"; String updateBookingbookingdateSql = "update booking set bookingdate = ? where idbooking = ?"; //String deleteBookingSql = "delete from booking where idbooking = ?"; PreparedStatement insertBookingStatement; //PreparedStatement updateBookingdateStatement; //PreparedStatement deleteBookingStatement; try { Connection connection = dataSource.getConnection(); insertBookingStatement = connection.prepareStatement(insertBookingSql); //updateBookingdateStatement = connection.prepareStatement(updateBookingbookingdateSql); //deleteBookingStatement = connection.prepareStatement(deleteBookingSql); //*** FIRST ROW **** //ID insertBookingStatement.setString(1, "B05"); //Booking date Date bookingDate = new Date(20090629); insertBookingStatement.setDate(2,bookingDate); //FK_idfacility insertBookingStatement.setString(3, "T2033B"); //FK_iddate Date idDate = new Date(20090630); insertBookingStatement.setDate(4,idDate); insertBookingStatement.executeUpdate(); //*** END OF FIRST ROW **** //insertCustomerStatement.setInt(1, 2); //insertCustomerStatement.setString(2, "Jane"); //insertCustomerStatement.setString(3, "Davis"); //insertCustomerStatement.setString(4, null); //insertCustomerStatement.executeUpdate(); //updateCustomerLastNameStatement.setString(1, "Jones"); //updateCustomerLastNameStatement.setInt(2, 2); //updateCustomerLastNameStatement.executeUpdate(); //deleteCustomerStatement.setInt(1, 1); // //deleteCustomerStatement.executeUpdate(); connection.close(); response.getWriter().println("Database Updated Successfully"); } catch (SQLException e) { e.printStackTrace(); } } }
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityCons traintViolationException: Cannot add or update a child row: a foreign key constraint fails (`ebooking`.`booking`, CONSTRAINT `FK_iddate` FOREIGN KEY (`FK_iddate`) REFERENCES `calendar` (`iddate`)) '
I believe I understand what the error message is saying, that I can't enter a value in the FK_iddate column that dosen't exist in the iddate column of the calendar table. I find this puzzling as the value I am trying to add for FK_iddate column already exists in the calendar table and the relationship between the calendar and booking table is '1(calendar) to many(booking)' However when I run the SQL query directly in MYSQL itself, I am not getting any error and a new row is sucessfully added in the booking table. The MySQL query is as follows:
Any idea what is wrong, I am very puzzled by this.Java Code:insert into booking (idbooking, bookingdate, FK_idfacility, FK_iddate) values ('B05','2009-06-29','T2033B','2009-06-30');
regards
glisando
- 03-04-2010, 01:23 PM #2
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-04-2010, 01:27 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 4
- Rep Power
- 0
Hi there...
Yes I did post in the other forum you mentioned. I dont think there is anything wrong. I am looking for answers, and I think there is nothing wrong in opening up the problem to more people. Infact I have also posted the same problem in one other forum too. When the problem is eventually solved, I will be sharing the solution with everyone, I think more people will benefit this way. Thanks.
- 03-04-2010, 01:32 PM #4
Then say upfront that you have crossposted. Boy, I'd be pissed to give you a lengthy reply and somebody else had already given the same reply on another forum. You're likely wasting volunteer's time here.
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 03-04-2010, 03:17 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 4
- Rep Power
- 0
I am sorry I didnt know there was a need to declare upfront that one has posted the same question in another forum. In the past I have posted questions in more than one forum and never once did I get the same reply from the various forums.
I respect your views, though personally I don't feel that I am wasting anybody's time here. If I stick with just one forum, there is a possibility I may not get the answers I need. Also as I mentioned before, once the problem has been solved, the solution will be shared with one and all.
If have wasted your time in anyway, I am terribly sorry.
- 03-05-2010, 08:31 AM #6
Member
- Join Date
- Mar 2010
- Posts
- 4
- Rep Power
- 0
Hi Guys...the problem has been solved and the working code is as below.
For my purpose, 'Date' should have been used this way:Java Code:import java.io.IOException; //import java.sql.Connection; //import java.sql.PreparedStatement; //import java.sql.SQLException; import javax.annotation.Resource; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.sql.DataSource; import java.sql.*; //import java.util.Calendar; public class JDBCUpdateServlet extends HttpServlet { @Resource(name = "jdbc/DMIT") private DataSource dataSource; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String insertBookingSql = "insert into booking (idbooking, bookingdate, FK_idfacility, FK_iddate) values (?,?,?,?)"; //String updateBookingbookingdateSql = "update booking set bookingdate = ? where idbooking = ?"; //String deleteBookingSql = "delete from booking where idbooking = ?"; PreparedStatement insertBookingStatement; try { Connection connection = dataSource.getConnection(); insertBookingStatement = connection.prepareStatement(insertBookingSql); Date bdate = new Date(0000-00-00); Date iddate = new Date(0000-00-00); //*** FIRST ROW **** //ID insertBookingStatement.setString(1, "B05"); //Booking date insertBookingStatement.setDate(2,Date.valueOf("2009-06-29")); //FK_idfacility insertBookingStatement.setString(3, "T2033B"); //FK_iddate insertBookingStatement.setDate(4,Date.valueOf("2009-06-29")); insertBookingStatement.executeUpdate(); //*** END OF FIRST ROW **** connection.close(); response.getWriter().println("Database Updated Successfully"); } catch (SQLException e) { e.printStackTrace(); } } }
Date.valueOf("2009-06-29"). I had mistakenly done this
Date d1 = new Date(20090630); and d1 was assigned a 1970 date and hence the error message about the FK_iddate column not existing in the iddate column of the calendar table. I hope this info helps those facing the same problem as me. Thanks PhHein for the help.
Similar Threads
-
Hi Null pointer Execption
By kirtichopra2003 in forum Advanced JavaReplies: 0Last Post: 10-09-2009, 10:56 AM -
Null Pointer Execption
By bl00dshooter in forum AWT / SwingReplies: 2Last Post: 09-09-2009, 10:38 PM -
MySQL/JDBC Prepared Statement Select query
By thelinuxguy in forum Advanced JavaReplies: 4Last Post: 02-12-2009, 05:29 PM -
MySQL/JDBC prepared statement problem
By thelinuxguy in forum Advanced JavaReplies: 3Last Post: 02-11-2009, 11:21 PM -
get Trouble with SetDate
By hungleon88 in forum JDBCReplies: 0Last Post: 09-17-2008, 09:02 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks