Created
April 29, 2013 18:53
-
-
Save gaso/5483803 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.Connection; | |
import java.sql.DriverManager; | |
import java.sql.PreparedStatement; | |
import java.sql.SQLException; | |
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import javax.sql.DataSource; | |
import javax.swing.JOptionPane; | |
/** | |
* | |
* @author PCGASO | |
*/ | |
public class bd { | |
//Instancia estatica de la conexion, sino uno llega a existir | |
private static Connection connection = null; | |
/** | |
* | |
* @return Devuelve la instancia unica de Conexion | |
*/ | |
public static Connection getConexion() { | |
//Si la instancia no ha sido creado aun, se crea | |
if (bd.connection == null) { | |
contruyendoConexion(); | |
} | |
return bd.connection; | |
} | |
//Obtener las instancias de Conexion JDBC | |
private static void contruyendoConexion() { | |
try { | |
Class.forName("org.postgresql.Driver"); | |
String url = "jdbc:postgresql://localhost:5432/bd?allowEncodingChanges=true"; | |
String usuario = "postgres"; | |
String clave = "gidsoft"; | |
bd.connection = DriverManager.getConnection(url, usuario, clave); | |
} catch (ClassNotFoundException e) { | |
System.out.println("ClassNotFoundException(contruyendoConexion) : " + e.getMessage()); | |
System.gc(); | |
} catch (SQLException e) { | |
System.out.println("SQLException(contruyendoConexion) : " + e.getMessage()); | |
System.gc(); | |
JOptionPane.showMessageDialog(null, e.getMessage(), "No fue Posible Conectar con la Base de Datos", JOptionPane.ERROR_MESSAGE); | |
System.exit(0); | |
} catch (Exception e) { | |
System.out.println(" Exception General (contruyendoConexion) : " + e.getMessage()); | |
System.gc(); | |
} | |
} | |
public static void liberarConexionS(Connection conex) { | |
try { | |
conex.close(); | |
} catch (SQLException ex) { | |
System.out.println(estadosSQL.getM(ex.getSQLState(), ex.getMessage())); | |
} | |
} | |
/** | |
* Cierra la conexion. Al provenir de BasicDataSource, en realidad no se | |
* esta cerrando. La llamada a close() le indica al BasicDataSource que | |
* hemos terminado con dicha conexion y que puede asignarsela a otro que la | |
* pida. | |
* | |
* @param conexion | |
*/ | |
//esto es un pool de conexiones, pero no se esta usando | |
public static void liberaConexion(Connection conexion) { | |
try { | |
if (null != conexion) { | |
// En realidad no cierra, solo libera la conexion. | |
conexion.close(); | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void liberarStatement(PreparedStatement p){ | |
try{ | |
if(null != p){ | |
p.close(); | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment