Last active
December 12, 2023 02:00
-
-
Save joaomosantos/a9fdedcc026f23c530b3 to your computer and use it in GitHub Desktop.
Java - Conexão com o banco PostgreSQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Bibliotecas > Adicionar Biblioteca > Driver JDBC do PostgreSQL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dba; | |
import java.sql.*; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.swing.JOptionPane; | |
public class Connect { | |
public Statement stm; // Responsavel por preparar e realizar pesquisas no banco de dados; | |
public ResultSet rs; // Responsavel por armazenar o resultado de um pesquisa passada para o statement; | |
private String driver = "org.postgresql.Driver"; | |
private String caminho = "jdbc:postgresql://localhost:5432/meubanco"; // O "meubanco" representa a minha database | |
private String usuario = "postgres"; // Usuario default | |
private String senha = "123"; | |
public Connection conexao; // Responsavel por realizar a conexão com o banco de dados; | |
public void conectar() { // Metodo responsavel por realizar a conexão; | |
try { | |
System.setProperty("jdbc.Drivers", driver); // Seta a propriedade do driver de conexão; | |
conexao = DriverManager.getConnection(caminho, usuario, senha); // Realiza a conexão com o banco; | |
JOptionPane.showMessageDialog(null, "Conectado com sucesso!", "Banco de Dados", JOptionPane.INFORMATION_MESSAGE); | |
} catch (SQLException ex) { | |
Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex); | |
JOptionPane.showMessageDialog(null, "Erro de conexão!\nERRO: " + ex.getMessage(), "Banco de Dados", JOptionPane.INFORMATION_MESSAGE); | |
} | |
} | |
public void desconectar() { // Metodo responsavel por fechar a conexão | |
try { | |
conexao.close(); // Fechar conexão | |
JOptionPane.showMessageDialog(null, "Conexão fechada com sucesso!", "Banco de Dados", JOptionPane.INFORMATION_MESSAGE); | |
} catch (SQLException ex) { | |
Logger.getLogger(Connect.class.getName()).log(Level.SEVERE, null, ex); | |
JOptionPane.showMessageDialog(null, "Erro ao fechar a conexão!\nERRO: " + ex.getMessage(), "Banco de Dados", JOptionPane.INFORMATION_MESSAGE); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
PreparedStatement pst = conexao.conexao.prepareStatement("insert into tb_pessoa (nome, sobrenome) values (?,?)"); | |
pst.setString(1, "max"); | |
pst.setString(2, "oliveira"); | |
pst.executeUpdate(); | |
JOptionPane.showMessageDialog(null, "Salvo com Sucesso!"); | |
} catch (SQLException ex) { | |
JOptionPane.showMessageDialog(null, "Erro na inserção!\nErro: " + ex.getMessage()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package aula; | |
import dba.Connect; | |
public class Aula { | |
public static void main(String[] args) { | |
Connect conexao = new Connect(); | |
conexao.conectar(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment