Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void handleSaveButton() throws SQLException {
- countryId = getCountryId();
- cityId = getCityId();
- addressId = getAddressId();
- }
- /**
- *
- * @return @throws SQLException
- */
- private long getCountryId() throws SQLException {
- getConnection();
- param = toTitleCase(countryTextField.getText());
- System.out.println("country param: " + param);
- String query = "insert into country (country) values (?)";
- ps = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
- ps.setString(1, param);
- try {
- ps.executeUpdate();
- rs = ps.getGeneratedKeys();
- rs.next();
- primaryKey = rs.getLong(1);
- System.out.println("inserted country id: " + primaryKey);
- } catch (SQLIntegrityConstraintViolationException e) {
- ps = con.prepareStatement("select countryId from country where country = ?");
- ps.setString(1, param);
- rs = ps.executeQuery();
- rs.next();
- primaryKey = (long) rs.getInt(1);
- System.out.println("existing country id: " + primaryKey);
- } finally {
- closeConnection(ps);
- }
- return primaryKey;
- }
- /**
- *
- * @return @throws SQLException
- */
- private long getCityId() throws SQLException {
- getConnection();
- param = toTitleCase(cityTextField.getText());
- System.out.println("city param: " + param);
- String query = "insert into city (city, countryId) values (?, ?)";
- ps = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
- ps.setString(1, param);
- ps.setLong(2, countryId);
- try {
- ps.executeUpdate();
- rs = ps.getGeneratedKeys();
- rs.next();
- primaryKey = rs.getLong(1);
- System.out.println("inserted city id: " + primaryKey);
- } catch (SQLIntegrityConstraintViolationException e) {
- ps = con.prepareStatement("select cityId from city where city = ?");
- ps.setString(1, param);
- rs = ps.executeQuery();
- rs.next();
- primaryKey = (long) rs.getInt(1);
- System.out.println("existing city id: " + primaryKey);
- } finally {
- closeConnection(ps);
- }
- return primaryKey;
- }
- /**
- *
- * @return @throws SQLException
- */
- private long getAddressId() throws SQLException {
- getConnection();
- param = toTitleCase(addressTextField.getText());
- System.out.println("address param: " + param);
- String query = "insert into address (address, cityId, postalCode, phone) values (?,?,?,?)";
- ps = con.prepareStatement(query, Statement.RETURN_GENERATED_KEYS);
- ps.setString(1, param);
- ps.setLong(2, cityId);
- ps.setString(3, postalTextField.getText());
- ps.setString(4, phoneTextField.getText());
- try {
- ps.executeUpdate();
- rs = ps.getGeneratedKeys();
- rs.next();
- primaryKey = rs.getLong(1);
- System.out.println("inserted address id: " + primaryKey);
- } catch (SQLIntegrityConstraintViolationException e) {
- ps = con.prepareStatement("select addressId from address where address = ? and cityId = ?");
- ps.setString(1, param);
- ps.setLong(2, cityId);
- rs = ps.executeQuery();
- rs.next();
- primaryKey = (long) rs.getInt(1);
- System.out.println("existing address id: " + primaryKey);
- } finally {
- closeConnection(ps);
- }
- return primaryKey;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement