Advertisement
Maria-I-Pap

Untitled

Nov 22nd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. /**
  2. * Establish a database connection and add the member in the database.
  3. *
  4. * @param user
  5. * @throws ClassNotFoundException
  6. */
  7. public static void addUser(cs359db.model.User user) throws ClassNotFoundException {
  8. // Check that we have all we need
  9. try {
  10. user.checkFields();
  11.  
  12. } catch (Exception ex) {
  13. // Log exception
  14. Logger.getLogger(cs359db.model.User.class.getName()).log(Level.SEVERE, null, ex);
  15. }
  16. try {
  17. try (Connection con = CS359DB.getConnection();
  18. Statement stmt = con.createStatement()) {
  19.  
  20. StringBuilder insQuery = new StringBuilder();
  21.  
  22. insQuery.append("INSERT INTO ")
  23. .append(" user (USERNAME, EMAIL, PASSWORD, FIRSTNAME, LASTNAME, BIRTHDATE, COUNTRY, TOWN, ADDITIONAL) ")
  24. .append(" VALUES (")
  25. .append("'").append(user.getUserName()).append("',")
  26. .append("'").append(user.getEmail()).append("',")
  27. .append("'").append(user.getPassword()).append("',")
  28. .append("'").append(user.getFirstName()).append("',")
  29. .append("'").append(user.getLastName()).append("',")
  30. .append("'").append(user.getBirthDate()).append("',")
  31. //.append("'").append(user.getGender()).append("',")
  32. .append("'").append(user.getCountry()).append("',")
  33. .append("'").append(user.getTown()).append("',")
  34. .append("'").append(user.getInfo()).append("');");
  35.  
  36. stmt.executeUpdate(insQuery.toString());
  37. System.out.println("#DB: The member was successfully added in the database.");
  38.  
  39. // Close connection
  40. stmt.close();
  41. con.close();
  42.  
  43. }
  44.  
  45. } catch (SQLException ex) {
  46. // Log exception
  47. Logger.getLogger(cs359db.model.User.class.getName()).log(Level.SEVERE, null, ex);
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement