Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.eib.Modelo;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- public class Banco extends JFrame implements ActionListener {
- String driver = "com.mysql.jdbc.Driver";
- String url = "jdbc:mysql://127.0.0.1/agenda";
- String user = "root";
- String senha = "root";
- Connection conn = null;
- private JLabel lbId;
- private JLabel lbNome;
- private JLabel lbEndereco;
- private JLabel lbTelefone;
- private JLabel lbEmail;
- private JTextField txId;
- private JTextField txNome;
- private JTextField txEndereco;
- private JTextField txTelefone;
- private JTextField txEmail;
- private JButton btnInserir;
- private JButton btnUpdate;
- private JButton btnDeletar;
- private JButton btnLimpar;
- public Banco() {
- this.setLayout(null);
- this.lbId = new JLabel();
- this.lbId.setBounds(70, 20, 50, 40);
- this.lbId.setText("Id: ");
- this.lbNome = new JLabel();
- this.lbNome.setBounds(70, 60, 50, 40);
- this.lbNome.setText("Nome: ");
- this.lbEndereco = new JLabel();
- this.lbEndereco.setBounds(70, 100, 80, 40);
- this.lbEndereco.setText("Endereço: ");
- this.lbTelefone = new JLabel();
- this.lbTelefone.setBounds(70, 140, 80, 40);
- this.lbTelefone.setText("Telefone: ");
- this.lbEmail = new JLabel();
- this.lbEmail.setBounds(70, 180, 50, 40);
- this.lbEmail.setText("E-mail: ");
- this.txId = new JTextField();
- this.txId.setBounds(180, 20, 150, 40);
- this.txNome = new JTextField();
- this.txNome.setBounds(180, 60, 150, 40);
- this.txEndereco = new JTextField();
- this.txEndereco.setBounds(180, 100, 150, 40);
- this.txTelefone = new JTextField();
- this.txTelefone.setBounds(180, 140, 150, 40);
- this.txEmail = new JTextField();
- this.txEmail.setBounds(180, 180, 150, 40);
- this.btnInserir = new JButton();
- this.btnInserir.setBounds(20, 220, 80, 40);
- this.btnInserir.setText("Inserir");
- this.btnInserir.addActionListener(this);
- this.btnUpdate = new JButton();
- this.btnUpdate.setBounds(100, 220, 100, 40);
- this.btnUpdate.setText("Atualizar");
- this.btnUpdate.addActionListener(this);
- this.btnDeletar = new JButton();
- this.btnDeletar.setBounds(200, 220, 80, 40);
- this.btnDeletar.setText("Deletar");
- this.btnDeletar.addActionListener(this);
- this.btnLimpar = new JButton();
- this.btnLimpar.setBounds(280, 220, 80, 40);
- this.btnLimpar.setText("Limpar");
- this.btnLimpar.addActionListener(this);
- this.add(lbId);
- this.add(lbNome);
- this.add(lbEndereco);
- this.add(lbTelefone);
- this.add(lbEmail);
- this.add(txId);
- this.add(txNome);
- this.add(txEndereco);
- this.add(txTelefone);
- this.add(txEmail);
- this.add(btnInserir);
- this.add(btnUpdate);
- this.add(btnDeletar);
- this.add(btnLimpar);
- this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- this.setVisible(true);
- this.setBounds(100, 100, 400, 400);
- }
- @Override
- public void actionPerformed(ActionEvent arg0) {
- if (arg0.getSource().equals(btnLimpar)) {
- txNome.setText("");
- txEndereco.setText("");
- txTelefone.setText("");
- txEmail.setText("");
- txId.setText("");
- return;
- }
- String sql = "";
- if (arg0.getSource().equals(btnInserir)) {
- sql = "insert into contato(nome, endereco,telefone,email)"
- + "values('" + txNome.getText() + "','"
- + txEndereco.getText() + "','" + txTelefone.getText()
- + "','" + txEmail.getText() + "');";
- } else if (arg0.getSource().equals(btnUpdate)) {
- sql = "update contato set nome = '" + txNome.getText()
- + "', endereco = '" + txEndereco.getText()
- + "',telefone = '" + txTelefone.getText() + "',email = '"
- + txEmail.getText() + "' where id = '" + txId.getText()
- + "';";
- } else if (arg0.getSource().equals(btnDeletar)) {
- sql = "delete from contato where id = " + txId.getText() + ";";
- }
- try {
- Class.forName(driver);
- conn = DriverManager.getConnection(url, user, senha);
- Statement stm = conn.createStatement();
- stm.executeUpdate(sql);
- conn.close();
- } catch (ClassNotFoundException e) {
- System.out.println(e.getMessage());
- } catch (SQLException e) {
- System.out.println(e.getMessage());
- }
- }
- }
Add Comment
Please, Sign In to add comment