cómo cerrar las conexiones abiertas a MySQL
Publicado por Francisco (56 intervenciones) el 07/10/2018 15:15:54
Buenos días. Me pasan el código que me explique como cerrar las conexiones abiertas a la base de datos, por favor?
Valora esta pregunta


0
package proyectoppiiclinicamedica.usuarios;
import java.awt.HeadlessException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
//&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
public class Conexion {
private static String url = "jdbc:mysql://localhost:3306/mondo?autoReconnect=true&useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT-3";
private static String usuario = "root";
private static String password = "francisco";
private static Connection conexion = null;
public Conexion() throws ClassNotFoundException, SQLException,HeadlessException{
if( conexion == null){
try{
Class.forName("com.mysql.jdbc.Driver");
conexion = DriverManager.getConnection(url, usuario, password);
//conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/mondo?autoReconnect=true&useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root","francisco");
if( conexion != null){
JOptionPane.showMessageDialog(null,"estas conectado");
}
}catch(ClassNotFoundException ex){
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE,null,ex);
}catch(SQLException ex){
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE,null,ex);
}catch(HeadlessException ex){
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
public Connection getConexion() {
return conexion;
}
public void cerrarConexion(){
Connection cx = null;
try{
if(cx == null){
Conexion conex = new Conexion();
cx = conex.getConexion();
}
}catch(SQLException ex){
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE,null,ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
} catch (HeadlessException ex) {
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
}finally{
try{
if( cx != null){
cx.close();
}
}catch(SQLException ex){
Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
}
if (conexion != null) {
try {
conexion.close();
} catch (SQLException e) { /* ignored */}
}