What facility does java provide for getting input from the command line for the executeUpdate() method in SQL -
scanner input = new scanner(system.in); system.out.println("enter staff name: "); string staffname = input.nextline(); system.out.println("enter department name: "); string department =input.nextline(); stmt.executeupdate("insert staff (staffname,department) " + "values " + staffname,department);
this gives me error , asks me check sql manual.is possible?
it possible, since happened. strings must enclosed in quotes in sql:
insert foo (bar, baz) values ('hello', 'world');
learn use prepared statements instead of string concatenations, make queries work, , not subject sql injection attacks:
preparedstatement stmt = connection.preparestatement("insert staff (staffname, department) values (?, ?)"); stmt.setstring(1, staffname); stmt.setstring(2, department); stmt.executeupdate();
using above, don't need mess quotes nor escape them inside strings.
Comments
Post a Comment