Escolar Documentos
Profissional Documentos
Cultura Documentos
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 {
} 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) {
}
}
}