Write only one field to the database

Asked

Viewed 84 times

1

How do I record only one of these fields select?

Statement stmt = con.createStatement();
ResultSet RS = null;
RS = stmt.executeQuery("select codplpag, descricao from pcplpag ORDER BY 1,2"); 

while(RS.next()){  
    //  ListaBox.addItem(RS.getString("codplpag")+" / " + RS.getString("descricao"));
    ListaBox.addItem(RS.getString("codplpag" )+" / " + RS.getString("descricao"));
}  

Update:

int registro = stmt.executeUpdate("update pcpedc set codplpag =" +ListaBox.getSelectedItem().toString()+ 
                    "where numped=" + consPCPEDC);
  • 2

    One space left before WHERE, I wonder if that’s all?

  • worse than not @bfavaretto, it brings me the selected item and everything else, amis when I put to save it saves the description along with the number "codplpag"+ the description "Description" ai it from the error

  • The best thing you can do is use PreparedStatement and avoid creating their query concatenating variables.

  • My advice would be not to use two distinct pieces of information as an item from ListBox. But if you want to insist on this you would have to separate the two data to use only what matters. This codplpag has fixed size guaranteed? What is?

  • has the size of 2 fields

  • how do I separate the data?

  • You can get a substring of the selected item by taking the first characters or counting the separator you used (" /"). But it can get better than that. What is the type/class of ListaBox?

  • What is size of 2 fields? It would be 2 characters?

Show 3 more comments

1 answer

2


I would advise you not to put two pieces of information together in one ListBox. But if you want to insist on this you can have a solution.

If you don’t know the size of the field you can do this:

String codPlPag = ListaBox.getSelectedItem().toString();
int posicaoBarra = codPlPag.indexOf(" /");
if(posicaoBarra != -1){
    int registro = stmt.executeUpdate("update pcpedc set codplpag ='" + 
                       codPlPag.substring(0, posicaoBarra) + "' where numped=" + consPCPEDC);
} else {
    //deu erro
}

I tried to take only the part that matters and I fixed the problem of the lack of quotation marks and lack of space before the where denounced by the bfavaretto.

I have my doubts whether the consPCPEDC also does not need quotes. Under normal conditions should.

If you know the size, if it’s 2 characters guaranteed, you can make it simpler:

String codPlPag = ListaBox.getSelectedItem().toString();
int registro = stmt.executeUpdate("update pcpedc set codplpag ='" + 
                       codPlPag.substring(0, 2) + "' where numped=" + consPCPEDC);

I put in the Github for future reference.

Note that what you are doing is dangerous and unsafe, do not.

  • Really good guy I was really going to be dying in this stuff thank you very much. hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.