Sub-string

2 respostas
gctsukasa
Primeiro
package locadora;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
 import java.sql.*;

public class CadastroCliente extends JInternalFrame{
        
  //declaração dos componentes   
  private JLabel lblNome;
  private JLabel lblData;
  private JLabel lblEnd;
  private JLabel lblN;
  private JLabel lblBairro;
  private JLabel lblCEP;
  private JLabel lblCidade;
  private JLabel lblCPF;
  private JLabel lblStado;
  private JTextField txtCodigo;
  private static JTextField txtNome;
  private JTextField txtData;
  private JTextField txtEnd;
  private JTextField txtN;
  private JTextField txtBairro;
  private JTextField txtCEP;
  private JTextField txtCPF;
  private JTextField txtCidade;
  private JButton buttonCadastrar;
  private JButton buttonCancelar;
  private javax.swing.JComboBox Estado;
  public CadastroCliente(){
      
      super("Cadastro", true, true, false, true);
      initComponents();
  }
   

 
  //adição de componentes no programa
private void initComponents(){
    
    lblNome = new JLabel();
    lblData = new JLabel();
    lblEnd = new JLabel();
    lblN = new JLabel();
    lblBairro=new JLabel();
    lblCEP= new JLabel();
    lblCidade=new JLabel();
    lblStado =new JLabel();
    lblCPF= new JLabel();
    txtCodigo = new JTextField();
    txtNome = new JTextField();
    txtData = new JFormattedTextField();
    txtEnd = new JTextField();
    txtN = new JTextField();
    txtBairro = new JTextField();
    txtCEP= new JTextField();
    txtCidade= new JTextField();
    Estado=new JComboBox();
        Estado.addItem("");
        Estado.addItem("AC");
        Estado.addItem("AL");
        Estado.addItem("AM");
        Estado.addItem("AP");
        Estado.addItem("BA");
        Estado.addItem("CE");
        Estado.addItem("DF");
        Estado.addItem("ES");
        Estado.addItem("GO");
        Estado.addItem("MA");
        Estado.addItem("MG");
        Estado.addItem("MS");
        Estado.addItem("PA");
        Estado.addItem("PB");
        Estado.addItem("PE");
        Estado.addItem("PI");
        Estado.addItem("PR");
        Estado.addItem("RJ");
        Estado.addItem("RN");
        Estado.addItem("RO");
        Estado.addItem("RR");
        Estado.addItem("RS");
        Estado.addItem("SC");
        Estado.addItem("SE");
        Estado.addItem("SP");
        Estado.addItem("TO");             
        
    txtCPF= new JTextField();
    buttonCadastrar = new JButton();
    buttonCancelar = new JButton();

    this.setResizable(false);
    
    this.getContentPane().setLayout(null);
    
     
//campo de label
lblNome.setText("Nome:");
lblNome.setBounds(5, 20, 70, 20);
this.add(lblNome);

lblData.setText("Data nasc:");
lblData.setBounds(350,20,70,20);
this.add(lblData);
lblEnd.setText("Endereço:");
lblEnd.setBounds(5, 50, 70, 20);
this.add(lblEnd);
lblN.setText("Nº:");
lblN.setBounds(270,50,70,20);
this.add(lblN);
lblBairro.setText("Bairro:");
lblBairro.setBounds(350,50,70,20);
this.add(lblBairro);
lblCEP.setText("CEP:");
lblCEP.setBounds(5,80,70,20);
this.add(lblCEP);
lblCidade.setText("Cidade:");
lblCidade.setBounds(190,80,50,20);
this.add(lblCidade);
lblStado.setText("Estado:");
lblStado.setBounds(410,80,50,20);
this.add(lblStado);
lblCPF.setText("CPF:");
lblCPF.setBounds(5,110,50,20);
this.add(lblCPF);

//area de texto
   this.add(txtCodigo);
   txtNome.setBounds(90,20,250,20);
   this.add(txtNome);
      
        try{

                javax.swing.text.MaskFormatter format_textField = new

                javax.swing.text.MaskFormatter("####-##-##");

                txtData = new javax.swing.JFormattedTextField(format_textField);

        }catch (Exception e){}
   txtData.setBounds(430,20,90,20);
   this.add(txtData);
   txtEnd.setBounds(90,50,175,20);
   this.add(txtEnd);
      
        try{

                javax.swing.text.MaskFormatter format_textField = new

                javax.swing.text.MaskFormatter("####");

                txtN = new javax.swing.JFormattedTextField(format_textField);

        }catch (Exception e){}
   
   txtN.setBounds(290,50,50,20);
   this.add(txtN);
   txtBairro.setBounds(400,50,120,20);
   this.add(txtBairro);
      
        try{

                javax.swing.text.MaskFormatter format_textField = new

                javax.swing.text.MaskFormatter("#####-###");

                txtCEP = new javax.swing.JFormattedTextField(format_textField);

        }catch (Exception e){}
   
   txtCEP.setBounds(90,80,80,20);
   this.add(txtCEP);
   txtCidade.setBounds(240,80,160,20);
   this.add(txtCidade);
   Estado.setBounds(460,80,60,20);
   this.add(Estado);
   
        try{

                javax.swing.text.MaskFormatter format_textField = new

                javax.swing.text.MaskFormatter("#########-##");

                txtCPF = new javax.swing.JFormattedTextField(format_textField);

        }catch (Exception e){}
   
   txtCPF.setBounds(90,110,100,20);
   this.add(txtCPF);
    //botões   
   buttonCadastrar.setText("Cadastrar");
   buttonCadastrar.setBounds(350,110,100,30);
   this.add(buttonCadastrar);
   this.setVisible(true);
   buttonCancelar.setText("Cancelar");
   buttonCancelar.setBounds(450,110,100,30);
   this.add(buttonCancelar);
   this.setVisible(true);
   
      buttonCadastrar.addActionListener(new ActionListener()
   {
       public void actionPerformed(ActionEvent e){
          Banco();
       }
   });;
}
   
        public void Banco() {
                  
       String url = "jdbc:mysql://192.168.0.69/locadora";
     Connection con;
     String createString;
     createString ="insert into cliente (nome,data,endereco,numero,bairro,cep,cidade,estado,cpf) values ('"+txtNome.getText()+"','"+txtData.getText()+"','"+txtEnd.getText()+"','"+txtN.getText()+"','"+txtBairro.getText()+"','"+txtCEP.getText()+"','"+txtCidade+"','"+Estado.getSelectedItem()+"','"+txtCPF.getText()+"')";
                   
     Statement stmt;
     
    try {
       Class.forName("com.mysql.jdbc.Driver");       
    } catch(java.lang.ClassNotFoundException e) {
       System.err.print("ClassNotFoundException: ");
       System.err.println(e.getMessage());
    }
    try {
        
       con = DriverManager.getConnection(url, "admin", "cateto");
      stmt = con.createStatement();
       stmt.execute(createString);

       stmt.close();
      con.close();
    } catch(SQLException ex) {
       ex.printStackTrace();
    }  
  } 
}

o meu campo de cep tem um mask que depos de 5 numeros e insere um hífem.
porém o MySql não aceita esse traço eu gostaria de saber como eu faria para remover esse hífem logo depois que eu apertar o "buttonCadastrar" me falaram que é um tal de sub-string só que eu não entendi direito como que funciona.

2 Respostas

Kassiane_Pretti

Será q isso ajuda:

http://www.guj.com.br/posts/list/5284.java

Até \o/

peczenyj

No seu caso vc precisa de um replace(), olha o javadoc da classe String e lembra que a mesma é imutavel :wink:

Criado 21 de julho de 2008
Ultima resposta 21 de jul. de 2008
Respostas 2
Participantes 3