patternjavaMinor
Inserting a Java object into a MySQL database
Viewed 0 times
intojavadatabasemysqlinsertingobject
Problem
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();
}
}Solution
If you don't have Java 8, here are a few things you can do.
-
Use a string builder instead of concatenating string together. Each time you concatenate to a string, a new string is created. Using a String builder is a more efficient way of doing the same thing.
-
You can use String.join to generate a comma separated string of headers
-
I'm not sure the best way to generate the list of question marks, but this is how I normally do it.
-
When setting your parameters, using i++ instead of hardcoded numbers makes code maintenance easier. If you end up needing to add one more setString(), you can add the one line of code without having adjust the rest of the indexes
-
Use a string builder instead of concatenating string together. Each time you concatenate to a string, a new string is created. Using a String builder is a more efficient way of doing the same thing.
StringBuilder sb = new StringBuilder("INSERT into authorizations (");
sb.append("another string");
sb.append("another string");
String query = sb.toString();-
You can use String.join to generate a comma separated string of headers
String headers = String.join(",", authorizationHeaders);
sb.append(headers);-
I'm not sure the best way to generate the list of question marks, but this is how I normally do it.
sb.append( ") values(");
boolean first = true;
for(String header: authorizationHeaders) {
if(first){
sb.append("?");
first= false;
}else{
sb.append("?,");
}
}
sb.append(")");-
When setting your parameters, using i++ instead of hardcoded numbers makes code maintenance easier. If you end up needing to add one more setString(), you can add the one line of code without having adjust the rest of the indexes
int i=1;
ps.setString(i++, authorization.getCompany());
ps.setString(i++, authorization.getPromoType());
ps.setString(i++, authorization.getPromoDescription());
ps.setString(i++, authorization.getStartDate());
ps.setString(i++, authorization.getEndDate());
ps.setString(i++, authorization.getVlMarketingNum());
ps.setString(i++, authorization.getMarketingNum());
ps.setString(i++, authorization.getStatus());
ps.setDouble(i++, authorization.getForecast());
ps.setDouble(i++, authorization.getActual());Code Snippets
StringBuilder sb = new StringBuilder("INSERT into authorizations (");
sb.append("another string");
sb.append("another string");
String query = sb.toString();String headers = String.join(",", authorizationHeaders);
sb.append(headers);sb.append( ") values(");
boolean first = true;
for(String header: authorizationHeaders) {
if(first){
sb.append("?");
first= false;
}else{
sb.append("?,");
}
}
sb.append(")");int i=1;
ps.setString(i++, authorization.getCompany());
ps.setString(i++, authorization.getPromoType());
ps.setString(i++, authorization.getPromoDescription());
ps.setString(i++, authorization.getStartDate());
ps.setString(i++, authorization.getEndDate());
ps.setString(i++, authorization.getVlMarketingNum());
ps.setString(i++, authorization.getMarketingNum());
ps.setString(i++, authorization.getStatus());
ps.setDouble(i++, authorization.getForecast());
ps.setDouble(i++, authorization.getActual());Context
StackExchange Code Review Q#107567, answer score: 6
Revisions (0)
No revisions yet.