/* * Interface.java * * Created on 11 de Setembro de 2007, 19:55 */ package hotel; import java.awt.Color; import java.sql.SQLException; import java.sql.Statement; import javax.swing.GroupLayout; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JRadioButton; import javax.swing.LayoutStyle; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.text.DefaultFormatterFactory; import javax.swing.text.MaskFormatter; /** * * @author Marcelo * * * */ public class Hospede extends JFrame { /** * */ public Hospede() { frescuras(); netbeans(); botao(); } private DefaultFormatterFactory formato(String mask) { MaskFormatter comFoco = null; try { comFoco = new MaskFormatter(mask); } catch (Exception pe) { } DefaultFormatterFactory factory = new DefaultFormatterFactory(comFoco, comFoco); // return factory; } private void botao() { jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { if (VerificaCPF()) { if (VerificaCampos()) sqladicao(); // Só entra se retornar true } else { JOptionPane.showMessageDialog(null, "Campo do CPF inválido !"); CPF.requestFocus(); } } private boolean VerificaCPF() { // TODO Auto-generated method stub cpf = new Cpf(); if (!(CPF.getText().equals(""))) { if (cpf.validacpf(CPF.getText())) return true; } return false; } private boolean VerificaCampos() { // TODO Auto-generated method stub if (Nome.getText().equals("") || Sobrenome.getText().equals("") || CPF.getText().equals("") || Cidade.getText().equals("")) { JOptionPane.showMessageDialog(null, "Existem campos vazios no formulário"); Nome.requestFocus(); // Coloca focus para o primeiro // campo return false; } else return true; } private void sqladicao() { Conexao sql = new Conexao(); sql.conectar(); query2 = "USE hotel"; query = "INSERT INTO hospede (nome,sobrenome,cidade,cpf) VALUES" + " " + "("; query += "'" + Nome.getText() + "'" + "," + "'" + Sobrenome.getText() + "'" + "," + "'" + Cidade.getText() + "'" + "," + "'" + CPF.getText() + "'" + ")" + ";"; Statement bdquery = sql.getStatement(); try { bdquery.executeUpdate(query2); // Usando a database Hotel bdquery.executeUpdate(query); } catch (SQLException e) { // TODO Auto-generated catch block // JOptionPane.showMessageDialog(null, // "Problema com o Banco de Dados"); switch (e.getErrorCode()) { case 1062: JOptionPane.showMessageDialog(null, "Hospede com mesmo CPF já cadastrado"); } } } }); } private void frescuras() { Icone = new ImageIcon("icone.GIF"); setIconImage(Icone.getImage()); setResizable(false); setTitle(" - Cadastro de Hóspede - "); } // Inicio do codigo gerado private void netbeans() { // Formmated Text Field // Nome = new JFormattedTextField(); Sobrenome = new JFormattedTextField(); Cidade = new JFormattedTextField(); CPF = new JFormattedTextField(); CPF.setFormatterFactory(formato("###########")); CPF.setToolTipText("CPF"); CPF.setBackground(new Color(204, 204, 255)); CPF.setDragEnabled(true); Nome.setFormatterFactory(formato("*************************")); Nome.setToolTipText("Nome"); Nome.setDragEnabled(true); Nome.setBackground(new Color(204, 204, 255)); Sobrenome.setToolTipText("Sobrenome"); Sobrenome.setDragEnabled(true); Sobrenome.setBackground(new Color(204, 204, 255)); Sobrenome.setFormatterFactory(formato("*************************")); Cidade.setToolTipText("Cidade"); Cidade.setDragEnabled(true); Cidade.setBackground(new Color(204, 204, 255)); Cidade.setFormatterFactory(formato("*************************")); // Fim Formmated Text Field // // Botões // Feminino = new JRadioButton(); Feminino.setText("Feminino"); Masculino = new JRadioButton(); Masculino.setText("Masculino"); Masculino.addChangeListener(new ChangeListener() { public void stateChanged(final ChangeEvent e) { Feminino.setSelected(false); } }); // Fim botões // jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); jLabel6 = new javax.swing.JLabel(); jLabel1.setFont(new java.awt.Font("Tempus Sans ITC", 1, 14)); jLabel1.setText("Nome"); jLabel2.setFont(new java.awt.Font("Tempus Sans ITC", 1, 14)); jLabel2.setText("Sobrenome"); jLabel3.setFont(new java.awt.Font("Tempus Sans ITC", 1, 14)); jLabel3.setText("CPF"); jLabel4.setFont(new java.awt.Font("Tempus Sans ITC", 1, 14)); jLabel4.setText("Cidade"); jButton1.setText("Cadastrar !"); jLabel6.setFont(new java.awt.Font("Tempus Sans ITC", 1, 14)); jLabel6.setText("Cadastro de Hospedes"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout( getContentPane()); layout .setHorizontalGroup(layout .createParallelGroup(GroupLayout.Alignment.TRAILING) .addGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup( GroupLayout.Alignment.TRAILING) .addGroup( layout .createSequentialGroup() .addGap( 18, 18, 18) .addGroup( layout .createParallelGroup( GroupLayout.Alignment.LEADING) .addComponent( CPF, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE) .addComponent( Sobrenome, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE) .addComponent( Nome, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE) .addComponent( Cidade, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE) .addComponent( jLabel2, GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE) .addComponent( jLabel1, GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE) .addComponent( jLabel4, GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE))) .addGroup( layout .createSequentialGroup() .addGap( 161, 161, 161) .addGroup( layout .createParallelGroup( GroupLayout.Alignment.TRAILING) .addComponent( jButton1) .addGroup( layout .createParallelGroup( GroupLayout.Alignment.LEADING) .addComponent( Feminino) .addComponent( Masculino)))) .addGroup( layout .createSequentialGroup() .addGap( 18, 18, 18) .addComponent( jLabel3, GroupLayout.DEFAULT_SIZE, 415, Short.MAX_VALUE))) .addContainerGap()).addGroup( layout.createSequentialGroup().addContainerGap( 154, Short.MAX_VALUE).addComponent( jLabel6).addGap(142, 142, 142))); layout.setVerticalGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING) .addGroup( layout.createSequentialGroup().addContainerGap() .addComponent(jLabel6, GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE).addPreferredGap( LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel1).addPreferredGap( LayoutStyle.ComponentPlacement.RELATED) .addComponent(Nome, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addPreferredGap( LayoutStyle.ComponentPlacement.RELATED) .addComponent(Sobrenome, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel3, GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE).addPreferredGap( LayoutStyle.ComponentPlacement.RELATED) .addComponent(CPF, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addGap(13, 13, 13).addComponent(jLabel4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addPreferredGap( LayoutStyle.ComponentPlacement.RELATED) .addComponent(Cidade, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addGap(6, 6, 6).addComponent(Masculino) .addPreferredGap( LayoutStyle.ComponentPlacement.RELATED) .addComponent(Feminino).addGap(20, 20, 20) .addComponent(jButton1).addContainerGap())); getContentPane().setLayout(layout); pack(); } // Fim do codigo gerado // Variaveis private JButton jButton1; private JRadioButton Feminino; private JFormattedTextField Cidade; private JFormattedTextField Nome; private JFormattedTextField Sobrenome; private JRadioButton Masculino; private JLabel jLabel1; private JLabel jLabel2; private JLabel jLabel3; private JLabel jLabel4; private JLabel jLabel6; private ImageIcon Icone; private Cpf cpf; private String query, query2; private static final long serialVersionUID = 1L; private JFormattedTextField CPF; // Fim das Variaveis }