Ajudem-me plis! Preciso mudar um Status [RESOLVIDO]

5 respostas
rafaelaalves

Estou precisando mudar o status.. de um livro
a parti do código dele... Alguém HELP-ME
?
Ai vai os codigos

package biblioteca.gui;
import biblioteca.dao.LivroDAO;
import biblioteca.model.DevolucaoLivroVO;
import biblioteca.model.Emprestimo;
import biblioteca.model.Livro;
import biblioteca.service.LivroService;
import javax.swing.JOptionPane;
/**
 *
 * @author rafaelabrito
 */
public class DevolucaoLivro extends javax.swing.JFrame {

    /** Creates new form DevolucaoLivro */
    public DevolucaoLivro() {
        initComponents();
    }
    private DevolucaoLivroVO devolucaolivroVO;
   

    /** Cria novo formulário para verificar a disponibilidade do livro */
  
    public DevolucaoLivro (DevolucaoLivroVO devolucaolivroVO) {
        initComponents();
        this.devolucaolivroVO = devolucaolivroVO;
      
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        codigo = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        voltar = new javax.swing.JButton();
        limpar = new javax.swing.JButton();
        consultar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel6.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jLabel6.setText("Devolução do Livro");

        codigo.setFont(new java.awt.Font("Times New Roman", 2, 14));
        codigo.setForeground(new java.awt.Color(204, 204, 255));

        jLabel9.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel9.setText("Código do Livro:");

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 14)); // NOI18N
        jLabel5.setText("Informe abaixo apenas o código do livro:");

        voltar.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        voltar.setText("Voltar");
        voltar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                voltarActionPerformed(evt);
            }
        });

        limpar.setFont(new java.awt.Font("Times New Roman", 1, 18));
        limpar.setText("Limpar");
        limpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                limparActionPerformed(evt);
            }
        });

        consultar.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        consultar.setText("Devolver");
        consultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                consultarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(consultar)
                        .addGap(41, 41, 41)
                        .addComponent(limpar)
                        .addGap(36, 36, 36)
                        .addComponent(voltar))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(93, 93, 93)
                        .addComponent(jLabel6))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(codigo, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(39, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5)
                .addGap(55, 55, 55)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(codigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 75, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(consultar)
                    .addComponent(limpar)
                    .addComponent(voltar))
                .addGap(53, 53, 53))
        );

        pack();
    }// </editor-fold>                        

private void voltarActionPerformed(java.awt.event.ActionEvent evt) {                                       
        new PaginaInicial().setVisible(true);
        dispose();
}                                      

private void limpaDados() {
        codigo.setText(null);
    }
private void limparActionPerformed(java.awt.event.ActionEvent evt) {                                       
        
    limpaDados();
}                                      

private void consultarActionPerformed(java.awt.event.ActionEvent evt) {                                          

        Livro livro = new Livro();

        try{
            String Scodigo = codigo.getText();

            if(Scodigo.equals("")){
                JOptionPane.showMessageDialog(null, "O campo CÓDIGO não pode ser nulo!");
                return;
            }

            if(Scodigo.length()>10){
                JOptionPane.showMessageDialog(null, "O campo CÓDIGO não pode conter mais de 10 dígitos!");
                codigo.setText(null);
                return;
            }

            livro.setCodigo(Scodigo);
        }

        catch (NumberFormatException e){
                 JOptionPane.showMessageDialog(null,"O campo CÓDIGO deve conter apenas números!" );
                 codigo.setText(null);
                 return;
        }

        //Consulta:
        try{
            String Status = LivroService.getInstance().selectStatus(livro.getCodigo());
            if (Status.equals("DISPONIVEL")){
                JOptionPane.showMessageDialog(null,"O livro solicitado não está alocado!"); 
                dispose(); // 
            }
            if(Status.equals("INDISPONIVEL")){
               int aux; 
               String Status1 = "DISPONIVEL"; // era p mudar o status aqui... 
               aux= Integer.parseInt(Status1);
                 dispose();
               JOptionPane.showMessageDialog(null,"Status Alterado com Sucesso");
            }

        }

        catch(Exception e){
            JOptionPane.showMessageDialog(null,"Erro : " + e.getMessage());
        }
}                                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DevolucaoLivro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DevolucaoLivro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DevolucaoLivro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DevolucaoLivro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new DevolucaoLivro().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    private javax.swing.JTextField codigo;
    private javax.swing.JButton consultar;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JButton limpar;
    private javax.swing.JButton voltar;
    // End of variables declaration                   
}
Ai tem o codigo do livro
public String AtualizaStatus(int codigo) throws SQLException, ClassNotFoundException {
                String Status = null;
                Connection conn = null;
                PreparedStatement pstmt = null;
                ResultSet rs = null;
		try {
                        conn = getConnection();
			pstmt = super.prepareStatement( conn, UPDATE_STATUS );
			pstmt.setInt(1, codigo);
			rs = pstmt.executeQuery();
			rs.setFetchSize( 1 );

			if( rs.next() ) {
				Status = rs.getString("STATUS");
			} else {
				throw new SQLException("CÓDIGO não encontrado!");
			}
		} finally {
			release(conn, pstmt, rs);
		}
		return Status;}

}
metodo acima criado no DAO apenas para atualizar! =/ abaixo o metodo do service...
}
public String AtualizaStatus(int codigo) throws LivroException {
        
       
       String Status = null;
       
		LivroDAO livroDAO = null;
		try {
			livroDAO = mysqlDAOFactory.getLivroDAO();
			livroDAO.getConnection();
			Status = livroDAO.AtualizaStatus( codigo );
		} catch (SQLException e) {
			throw new LivroException("CÓDIGO não encontrado! ", e);
		} catch (Exception e) {
			throw new LivroException("CÓDIGO não encontrado!", e);

                }
return Status;
}
}

Axo que esses dois tem que fazer algo no meu código de devolução... lá em cima onde eu não consigo fazer nada!...
=/
Se alguém poder me ajudar!

5 Respostas

A

vc tem que fazer algo assim,

String Status = LivroService.getInstance().selectStatus(livro.getCodigo());  
            if (Status.equals("DISPONIVEL")){  
                JOptionPane.showMessageDialog(null,"O livro solicitado não está alocado!");   
                dispose(); //   
            }  
            else if(Status.equals("INDISPONIVEL")){  
				LivroService.getInstance().alterarStatus(livro.getCodigo());
               Status = "DISPONIVEL"; 
                 dispose();  
               JOptionPane.showMessageDialog(null,"Status Alterado com Sucesso");  
            }

t+

rafaelaalves

eu tinha feito isso…
o négocio é que ele tem que mudar o status no campo… estou fazendo isso errado, mas não sei como fazer.

rafaelaalves
  • banco, não no campo, sim no banco --’
A

tipo vc vai ter um metodo igual a esse

LivroService.getInstance().alterarStatus(livro.getCodigo(), "DISPONIVEL");//ou INDISPONIVEL

so fazer isso

t+

rafaelaalves

Certissimo. =)

Criado 7 de novembro de 2011
Ultima resposta 19 de dez. de 2011
Respostas 5
Participantes 2