Guest User

Untitled

a guest
Aug 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. How to insert a string date to a mysql database date field
  2. package events;
  3.  
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.Statement;
  7. import java.util.Date;
  8. /**
  9. *
  10. * @author mewq
  11. */
  12. public class ConDb {
  13. public static void main(String[] args){
  14. String description = null;
  15. try{
  16. Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/tangkilikan", "user=root", "password=tangkilik");
  17.  
  18. Statement stat = (Statement) conn.createStatement();
  19.  
  20. String eventName = "Imba meeting";
  21. String Address = "Zamora, Cabarroguis, Quirino";
  22. Date date = '2011-09-09';
  23. String description = "meeting";
  24.  
  25.  
  26. String insert = "insert into events values ('" + eventName + "', '" + Address + "', ' + date + ', '" + description + "')";
  27.  
  28.  
  29. stat.executeUpdate(insert);
  30.  
  31. } catch(Exception e) {
  32.  
  33. }
  34.  
  35. }
  36.  
  37. }
  38.  
  39. DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
  40. Date date = (Date)formatter.parse("05/15/11");
  41.  
  42. String insert = "insert into events values ('" + eventName + "', '" + Address + "', '" + date + "', '" + description + "')";
  43.  
  44. Date date = '2011-09-09';
  45.  
  46. String dateString = "2011-09-09";
  47. DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  48. Date myDate = formatter.parse(dateString);
  49.  
  50. String insert = "insert into events values (?, ? , ? , ?)";
  51. PreparedStatement ps = conn.prepareStatment(insert);
  52. ps.setString(1, eventName);
  53. ps.setString(2, Address);
  54. ps.setDate(3, myDate);
  55. ps.setString(4, description);
  56.  
  57. ps.executeUpdate();
Add Comment
Please, Sign In to add comment