Você está na página 1de 7

import java.awt.

Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class CadastroFilmes {

public static void main(String args[]) {


// Cria janela
JFrame janela = new TelaCadastroFilmes();
}
}
class TelaCadastroFilmes extends JFrame implements ActionListener {
JLabel etiqueta1, etiqueta2, etiqueta3, etiqueta4, etiqueta5, etiqueta6, eti
queta7;
JButton botao1, botao2, botao3, botao4, botao5;
JTextField tfCodigo, tfNome, tfGenero, tfProdut, tfDatcom, tfAnopro, tfTemdu
r;
ResultSet rs;
PreparedStatement pst;
Connection cn;
DateFormat df;
public TelaCadastroFilmes() {

// Cria formatador de datas


df = new SimpleDateFormat("dd/MM/yyyy");
df.setLenient(false);
// Cria as etiquetas
etiqueta1 = new JLabel("Código:");
etiqueta2 = new JLabel("Título:");
etiqueta3 = new JLabel("Gênero:");
etiqueta4 = new JLabel("Produtora:");
etiqueta5 = new JLabel("Data de Compra:");
etiqueta6 = new JLabel("Ano de Produção:");
etiqueta7 = new JLabel("Tempo de Duração:");
// Cria caixas de texto
tfCodigo = new JTextField(10);
tfNome = new JTextField(35);
tfGenero = new JTextField(10);
tfProdut = new JTextField(15);
tfDatcom = new JTextField(8);
tfAnopro = new JTextField(5);
tfTemdur = new JTextField(5);
// Cria botões
botao1=new JButton("Inserir");
botao2=new JButton("Atualizar");
botao3=new JButton("Excluir");
botao4=new JButton("Localizar");
botao5=new JButton("Novo");
// Configura cor de fundo dos botões
botao1.setBackground(new Color(180,180,250));
botao2.setBackground(new Color(180,180,250));
botao3.setBackground(new Color(180,180,250));
botao4.setBackground(new Color(180,180,250));
botao5.setBackground(new Color(180,180,250));
// registra manipulador de eventos para os botões
botao1.addActionListener(this);
botao2.addActionListener(this);
botao3.addActionListener(this);
botao4.addActionListener(this);
botao5.addActionListener(this);
// define a posição e o tamanho dos componentes
// (gerenciador de layout desligado)
botao1.setBounds(10, 220, 90, 20);
botao2.setBounds(110, 220, 90, 20);
botao3.setBounds(210, 220, 90, 20);
botao4.setBounds(310, 220, 90, 20);
botao5.setBounds(410, 220, 90, 20);
etiqueta1.setBounds(10, 10, 150, 20);
etiqueta2.setBounds(10, 40, 150, 20);
etiqueta3.setBounds(10, 70, 150, 20);
etiqueta4.setBounds(10, 100, 150, 20);
etiqueta5.setBounds(10, 130, 150, 20);
etiqueta6.setBounds(10, 160, 150, 20);
etiqueta7.setBounds(10, 190, 150, 20);
tfAnopro.setBounds(180, 160, 100, 20);
tfCodigo.setBounds(180, 10, 100, 20);
tfDatcom.setBounds(180, 130, 100, 20);
tfGenero.setBounds(180, 70, 100, 20);
tfNome.setBounds(180, 40, 320, 20);
tfProdut.setBounds(180, 100, 320, 20);
tfTemdur.setBounds(180, 190, 100, 20);
// adiciona etiquetas e botões ao painel
this.add(etiqueta1);
this.add(tfCodigo);
this.add(etiqueta2);
this.add(tfNome);
this.add(etiqueta3);
this.add(tfGenero);
this.add(etiqueta4);
this.add(tfProdut);
this.add(etiqueta5);
this.add(tfDatcom);
this.add(etiqueta6);
this.add(tfAnopro);
this.add(etiqueta7);
this.add(tfTemdur);
this.add(botao1);
this.add(botao2);
this.add(botao3);
this.add(botao4);
this.add(botao5);
// Configura janela principal
this.setLayout(null);
this.setSize(515, 280);
this.setLocation(150, 150);
this.setTitle("Cadastramento de Filmes");
this.setResizable(false);
this.setVisible(true);
//this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Cria manupulador de eventos da janela
WindowListener x = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
if (!cn.isClosed()) cn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
System.exit(0);
}
};
// Registra manipulador de eventos
this.addWindowListener(x);
try {
// Cria conexão com o banco de dados
cn = (new Conexao()).getConexao();
// Recupera dados do banco e posiciona no primeiro
// registro por ordem alfabética
pst = cn.prepareStatement("SELECT * FROM Filmes ORDER BY Finome");
rs = pst.executeQuery();
if (rs.next()) atualizaCampos();

} catch(ClassNotFoundException ex) {
JOptionPane.showMessageDialog(this,
"O Driver para acesso ao banco não foi encontrado!",
null, JOptionPane.ERROR_MESSAGE);
System.exit(1);
} catch(SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,
"Erro na recuperação dos dados!",
null, JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}
public void actionPerformed(ActionEvent e) {
// Rotina para novo registro
if (e.getSource() == botao5) {
limpaCampos();
}
// Rotina de inclusão
if (e.getSource() == botao1) {
try {
String SQL = "INSERT INTO Filmes (Ficodigo, Finome, Figenero," +
"Fiprodut, Fidatcom, Fianopro, Fitemdur) Values ( ?, ?,
?, ?, ?, ?, ? )";
pst = cn.prepareStatement(SQL);
pst.setString(1, tfCodigo.getText());
pst.setString(2, tfNome.getText());
pst.setString(3, tfGenero.getText());
pst.setString(4, tfProdut.getText());
pst.setTimestamp(5, new Timestamp(df.parse(tfDatcom.getText()).g
etTime()));
pst.setString(6, tfAnopro.getText());
pst.setString(7, tfTemdur.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(this,"Gravação realizada com sucesso!"
);
limpaCampos();
} catch(SQLException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(this,
"Falha na gravação dos dados!",
null, JOptionPane.ERROR_MESSAGE);
System.exit(1);

} catch(ParseException ex) {
JOptionPane.showMessageDialog(this,
"Data Inválida",
null, JOptionPane.ERROR_MESSAGE);
}
}
// Rotina de Alteração
if (e.getSource() == botao2) {
try {
String SQL = "UPDATE Filmes SET " +
"Finome = ?, " +
"Figenero = ?, " +
"Fiprodut = ?, " +
"Fidatcom = ?, " +
"Fianopro = ?, " +
"Fitemdur = ? " +
"WHERE Ficodigo = ?";
pst = cn.prepareStatement(SQL);
pst.setString(7, tfCodigo.getText());
pst.setString(1, tfNome.getText());
pst.setString(2, tfGenero.getText());
pst.setString(3, tfProdut.getText());
pst.setTimestamp(4, new Timestamp(df.parse(tfDatcom.getText()).g
etTime()));
pst.setString(5, tfAnopro.getText());
pst.setString(6, tfTemdur.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(this,"Alteração realizada com sucesso!
");
} catch(SQLException ex) {
JOptionPane.showMessageDialog(this,
"Falha na alteração dos dados!",
null, JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
System.exit(1);
} catch(ParseException ex) {
JOptionPane.showMessageDialog(this,
"Data Inválida",
null, JOptionPane.ERROR_MESSAGE);
}
}
// Rotina para exclusão
if (e.getSource() == botao3) {

try {
String SQL = "SELECT Ficodigo, Finome FROM Filmes WHERE Ficodigo
= ?";
pst = cn.prepareStatement(SQL);
pst.setString(1, tfCodigo.getText());
pst.executeQuery();
String texto = "";
try {
if (!rs.next()) {
JOptionPane.showMessageDialog(this, "Filme nao Encontrad
o!",
null, JOptionPane.ERROR_MESSAGE);
limpaCampos();
} else {
texto = "Deletar o Filme: "+ rs.getString("finome");
int n = JOptionPane.showConfirmDialog(this, texto, null,
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
SQL = "DELETE FROM Filmes Where Ficodigo = ?";
pst = cn.prepareStatement(SQL);
pst.setString(1, tfCodigo.getText());
pst.executeUpdate();
JOptionPane.showMessageDialog(this, "Exclusão realizad
a com sucesso",
null, JOptionPane.INFORMATION_MESSAGE);

limpaCampos();
}
}
} catch(SQLException ex1) {
JOptionPane.showMessageDialog(this,
"Filme nao cadastrado!",
null, JOptionPane.INFORMATION_MESSAGE);
}
} catch(SQLException ex){
JOptionPane.showMessageDialog(this, "Falha na Exclusão dos Dados",
null, JOptionPane.INFORMATION_MESSAGE);
}
}

// Rotina de localização
if (e.getSource() == botao4) {
try {
String SQL = "SELECT * FROM Filmes WHERE Ficodigo = ?";
pst = cn.prepareStatement(SQL);
pst.setString(1, tfCodigo.getText());
rs = pst.executeQuery();
if (!rs.next()) {
JOptionPane.showMessageDialog(this, "Filme nao Encontrado!",
null, JOptionPane.ERROR_MESSAGE);
} else {
atualizaCampos();
}
} catch(SQLException ex) {
JOptionPane.showMessageDialog(this, "Falha na recuperação dos dados!
",
null, JOptionPane.ERROR_MESSAGE);
}
}
}
public void limpaCampos() {
tfCodigo.setText("");
tfNome.setText("");
tfGenero.setText("");
tfProdut.setText("");
tfDatcom.setText("");
tfAnopro.setText("");
tfTemdur.setText("");
}
public void atualizaCampos() {
try {
tfCodigo.setText(rs.getString("Ficodigo"));
tfNome.setText(rs.getString("Finome"));
tfGenero.setText(rs.getString("Figenero"));
tfProdut.setText(rs.getString("Fiprodut"));
tfDatcom.setText(df.format(rs.getDate("Fidatcom")));
tfAnopro.setText(rs.getString("Fianopro"));
tfTemdur.setText(rs.getString("Fitemdur"));
} catch(SQLException ex) {
}
}
}

Você também pode gostar