java - Inserting values from textfield in to database -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i trying insert textfields data database. insert data table compte_utilisateur
, i've 2 comboboxes : first groupe containing names of groups (alias libelle_groupe
) in database, , second statut containing status values (alias valeur
) in database.
i want insert db user ( alias compte_utilisateur
) , need id_groupe
of name of group selected in combobox , id_statut
of status selected in combobox.
i've tried this, i'm having exception : java.lang.nullpointerexception
the code :
resultset valeur1= stmtlistelivre.executequery("select id_groupe groupe libelle_groupe='"+combo_name.getselecteditem().tostring()+"'"); resultset valeur2= stmtlistelivre1.executequery("select id_statut statut valeur='"+jcombobox1.getselecteditem().tostring()+"'"); number val1 = ((number) valeur1.getobject(1)).intvalue(); number val2 = ((number) valeur2.getobject(1)).intvalue(); string requete="insert compte_utilisateur(id_compte, nom, prenom, matricule, id_groupe, id_statut) values (?,?,?,?,?,?)"; pst=maconnexion.obtenirconnexion().preparestatement(requete); pst.setstring(1, jtidf.gettext()); pst.setstring(2, jtnom.gettext()); pst.setstring(3, jtprenom.gettext()); pst.setstring(4, jtmatricule.gettext()); pst.setstring(5, val1.tostring()); pst.setstring(6, val2.tostring()); pst.execute();
there number of problems this, lets start seems obvious...
resultset valeur1= stmtlistelivre.executequery("select id_groupe groupe libelle_groupe='"+combo_name.getselecteditem().tostring()+"'"); resultset valeur2= stmtlistelivre1.executequery("select id_statut statut valeur='"+jcombobox1.getselecteditem().tostring()+"'"); number val1 = ((number) valeur1.getobject(1)).intvalue(); number val2 = ((number) valeur2.getobject(1)).intvalue();
neither valeur1
or valeur2
have been primed first (possible) record.
when resultset
returned, it's cursor (which record it's pointing to) set before first record. need check if resultset
has next
record or not. doing will, if possible, move cursor next record (or return false
indicating it's reached end of resultset
)
number val1 = null; number val2 = null; if (valeur1.next()) { val1 = ((number) valeur1.getobject(1)).intvalue(); } if (valeur2.next()) { val2 = ((number) valeur2.getobject(1)).intvalue(); }
Comments
Post a Comment