lunes, 21 de septiembre de 2015

Mi clase java Postgres

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package abbir;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author Administrador
 */
public class Postgres {
//DATOS PARA LA CONEXION

    private final String database = "abbir";
    private final String hostname = "localhost";
    private final String port = "5432";
    private final String user = "postgres";
    private final String password = "1234";
    private final String gestor = "postgresql";
    private final String url = "jdbc:" + gestor + "://" + hostname + ":" + port + "/" + database;
    private Connection conectar = null;

    //Constructor de clase que se conecta a la base de datoS
    public Connection Postgres() {
        try {
            Class.forName("org.postgresql.Driver");
            conectar = DriverManager.getConnection(url, user, password);
            System.out.println("Conectado a la base de datos [ " + this.database + " " + this.gestor + "]");
        } catch (ClassNotFoundException | SQLException e) {
            System.err.println(e.getMessage());
        }
        return conectar;
    }

    Connection getConnection() {
        return conectar;
    }

    void desconectar() {
        conectar = null;
        System.out.println("La conexion a la  base de datos " + database + " a terminado");
    }
}

No hay comentarios:

Publicar un comentario