×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Sidney Castro
Added: Oct 7, 2018 11:00 PM
Views: 3466
Tags: no tags
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package modelo.sql;
  7.  
  8. import modelo.Aluno;
  9. import gui.AlunoGUI;
  10. import java.sql.Connection;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16. import javax.swing.DefaultListModel;
  17.  
  18. /**
  19.  *
  20.  * @author Sidney
  21.  */
  22. public class AlunoSQL {
  23.  
  24.     Connection con;
  25.     Statement stm;
  26.     ResultSet rs;
  27.     String sql;
  28.  
  29.     public AlunoSQL(Connection con) {
  30.         this.con = con;
  31.         try {
  32.             this.stm = con.createStatement();
  33.         } catch (SQLException ex) {
  34.             Logger.getLogger(AlunoGUI.class.getName()).log(Level.SEVERE, null, ex);
  35.         }
  36.     }
  37.  
  38.     public Aluno buscaAluno(int idAluno) {
  39.         sql = "select * from aluno where idaluno=" + idAluno + ";";
  40.         try {
  41.  
  42.             rs = stm.executeQuery(sql);
  43.  
  44.             if (rs.next()) {
  45.                 Aluno al = new Aluno(rs.getInt("idaluno"), rs.getString("nome"));
  46.                 return al;
  47.             }
  48.  
  49.         } catch (SQLException ex) {
  50.             Logger.getLogger(AlunoSQL.class.getName()).log(Level.SEVERE, null, ex);
  51.         }
  52.  
  53.         return null;
  54.     }
  55.  
  56.     public DefaultListModel<Aluno> alunosBuscar(String part) {
  57.        
  58.         sql = "select * from aluno where nome like '" + part + "%';";
  59.        
  60.         DefaultListModel<Aluno> alunos = new DefaultListModel();
  61.         Aluno al;
  62.  
  63.         try {
  64.  
  65.             rs = stm.executeQuery(sql);
  66.  
  67.             while (rs.next()) {
  68.                 al = new Aluno(rs.getInt("idaluno"), rs.getString("nome"));
  69.                 alunos.addElement(al);
  70.             }
  71.             return alunos;
  72.  
  73.         } catch (SQLException ex) {
  74.             Logger.getLogger(AlunoSQL.class.getName()).log(Level.SEVERE, null, ex);
  75.         }
  76.  
  77.         return null;
  78.     }
  79.  
  80.     public boolean alteraNome(int idAluno, String nome) {
  81.         sql = "update aluno set nome='" + nome + "' where idaluno=" + idAluno + ";";
  82.         System.out.println(sql);
  83.         try {
  84.             stm.executeUpdate(sql);
  85.             return true;
  86.         } catch (SQLException ex) {
  87.             Logger.getLogger(AlunoSQL.class.getName()).log(Level.SEVERE, null, ex);
  88.         }
  89.         return false;
  90.     }
  91.  
  92. }
  93.