Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. package undergroundpersist;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.SQLException;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13.  
  14.  
  15. public class ConexionBDRelacional {
  16.  
  17.     private Connection conn = null;
  18.     private String userName;
  19.     private String password;
  20.     private String driver;
  21.     private String url;
  22.     private String ruta = "D:/Documents/Trabajo De Campo/UndergroundPersist/src/undergroundpersist/ConfiguracionBD.txt";
  23.  
  24.     Connection Connect() {
  25.  
  26.         try {
  27.  
  28.             File archivo = new File(ruta);
  29.             FileReader lector = new FileReader(archivo);
  30.             BufferedReader buffer = new BufferedReader(lector);
  31.  
  32.             userName = buffer.readLine();
  33.             password = buffer.readLine();
  34.             driver = buffer.readLine();
  35.             url = buffer.readLine();
  36.  
  37.  
  38.             // Conexion
  39.  
  40.             try {
  41.                 Class.forName(driver).newInstance(); // setup of the database driver
  42.                 conn = DriverManager.getConnection(url, userName, password); // creates the connection
  43.             } catch (SQLException ex) {
  44.                 Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
  45.             } catch (ClassNotFoundException ex) {
  46.                 Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
  47.             } catch (InstantiationException ex) {
  48.                 Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
  49.             } catch (IllegalAccessException ex) {
  50.                 Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
  51.             }
  52.  
  53.         } catch (FileNotFoundException e) {
  54.             System.out.println("No se encontro el archivo");
  55.         } catch (IOException m) {
  56.             System.out.println("No sepuede leer el archivo");
  57.         }
  58.         return conn;
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement