Write only one field in the database

1

How do I save only one of the fields of this 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);
    
asked by anonymous 08.01.2015 / 19:18

1 answer

2

I would recommend you not to merge two pieces of information into an item of ListBox . But if you insist on this, you can have a solution.

If you do not 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 get only the part that interests me and I fixed the problem of lack of quotes and lack of space before the where denounced by bfavaretto.

I have my doubts that consPCPEDC does not need quotes either. 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);
    
08.01.2015 / 19:48