josefloressv

db.sql

May 13th, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. /**
  4.  *
  5.  * @author josefloressv
  6.  */
  7. public class db {
  8.  
  9.     private static String user = "root";
  10.     private static String pass = "root";
  11.     private static String url = "jdbc:mysql://localhost:3306/db_favoritos";
  12.     private static String jdbcdriver = "com.mysql.jdbc.Driver";
  13.     private static Driver driver = null;
  14.     //Evitar la concurrencia con diferentes usuarios conectados
  15.     //o diferentes peticiones.
  16.  
  17.     public static synchronized Connection getConnection() throws SQLException {
  18.         try {
  19.             //Registrar el Driver
  20.             Class jdbcDriverClass = Class.forName(jdbcdriver);
  21.             driver = (Driver) jdbcDriverClass.newInstance();
  22.             DriverManager.registerDriver(driver);            
  23.  
  24.         } catch (InstantiationException ex) {
  25.             System.out.println("Excepcion al crear la Instancia de jdbcDriverClass");
  26.         } catch (IllegalAccessException ex) {
  27.             System.out.println("Excepcion al crear la Instancia de jdbcDriverClass");
  28.         } catch (ClassNotFoundException e) {
  29.             System.out.println("Error al Cargar el Driver");
  30.             e.printStackTrace();
  31.         }
  32.         return DriverManager.getConnection(url, user, pass);
  33.     }
  34.   //Cierre de Conexion
  35.     public static void close(Connection con){
  36.         try {
  37.             if(con!=null) con.close();
  38.         } catch (Exception e) {
  39.         }
  40.     }
  41.     //Cierre de resultset
  42.     public static void close(ResultSet rs){
  43.         try {
  44.             if(rs!=null) rs.close();
  45.         } catch (Exception e) {
  46.         }
  47.     }
  48.     //Cierre de Statement
  49.     public static void close(PreparedStatement stmt){
  50.         try {
  51.             if(stmt!=null) stmt.close();
  52.         } catch (Exception e) {
  53.         }
  54.     }
  55.    
  56. }
Advertisement
Add Comment
Please, Sign In to add comment