Guest User

Untitled

a guest
Jan 23rd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. package br.com.eib.Modelo;
  2.  
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JTextField;
  15.  
  16. public class Banco extends JFrame implements ActionListener {
  17. String driver = "com.mysql.jdbc.Driver";
  18. String url = "jdbc:mysql://127.0.0.1/agenda";
  19. String user = "root";
  20. String senha = "root";
  21. Connection conn = null;
  22. private JLabel lbId;
  23. private JLabel lbNome;
  24. private JLabel lbEndereco;
  25. private JLabel lbTelefone;
  26. private JLabel lbEmail;
  27. private JTextField txId;
  28. private JTextField txNome;
  29. private JTextField txEndereco;
  30. private JTextField txTelefone;
  31. private JTextField txEmail;
  32. private JButton btnInserir;
  33. private JButton btnUpdate;
  34. private JButton btnDeletar;
  35. private JButton btnLimpar;
  36.  
  37. public Banco() {
  38. this.setLayout(null);
  39.  
  40. this.lbId = new JLabel();
  41. this.lbId.setBounds(70, 20, 50, 40);
  42. this.lbId.setText("Id: ");
  43.  
  44. this.lbNome = new JLabel();
  45. this.lbNome.setBounds(70, 60, 50, 40);
  46. this.lbNome.setText("Nome: ");
  47.  
  48. this.lbEndereco = new JLabel();
  49. this.lbEndereco.setBounds(70, 100, 80, 40);
  50. this.lbEndereco.setText("Endereço: ");
  51.  
  52. this.lbTelefone = new JLabel();
  53. this.lbTelefone.setBounds(70, 140, 80, 40);
  54. this.lbTelefone.setText("Telefone: ");
  55.  
  56. this.lbEmail = new JLabel();
  57. this.lbEmail.setBounds(70, 180, 50, 40);
  58. this.lbEmail.setText("E-mail: ");
  59.  
  60. this.txId = new JTextField();
  61. this.txId.setBounds(180, 20, 150, 40);
  62.  
  63. this.txNome = new JTextField();
  64. this.txNome.setBounds(180, 60, 150, 40);
  65.  
  66. this.txEndereco = new JTextField();
  67. this.txEndereco.setBounds(180, 100, 150, 40);
  68.  
  69. this.txTelefone = new JTextField();
  70. this.txTelefone.setBounds(180, 140, 150, 40);
  71.  
  72. this.txEmail = new JTextField();
  73. this.txEmail.setBounds(180, 180, 150, 40);
  74.  
  75. this.btnInserir = new JButton();
  76. this.btnInserir.setBounds(20, 220, 80, 40);
  77. this.btnInserir.setText("Inserir");
  78. this.btnInserir.addActionListener(this);
  79.  
  80. this.btnUpdate = new JButton();
  81. this.btnUpdate.setBounds(100, 220, 100, 40);
  82. this.btnUpdate.setText("Atualizar");
  83. this.btnUpdate.addActionListener(this);
  84.  
  85. this.btnDeletar = new JButton();
  86. this.btnDeletar.setBounds(200, 220, 80, 40);
  87. this.btnDeletar.setText("Deletar");
  88. this.btnDeletar.addActionListener(this);
  89.  
  90. this.btnLimpar = new JButton();
  91. this.btnLimpar.setBounds(280, 220, 80, 40);
  92. this.btnLimpar.setText("Limpar");
  93. this.btnLimpar.addActionListener(this);
  94.  
  95. this.add(lbId);
  96. this.add(lbNome);
  97. this.add(lbEndereco);
  98. this.add(lbTelefone);
  99. this.add(lbEmail);
  100. this.add(txId);
  101. this.add(txNome);
  102. this.add(txEndereco);
  103. this.add(txTelefone);
  104. this.add(txEmail);
  105. this.add(btnInserir);
  106. this.add(btnUpdate);
  107. this.add(btnDeletar);
  108. this.add(btnLimpar);
  109.  
  110. this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  111. this.setVisible(true);
  112. this.setBounds(100, 100, 400, 400);
  113. }
  114.  
  115. @Override
  116. public void actionPerformed(ActionEvent arg0) {
  117. if (arg0.getSource().equals(btnLimpar)) {
  118. txNome.setText("");
  119. txEndereco.setText("");
  120. txTelefone.setText("");
  121. txEmail.setText("");
  122. txId.setText("");
  123. return;
  124. }
  125.  
  126. String sql = "";
  127.  
  128. if (arg0.getSource().equals(btnInserir)) {
  129. sql = "insert into contato(nome, endereco,telefone,email)"
  130. + "values('" + txNome.getText() + "','"
  131. + txEndereco.getText() + "','" + txTelefone.getText()
  132. + "','" + txEmail.getText() + "');";
  133. } else if (arg0.getSource().equals(btnUpdate)) {
  134. sql = "update contato set nome = '" + txNome.getText()
  135. + "', endereco = '" + txEndereco.getText()
  136. + "',telefone = '" + txTelefone.getText() + "',email = '"
  137. + txEmail.getText() + "' where id = '" + txId.getText()
  138. + "';";
  139. } else if (arg0.getSource().equals(btnDeletar)) {
  140. sql = "delete from contato where id = " + txId.getText() + ";";
  141. }
  142.  
  143. try {
  144. Class.forName(driver);
  145. conn = DriverManager.getConnection(url, user, senha);
  146. Statement stm = conn.createStatement();
  147.  
  148. stm.executeUpdate(sql);
  149. conn.close();
  150. } catch (ClassNotFoundException e) {
  151. System.out.println(e.getMessage());
  152. } catch (SQLException e) {
  153. System.out.println(e.getMessage());
  154. }
  155. }
  156. }
Add Comment
Please, Sign In to add comment