Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- /**
- *
- * @author josefloressv
- */
- public class db {
- private static String user = "root";
- private static String pass = "root";
- private static String url = "jdbc:mysql://localhost:3306/db_favoritos";
- private static String jdbcdriver = "com.mysql.jdbc.Driver";
- private static Driver driver = null;
- //Evitar la concurrencia con diferentes usuarios conectados
- //o diferentes peticiones.
- public static synchronized Connection getConnection() throws SQLException {
- try {
- //Registrar el Driver
- Class jdbcDriverClass = Class.forName(jdbcdriver);
- driver = (Driver) jdbcDriverClass.newInstance();
- DriverManager.registerDriver(driver);
- } catch (InstantiationException ex) {
- System.out.println("Excepcion al crear la Instancia de jdbcDriverClass");
- } catch (IllegalAccessException ex) {
- System.out.println("Excepcion al crear la Instancia de jdbcDriverClass");
- } catch (ClassNotFoundException e) {
- System.out.println("Error al Cargar el Driver");
- e.printStackTrace();
- }
- return DriverManager.getConnection(url, user, pass);
- }
- //Cierre de Conexion
- public static void close(Connection con){
- try {
- if(con!=null) con.close();
- } catch (Exception e) {
- }
- }
- //Cierre de resultset
- public static void close(ResultSet rs){
- try {
- if(rs!=null) rs.close();
- } catch (Exception e) {
- }
- }
- //Cierre de Statement
- public static void close(PreparedStatement stmt){
- try {
- if(stmt!=null) stmt.close();
- } catch (Exception e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment