Advertisement
Guilherme-Valle

BD Java Fran

Dec 4th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. Funcionario:
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. package projetobanco;
  8.  
  9. /**
  10. *
  11. * @author francisleide
  12. */
  13. public class Funcionario {
  14. private String nome;
  15. private int id;
  16. private String matricula;
  17. private int idade;
  18.  
  19.  
  20. public Funcionario(){
  21.  
  22. }
  23. public String getNome() {
  24. return nome;
  25. }
  26.  
  27. public void setNome(String nome) {
  28. this.nome = nome;
  29. }
  30.  
  31. public int getId() {
  32. return id;
  33. }
  34.  
  35. public void setId(int id) {
  36. this.id = id;
  37. }
  38.  
  39. public String getMatricula() {
  40. return matricula;
  41. }
  42.  
  43. public void setMatricula(String matricula) {
  44. this.matricula = matricula;
  45. }
  46.  
  47. public int getIdade() {
  48. return idade;
  49. }
  50.  
  51. public void setIdade(int idade) {
  52. this.idade = idade;
  53. }
  54.  
  55. public Funcionario(String nome, String matricula, int idade) {
  56. this.nome = nome;
  57. this.matricula = matricula;
  58. this.idade = idade;
  59. }
  60.  
  61.  
  62. }
  63.  
  64. Banco:
  65.  
  66. /*
  67. * To change this license header, choose License Headers in Project Properties.
  68. * To change this template file, choose Tools | Templates
  69. * and open the template in the editor.
  70. */
  71. package projetobanco;
  72.  
  73. import java.sql.Connection;
  74. import java.sql.DriverManager;
  75. import java.sql.ResultSet;
  76. import java.sql.Statement;
  77. import java.util.ArrayList;
  78.  
  79. /**
  80. *
  81. * @author francisleide
  82. */
  83. public class Conexao {
  84. public Connection conexao = null;
  85. public Statement declaracao = null;
  86. public ResultSet resultados = null;
  87.  
  88. public void conectar(){
  89. String servidor = "jdbc:mysql://localhost:3306/empresa";
  90. String usuario = "root";
  91. String senha = "";
  92. String driver = "com.mysql.jdbc.Driver";
  93.  
  94. try{
  95. Class.forName(driver);
  96. this.conexao = DriverManager.getConnection(servidor, usuario, senha);
  97. this.declaracao = conexao.createStatement();
  98. }catch (Exception e){
  99. System.out.println("Erro: "+e.getMessage());
  100. }
  101.  
  102. }
  103.  
  104. public boolean testaConexao(){
  105. if(this.conexao == null){
  106. return false;
  107. }
  108. return true;
  109. }
  110.  
  111. public void insereFuncionario(Funcionario funcionario){
  112. String sql = "INSERT INTO FUNCIONARIO (NOME, MATRICULA, IDADE) VALUES ('"
  113. + funcionario.getNome()+"', '"+funcionario.getMatricula()+
  114. "', "+funcionario.getIdade()+");";
  115.  
  116. try{
  117. declaracao.executeUpdate(sql);
  118.  
  119. }catch(Exception e){
  120. System.out.println("Erro: "+e.getMessage());
  121. }
  122. }
  123.  
  124.  
  125. public ArrayList<Funcionario> listarFuncionarios(){
  126. ArrayList<Funcionario> funcionarios = new ArrayList<>();
  127. String sql = "SELECT * FROM FUNCIONARIO";
  128. try{
  129. this.resultados=this.declaracao.executeQuery(sql);
  130. Funcionario funcionario;
  131. while(resultados.next()==true){
  132. funcionario = new Funcionario();
  133. funcionario.setId(resultados.getInt("idfuncionario"));
  134. funcionario.setIdade(resultados.getInt("idade"));
  135. funcionario.setNome(resultados.getString("nome"));
  136. funcionario.setMatricula(resultados.getString("matricula"));
  137. funcionarios.add(funcionario);
  138.  
  139. }
  140. }catch (Exception e){
  141. System.out.println("Erro: "+e.getMessage());
  142. }
  143.  
  144.  
  145. return funcionarios;
  146. }
  147.  
  148. }
  149.  
  150. Main:
  151.  
  152. -----Classe main------------------------
  153. package projetobanco;
  154.  
  155. import java.util.ArrayList;
  156. import javax.swing.JOptionPane;
  157.  
  158. /**
  159. *
  160. * @author francisleide
  161. */
  162. public class ProjetoBanco {
  163.  
  164. /**
  165. * @param args the command line arguments
  166. */
  167. public static void main(String[] args) {
  168. Conexao con = new Conexao();
  169. con.conectar();
  170. if(con.testaConexao()){
  171. System.out.println("Conectou !! o/");
  172. }else{
  173. System.out.println("Deu ruim!");
  174. }
  175. /* String nome=JOptionPane.showInputDialog("Informe o nome");
  176. String matricula = JOptionPane.showInputDialog("Infome a matricula");
  177. int idade = Integer.parseInt(JOptionPane.showInputDialog("Informe a idade"));
  178.  
  179. Funcionario f1 = new Funcionario(nome, matricula, idade);
  180. con.insereFuncionario(f1);*/
  181. ArrayList<Funcionario> funcionarios = con.listarFuncionarios();
  182. for(int i=0; i<funcionarios.size(); i++){
  183. System.out.println("Id: "+funcionarios.get(i).getId());
  184. System.out.println("Nome: "+funcionarios.get(i).getNome());
  185. System.out.println("Matricula: "+funcionarios.get(i).getMatricula());
  186. System.out.println("Idade: "+funcionarios.get(i).getIdade());
  187. }
  188.  
  189. int idEditar = Integer.parseInt(JOptionPane.showInputDialog("Informe o id do objeto a ser editado no banco!"));
  190. Funcionario f1 = new Funcionario();
  191. f1.setId(idEditar);
  192. f1.setIdade(Integer.parseInt(JOptionPane.showInputDialog("Informe a nova idade")));
  193. f1.setNome(JOptionPane.showInputDialog("Informe o novo nome"));
  194. f1.setMatricula(JOptionPane.showInputDialog("Informe a nova matrícula"));
  195. con.editaFuncionario(f1);
  196. /*f1.setId(idExcluir);
  197. con.excluiFuncionario(f1);*/
  198.  
  199.  
  200.  
  201. }
  202.  
  203.  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement