Package dao

Source Code of dao.Conexao

package dao;

/**
* Criar uma instancia da classe
*   ConnectionClass getConection = new ConnectionClass();
*
* Criar um objeto Conection (import java.sql.Connection;)
*   Connection con = null;
try {

//Usar nossa inst�ncia para definir um target de conex�o para o Conection
con = getConection.getSqlConnection();
//Executar os comandos sql atrav�s de uma Declara��o (Statement)
Statement st = con.createStatement(); (import java.sql.Statement;)
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;

import util.DriverConexao;

import controller.operating.GerenciadorConfiguracoesDriverXml;

public class Conexao {

    private static Connection conexao = null;
    private static String driverName,  driverUrl,  driverServerName,  driverDataBase,  driverUserName,  driverPassword;
    private static DriverConexao drive;

    public static Connection getSqlConnection() {
        try {
            getStringConfigureConnection();
            if (conexao == null) {
                //Class.forName("org.gjt.mm.mysql.Driver");
                Class.forName(driverName);
                driverUrl = "jdbc:mysql://" + driverServerName + "/" + driverDataBase;
                //conexao = DriverManager.getConnection("jdbc:mysql://localhost/modulorelacionamento", "root", "root");       
                conexao = DriverManager.getConnection(driverUrl, driverUserName, driverPassword);
            }
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(
                    null,
                    e.getMessage(),
                    "Atenção !!!", JOptionPane.ERROR_MESSAGE);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        return conexao;
    }

    public static Connection getDirectConection() {
        try {
            getStringConfigureConnection();
            if (conexao == null) {
                //Class.forName("org.gjt.mm.mysql.Driver");
                Class.forName(driverName);
                driverUrl = "jdbc:mysql://" + driverServerName;//+ "/" + driverDataBase;
                conexao = DriverManager.getConnection("jdbc:mysql://localhost/modulorelacionamento", "root", "root");       
                //conexao = DriverManager.getConnection(driverUrl, driverUserName, driverPassword);
            }
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(
                    null,
                    " Não foi possível estabelecer conexão com a base de dados",
                    "Atenção !!!", JOptionPane.ERROR_MESSAGE);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(
                    null,
                    " Não foi possível estabelecer conexão",
                    "Atenção !!!", JOptionPane.ERROR_MESSAGE);
        }
        return conexao;
    }

    public static void getStringConfigureConnection() {
        GerenciadorConfiguracoesDriverXml driverConfig = new GerenciadorConfiguracoesDriverXml();
        drive = driverConfig.getConfigureConnection();       
        // org.gjt.mm.mysql.Driver;jdbc:mysql://localhost/Desafio;localhost;Desafio;root;root;
        driverName = drive.getName();
        driverServerName = drive.getServerName();
        driverDataBase = drive.getDataBase();
        driverUserName = drive.getUserName();
        driverPassword = drive.getPassword();
    }
}
TOP

Related Classes of dao.Conexao

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.