Siguiendo los principios de la programación orientada a objetos, podrías encapsular una clase manager para que se encargue de las conexiones y las consultas, además del cierre correcto de las mismas. Por ejemplo:
##MySQLManager:##
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
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 {
statement.close();
} catch (Exception e) {
}
try {
connection.close();
} catch (Exception e) {
}
}
}
}
##PruebaConexion:##
Finalmente, te puedes encargar de realizar tus acciones solamente ingresando los parametros correctos y dejando que la clase se encargue del resto.
import java.sql.ResultSet;
public class PruebaConexion {
public static void main(String[] args) {
MySQLManager manager = new MySQLManager("localhost", "3306", "mydatabase", "user", "pass");
manager.executeUpdate("create database if not exists base");
manager.executeUpdate("use base");
manager.executeUpdate("create table if not exists msg (id int primary key auto_increment, nombre varchar(45))");
//crear n tablas...
//Queries
String sql = "select * from msg";
ResultSet rs = manager.executeQuery(sql);
}
}
como extra, si quieres realizar alguna acción con tus resultados, te dejo un ejemplo:
while (result.next()) {
Product product = new Product();
product.setTotal(result.getDouble("Total"));
product.setName(result.getString("Name"));
// etc.
productList.add(product);
}
La receta es la misma, solamente debes adecuarlo a tu problema.