[code]import java.sql.;
import javax.swing.;
import java.awt.*;
import java.awt.Event.*;
class ocorrencia extends JFrame implements ActionListener
{
JLabel1 label1,label2,label3,label4,label5;
JButton btGravar, btAlterar, btExcluir, btNovo, btLocalizar, btCancelar;
static JTextField tfCodigo,tfTitulo,tfGenero,tfProdutora,tfDataCompra;
JPanel painel1 = new JPanel();
ResultSet resultSet;
Statement statement;
public static void main ( String args[])
{
JFrame janela = new ocorrencia();
janela.setUndecorated(true);
janela.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
janela.setVisible(true);
}
ocorrencia()
{
painel1.setLayout(new FlowLayout(FlowLayout.LEFT));
label1 = new JLabel1 ("Código ");
label2 = new JLabel1 ("Título ");
label3 = new JLabel1 ("Gênero ");
label4 = new JLabel1 ("Produtora ");
label5 = new JLabel1 ("Data da Compra ");
tfCodigo = new JTextField(10);
tfCodigo.addActionListener(this);
tfTitulo = new JTextField(10);
tfGenero = new JTextField(10);
tfProdutora= new JTextField(15);
tfDataCompra = new JTextField(8);
btGravar = new JButton("Gravar");
btAlterar = new JButton("Alterar");
btExcluir = new JButton("Excluir");
btLocalizar = new JButton("Localizar");
btNovo = new JButton("Novo");
btCancelar = new JButton("Cancelar");
btGravar.setBackground(new Color(180,180,250));
btAlterar.setBackground(new Color(180,180,250));
btExcluir.setBackground(new Color(180,180,250));
btLocalizar.setBackground(new Color(180,180,250));
btNovo.setBackground(new Color(180,180,250));
btCancelar.setBackground(new Color(180,180,250));
btGravar.addActionListener(this);
btAlterar.addActionListener(this);
btExcluir.addActionListener(this);
btLocalizar.addActionListener(this);
btNovo.addActionListener(this);
btCancelar.addActionListener(this);
painel1.add(label1); painel1.add(tfCodigo);
painel1.add(label2); painel1.add(tfTitulo);
painel1.add(label3); painel1.add(tfGenero);
painel1.add(label4); painel1.add(tfProdutora);
painel1.add(label5); painel1.add(btLocalizar);
painel1.add(btNovo); painel1.add(btLocalizar);
painel1.add(btGravar); painel1.add(btAlterar);
painel1.add(btExcluir); painel1.add(btCancelar);
getContentPane().add(painel1);
setTitle("Cadastramento de Filmes ");
setSize(610,120);
setResizable(false);
setBotoes(true,true,false,false,false,false);
carregaResultSet();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==btNovo)
{
limpaCampos();
setBotoes(false,false,true,false,false,true);
return;
}
if (e.getSource()==btGravar)
{
try
{
String sql = " INSERT INTO Filmes (codigo,titulo,genero," + " produtora,datacompra) Values ('" +
tfCodigo.getText() + "','" +
tfTitulo.getText() + "','" +
tfGenero.getText() + "','" +
tfProdutora.getText() + "','" +
tfDataCompra.getText() + "')";
statement.executeUpdate(sql);
JOptionPane.showMessageDialog(null,"Gravação realizada com sucesso! ");
setBotoes(true,true,false,true,true,false);
}
catch (SQLException erro)
{
if (erro.getMessage().equals("General error"))
JOptionPane.showMessageDialog(null,"Filme já cadatrado ! ");
else
JOptionPane.showMessageDialog(null,"Data Inválida! ");
}
carregaResultSet();
}
//final da inclusão
if (e.getSource()==btAlterar)
{
try
{
String sql = "UPDATE Filmes " +
"codigo=" + tfCodigo.getText() + "'," +
"titulo=" + tfTitulo.getText() + "'," +
"genero=" + tfGenero.getText() + "'," +
"produtora=" + tfProdutora.getText() + "'," +
"datacompra=" + tfDataCompra.getText() + "' " +
"WHERE codigo = '" + tfCodigo.getText() + "'";
int r= statement.executeUpdate(sql);
if (r==1)
JOptionPane.showMessageDialog(null,"Alteração realizada com sucesso !");
else
JOptionPane.showMessageDialog(null,"Problemas na alteração! ");
setBotoes(true,true,false,true,true,false);
}
catch (SQLException erro)
{
JOptionPane.showMessageDialog(null,"Data inválida! ");
}
carregaResultSet();
}
//final da alteração
if (e.getSource()==btExcluir)
{
try
{
String sql = " SELECT codigo, titulo FROM Filmes WHERE codigo = '"+tfCodigo.getText()+"')";
resultSet = statement.executeQuery(sql);
String nome = "";
try
{
resultSet.next();
nome = "Deletar o filme: " + resultSet.getString("titulo");
}
catch (SQLException ex1)
{
JOptionPane.showMessageDialog(null,"Filme nao cadastrado! ");
carregaResultSet();
return;
}
int n = JOptionPane.showConfirmDialog(null,nome,"",JOptionPane.YES_OPTION);
if (n==JOptionPane.YES_OPTION)
{
sql = "DELETE FROM Filmes Where codigo = '" + tfCodigo.getText() + "'";
int r = statement.executeUpdate(sql);
if (r==1)
JOptionPane.showMessageDialog(null, "Não foi possível excluir o filme");
limpaCampos();
setBotoes(true,true,false,false,false,false);
}
}
catch (SQLException erro) {}
carregaResultSet();
}
//final da exclusão
if (e.getSource()==btLocalizar || e.getSource()==tfCodigo)
{
try
{
String sql = " SELECT * FROM Filmes Where codigo = '" + tfCodigo.getText() + "'";
resultSet = statement.executeQuery(sql);
resultSet.next();
atualizaCampos();
setBotoes(true,true,false,true,true,false);
}
catch (SQLException erro)
{
JOptionPane.showMessageDialog(null, "Filme nao encontrado! ");
carregaResultSet();
return;
}
}
//final da localização
[b] public void carregaResultSet()[/b] {[quote]fala illegal start of expression
String url = "jdbc:odbc:MeuBanco ";
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver");
Connection minhaConexao = DriverManager.getConnection(url);
statement = minhaConexao.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
resultSet = statement.executeQuery("SELECT * FROM Filmes");
}
catch (ClassNotFoundException erro)
{
System.out.println("Driver JDBC-ODBC não encontrado! ");
}
catch (SQLException erro)
{
System.out.println("Problemas na conexao com a fonte de dados");
}
}
public static void limpaCampos()
{
tfCodigo.setText("");
tfTitulo.setText("");
tfGenero.setText("");
tfProdutora.setText("");
tfDataCompra.setText("");
}
public void atualizaCampos()
{
try
{
tfCodigo.setText(resultSet.getString("codigo"));
tfTitulo.setText(resultSet.getString("titulo"));
tfGenero.setText(resultSet.getString("genero"));
tfProdutora.setText(resultSet.getString("produtora"));
tfDataCompra.setText(resultSet.getString("datacompra"));
}
catch (SQLException erro){ }
}
public void setBotoes(boolean bNovo,boolean BLocaliza,boolean bGravar,boolean BAlterar,boolean BExcluir,boolean bCancelar)
{
btNovo.setEnabled(bNovo);
btLocalizar.setEnabled(bLocalizar);
btGravar.setEnabled(bGravar);
btAlterar.setEnabled(bAlterar);
btExcluir.setEnabled(bExcluir);
btCancelar.setEnabled(bCancelar);
}
}[/code]
