Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.elias.alunos.conexao;
- import br.elias.alunos.exception.ErroSistema;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- /**
- *
- * @author elias
- */
- public class FabricaConexao {
- private static Connection conexao;
- private static final String URL_CONEXAO = "jdbc:mysql://localhost/sistema-alunos";
- private static final String USUARIO = "root";
- private static final String SENHA = "br354895";
- public static Connection getConexao() throws ErroSistema {
- if(conexao == null){
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conexao = DriverManager.getConnection(URL_CONEXAO, USUARIO, SENHA);
- } catch (SQLException ex) {
- throw new ErroSistema("Não foi possível conectar ao banco de dados!", ex);
- } catch (ClassNotFoundException ex) {
- throw new ErroSistema("O driver do banco de dados não foi encontrado!", ex);
- }
- }
- return conexao;
- }
- public static void fecharConexao() throws ErroSistema{
- if(conexao != null){
- try {
- conexao.close();
- conexao = null;
- } catch (SQLException ex) {
- throw new ErroSistema("Erro ao fechar conexão com o banco de dados!", ex);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement