HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Creating connections and executing queries/updates

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
executingcreatingupdatesandqueriesconnections

Problem

I've recently learned how to use MySQL to store/retrieve data. I've created a MySQLManager class for creating connections and executing queries/updates. Would this be an efficient way to do so?

```
public class MySQLManager {

private String host;
private String port;
private String database;
private String user;
private String password;

public MySQLManager(String host, String port, String database, String user, String password){
this.host = host;
this.port = port;
this.database = database;
this.user = user;
this.password = password;
}

private Connection createConnection(){
Connection connection;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, user, password);
return connection;
} catch (SQLException e) {
System.out.println("Could not connect to MySQL server! because: " + e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("JDBC Driver not found!");
}
return null;
}

public ResultSet executeQuery(String query){
Connection connection = createConnection();
Statement statement = null;
ResultSet set = null;
try {
statement = connection.createStatement();
set = statement.executeQuery(query);
return set;
} catch (SQLException e) {
e.printStackTrace();
} finally { //Close in order: ResultSet, Statement, Connection.
try { set.close(); } catch (Exception e) { }
try { statement.close(); } catch (Exception e) { }
try { connection.close(); } catch (Exception e) { }
}
return null;
}

public void executeUpdate(String update){
Connection connection = createConnection();
Statement statement = null;
try {
statement = connection.createStatement();
statement.executeUpdate(update);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try { st

Solution

You've written a wrapper around JDBC that automatically connects/disconnects, and swallows exceptions. Don't do that.

JDBC wants you to establish connections explicitly because it's a high-overhead activity. Connecting involves a TCP handshake, authentication, opening the requested database, and some work to set up a session (which includes some locale settings, for example). You have just taken a good design decision of JDBC (and all other database APIs) and made it worse.

Furthermore, if every query/update is in its own connection, then you can't execute multiple statements in a transaction. (MySQL users often don't appreciate the value of transactions.)

Finally, SQLException exists for a reason: they allow your code to gracefully handle failures. You merely swallow the exceptions after printing a stack trace. You might as well be writing your data to /dev/null if you don't care whether your writes succeeded. Also, returning null instead of propagating an exception does not make your caller's code any simpler.

I strongly recommend discarding your entire MySQLManager class and using JDBC as is.

Context

StackExchange Code Review Q#55401, answer score: 7

Revisions (0)

No revisions yet.