Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. private void handleSaveButton() throws SQLException {
  2. countryId = getCountryId();
  3. cityId = getCityId();
  4. addressId = getAddressId();
  5. }
  6.  
  7. /**
  8. *
  9. * @return @throws SQLException
  10. */
  11. private long getCountryId() throws SQLException {
  12. getConnection();
  13. param = toTitleCase(countryTextField.getText());
  14. System.out.println("country param: " + param);
  15. String query = "insert into country (country) values (?)";
  16. ps = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
  17. ps.setString(1, param);
  18. try {
  19. ps.executeUpdate();
  20. rs = ps.getGeneratedKeys();
  21. rs.next();
  22. primaryKey = rs.getLong(1);
  23. System.out.println("inserted country id: " + primaryKey);
  24. } catch (SQLIntegrityConstraintViolationException e) {
  25. ps = con.prepareStatement("select countryId from country where country = ?");
  26. ps.setString(1, param);
  27. rs = ps.executeQuery();
  28. rs.next();
  29. primaryKey = (long) rs.getInt(1);
  30. System.out.println("existing country id: " + primaryKey);
  31. } finally {
  32. closeConnection(ps);
  33. }
  34. return primaryKey;
  35. }
  36.  
  37. /**
  38. *
  39. * @return @throws SQLException
  40. */
  41. private long getCityId() throws SQLException {
  42. getConnection();
  43. param = toTitleCase(cityTextField.getText());
  44. System.out.println("city param: " + param);
  45. String query = "insert into city (city, countryId) values (?, ?)";
  46. ps = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
  47. ps.setString(1, param);
  48. ps.setLong(2, countryId);
  49. try {
  50. ps.executeUpdate();
  51. rs = ps.getGeneratedKeys();
  52. rs.next();
  53. primaryKey = rs.getLong(1);
  54. System.out.println("inserted city id: " + primaryKey);
  55. } catch (SQLIntegrityConstraintViolationException e) {
  56. ps = con.prepareStatement("select cityId from city where city = ?");
  57. ps.setString(1, param);
  58. rs = ps.executeQuery();
  59. rs.next();
  60. primaryKey = (long) rs.getInt(1);
  61. System.out.println("existing city id: " + primaryKey);
  62. } finally {
  63. closeConnection(ps);
  64. }
  65. return primaryKey;
  66. }
  67.  
  68. /**
  69. *
  70. * @return @throws SQLException
  71. */
  72. private long getAddressId() throws SQLException {
  73. getConnection();
  74. param = toTitleCase(addressTextField.getText());
  75. System.out.println("address param: " + param);
  76. String query = "insert into address (address, cityId, postalCode, phone) values (?,?,?,?)";
  77. ps = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
  78. ps.setString(1, param);
  79. ps.setLong(2, cityId);
  80. ps.setString(3, postalTextField.getText());
  81. ps.setString(4, phoneTextField.getText());
  82. try {
  83. ps.executeUpdate();
  84. rs = ps.getGeneratedKeys();
  85. rs.next();
  86. primaryKey = rs.getLong(1);
  87. System.out.println("inserted address id: " + primaryKey);
  88. } catch (SQLIntegrityConstraintViolationException e) {
  89. ps = con.prepareStatement("select addressId from address where address = ? and cityId = ?");
  90. ps.setString(1, param);
  91. ps.setLong(2, cityId);
  92. rs = ps.executeQuery();
  93. rs.next();
  94. primaryKey = (long) rs.getInt(1);
  95. System.out.println("existing address id: " + primaryKey);
  96. } finally {
  97. closeConnection(ps);
  98. }
  99. return primaryKey;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement