Put variable into SQL Statement (Java/SQL) -
so have 2 tabels, student , klas_student described below.
student
create table if not exists studenten( studentenummer varchar(7) check (studentenummer ~'[0-9a-za-z]{7}'), }
klas
create table if not exists klas( student varchar(7) references studenten (studentenummer) on delete cascade not null, klas text not null references klas (naam_id) on delete cascade not null );
as can see in klas_student student references studentnumber in table studenten. in java need take studentenummer studenten en put in klas_student.
to execute query , take studentenummer studenten.
sql = "insert studenten " + "values ('1559335')"; statement2.executeupdate(sql);
then want take value , :
string studentnummer = result.getstring(1).tostring(); sqlstudentoklas = "insert klas " + "values (studentnummer)"; statement2.executeupdate(sqlstudentoklas); conn.commit();
however in sqlstudenttoklas, doesnt take studentnummer(result.getstring(1))string argument.
anybody how pass result.getstring(1) argument/variable in values?
thanks in advance.
c#:
you using '@' passing parameter query string in ado.net
string studentnummer = result.getstring(1).tostring(); sqlstudentoklas = "insert klas " + "values (@studentenummer)"; command.parameters.addwithvalue("@list", studentenummer); statement2.executeupdate(sqlstudentoklas); conn.commit();
and use tutorial :the c# station ado.net tutorial
java jdbc:
string studentnummer = result.getstring(1).tostring(); sqlstudentoklas = "insert klas " + "values (?)"; sqlstudentoklas.setstring(1, studentenummer); statement2.executeupdate(sqlstudentoklas); conn.commit();
and use tutorial :named parameters preparedstatement
Comments
Post a Comment