Guest User

Untitled

a guest
Dec 11th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package Test_Conexion;
  7.  
  8. /**
  9.  *
  10.  * @author Ciriaco
  11.  */
  12. import java.sql.*;
  13.  
  14. public class Clase_Conexion {
  15.  
  16.     private Connection conexion=null;
  17.     private String servidor="";
  18.     private String database="";
  19.     private String usuario="";
  20.     private String password="";
  21.     private String url="";
  22.    
  23.     public Clase_Conexion(String servidor, String database, String usuario, String password){
  24.         try {
  25.  
  26.             this.servidor = servidor;
  27.             this.database = database;
  28.  
  29.             Class.forName("com.mysql.jdbc.Driver");
  30.             //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  31.             url="jdbc:mysql://"+servidor+"/"+database;
  32.             conexion=DriverManager.getConnection(url, usuario, password);
  33.             System.out.println("Conexion a Base de Datos "+url+" . . . . .Ok");
  34.  
  35.         }
  36.         catch (SQLException ex) {
  37.             System.out.println(ex);
  38.         }
  39.         catch (ClassNotFoundException ex) {
  40.             System.out.println(ex);
  41.         }
  42.     }
  43.  
  44.     public Connection getConexion(){
  45.         return conexion;
  46.     }
  47.  
  48.     public Connection cerrarConexion(){
  49.         try {
  50.             conexion.close();
  51.              System.out.println("Cerrando conexion a "+url+" . . . . . Ok");
  52.         } catch (SQLException ex) {
  53.             System.out.println(ex);
  54.         }
  55.         conexion=null;
  56.         return conexion;
  57.     }
  58. }
Add Comment
Please, Sign In to add comment