I'm writing a method to insert objects into a MySQL database and while everything runs fine I feel my code is bulky and inefficient. What steps would you take to make this code more pleasant to look at and more efficient?
public void addAuthorization(Authorization authorization) {
try {
// Builds the query, lastHeader is used to prevent a trailing comma
query = "INSERT into authorizations (";
for(int i = 0; i < authorizationsHeaders.size() - 1; i++) {
query += authorizationsHeaders.get(i) + ", ";
questionMarks += "?, ";
}
String lastHeader = authorizationsHeaders.get(authorizationsHeaders.size() - 1);
query += lastHeader + ") values(" + questionMarks + "?)";
openConnection();
ps = con.prepareStatement(query);
ps.setString(1, authorization.getCompany());
ps.setString(2, authorization.getPromoType());
ps.setString(3, authorization.getPromoDescription());
ps.setString(4, authorization.getStartDate());
ps.setString(5, authorization.getEndDate());
ps.setString(6, authorization.getVlMarketingNum());
ps.setString(7, authorization.getMarketingNum());
ps.setString(8, authorization.getStatus());
ps.setDouble(9, authorization.getForecast());
ps.setDouble(10, authorization.getActual());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeConnection();
}
}