package Novo;
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
public class Cada_Grupo extends JFrame
{
private JLabel rotulo1=new JLabel (“Codigo Grupo”);
private JLabel rotulo2=new JLabel (“Descrição”);
private JTextField texto1=new JTextField(10);
private JTextField texto2=new JTextField(10);
private JButton cadastrar=new JButton(“Cadastrar”);
private JButton limpar=new JButton (“Limpar”);
private JButton alterar=new JButton (“Alterar”);
private JButton excluir=new JButton (“Excluir”);
private JButton consultar=new JButton(“Consultar”);
private JButton fechar=new JButton (“Fechar”);
public Cada_Grupo()
{
super("Cadastrar Grupo");
Container tela= getContentPane();
setLayout(null);
rotulo1.setBounds (40,50,100,30);
rotulo2.setBounds(40, 90, 100, 30);
texto1.setBounds(140, 50, 100, 30);
texto2.setBounds(140, 90, 100, 30);
limpar.setBounds(40, 150, 100, 30);
cadastrar.setBounds(40, 190, 100, 30);
alterar.setBounds(40, 230, 100, 30);
consultar.setBounds(160, 150, 100, 30);
excluir.setBounds(160, 190, 100, 30);
fechar.setBounds(160, 230, 100, 30);
tela.add(rotulo1);
tela.add(rotulo2);
tela.add(texto1);
tela.add(texto2);
tela.add(cadastrar);
tela.add(limpar);
tela.add(consultar);
tela.add(alterar);
tela.add(excluir);
tela.add(fechar);
//desabilitar botões
alterar.setEnabled(false);
excluir.setEnabled(false);
consultar.setEnabled(false);
setSize (300,300);
setVisible(true);
setLocationRelativeTo(null);//centraliza a janela
//validar cod grupo p/ ver se ja existe
//cadastros com esse cod
texto1.addFocusListener(new FocusListener(){
public void focusLost (FocusEvent e)
{
if ((e.getSource()==texto1)&&(!texto1.getText().equals("")))//se a ação veio do texto1 e texto 1 for diferente que vazio
{
String url="jdbc:oracle:thin:@127.0.0.1:1521";
String driver="oracle.jdbc.OracleDriver";
String login ="SYSTEM";
String senha="123456";
Connection conexao=null;
try
{
Class.forName (driver);
conexao=DriverManager.getConnection(url,login,senha);
}
catch (java.lang.Exception ex)
{
System.out.println(ex);
}
try
{
Statement sent=conexao.createStatement();
ResultSet result=sent.executeQuery("select * from GRUPO_LOCADORA WHERE CD_GRUPO_LOCADORA="+texto1.getText());
if(result.next())
{
texto1.setText(result.getString ("CD_GRUPO_LOCADORA"));
texto2.setText(result.getString("NM_GRUPOLOCADORA"));
alterar.setEnabled(true);
cadastrar.setEnabled(false);
excluir.setEnabled(true);
consultar.setEnabled(true);
JOptionPane.showMessageDialog(null, "Codigo ja cadastrado"+
"\nDigite novo codigo");
}
else
cadastrar.setEnabled(true);
sent.close();
}
catch(SQLException ex){
System.out.println("Erro de consulta ao sair do campo");
}
}
}
public void focusGained(FocusEvent ex) {//precisa deste metodo
}
});
limpar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
texto1.setText("");
texto2.setText("");
}
});
cadastrar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if (texto1.getText().equals(""))
{
texto1.requestFocus();
JOptionPane.showMessageDialog(null, "Codigo não informado");
}
else if (texto2.getText().equals(""))
{
texto1.requestFocus();
JOptionPane.showMessageDialog(null, "Descrição não informado");
}
else
{
String url="jdbc:oracle:thin:@127.0.0.1:1521";
Connection con;
String query="INSERT INTO GRUPO_LOCADORA VALUES('"+texto1.getText()+"','"+
texto2.getText()+"')";
JOptionPane.showMessageDialog(null, "Comando de Inserção gerado:\n"+query);
Statement sent;
try {
Class.forName("oracle.jdbc.OracleDriver");
}
catch (java.lang.ClassNotFoundException e1)
{
System.err.print("ClassNotFoundException: ");
System.err.println(e1.getMessage());
}
try {
con=DriverManager.getConnection(url,"SYSTEM","123456");
sent=con.createStatement();
int rs=sent.executeUpdate(query);
JOptionPane.showMessageDialog(null, "Grupo cadastrado com sucesso!");
sent.close();
con.close();
} catch (SQLException ex)
{
System.out.println("problemas no cadastro: Registro duplicado ou tabela não existe!");
}
}}});
alterar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
String url="jdbc:oracle:thin:@127.0.0.1:1521";
Connection con;
String query="UPDATE GRUPO_LOCADORA"+" SET NM_GRUPOLOCADORA ='"+texto2.getText()+"'"+"WHERE CD_GRUPO_LOCADORA= '"+texto1.getText().trim()+"'";
Statement stmt;
try {
Class.forName("oracle.jdbc.OracleDriver");
}
catch (java.lang.ClassNotFoundException e1) {
System.err.print("ClassNotFoundException: ");
System.err.println(e1.getMessage());
}
try {
con=DriverManager.getConnection(url,"SYSTEM","123456");
stmt=con.createStatement();
int rs=stmt.executeUpdate(query);
if(rs>0)
{
JOptionPane.showMessageDialog(null, "Conta alterada com sucesso!");
}
else
JOptionPane.showMessageDialog(null, "Grupo não encontrado");
stmt.close();
con.close();
consultar.setEnabled(false);
} catch (SQLException ex) {
System.err.print("SQLException: ");
System.err.println(ex.getMessage());
}
}
});
excluir.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String url="jdbc:oracle:thin:@127.0.0.1:1521";
Connection con;
String query="DELETE FROM GRUPO_LOCADORA WHERE CD_GRUPO_LOCADORA='"+texto1.getText()+"'";
Statement stmt;
try {
Class.forName("oracle.jdbc.OracleDriver");
}
catch (java.lang.ClassNotFoundException e1) {
System.err.print("ClassNotFoundException: ");
System.err.println(e1.getMessage());
}
try
{
con=DriverManager.getConnection(url,"SYSTEM","123456");
stmt=con.createStatement();
int rs=stmt.executeUpdate(query);
if(rs>0)
{
JOptionPane.showMessageDialog(null, "Grupo excluido com sucesso!");
texto1.setText("");
texto2.setText("");
cadastrar.setEnabled(true);
alterar.setEnabled(false);
excluir.setEnabled(false);
}
else
JOptionPane.showMessageDialog(null, "Grupo não encontrado");
stmt.close();
con.close();
consultar.setEnabled(false);
} catch (SQLException ex) {
System.err.print("SQLException: ");
System.err.println(ex.getMessage());
}
}
});
fechar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
dispose();
}
});
consultar.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
new Consulta_Grupo();
}
});
}
public static void main(String[] args) {
Cada_Grupo Cada_Grupo = new Cada_Grupo();
Cada_Grupo.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}