java - Syntax Error in insert upto? MSAccess -
i trying insert data user enters jtextfield msaccess database. when try execute sql statement error stating syntax error in insert statement. checked sql statement , tried few different things cannot seem find kind of syntax error.
conn = connect.connectdb(); string sql = "insert team (" +"teamid," +"teamcity," +"teammascot," + "values("+txtid.gettext()+ ",'"+txtname.gettext()+"','"+txtaddress.gettext()+"')" ; try{ pst = conn.preparestatement(sql); pst.execute(); joptionpane.showmessagedialog(null, "entry " + txtid.gettext() + " saved"); updatejtable(); //conn.close(); } catch(exception e){ joptionpane.showmessagedialog(null, e); }
the error extra comma , no closing parenthesis before keyword values
string sql = "insert team (" +"teamid," +"teamcity," +"teammascot," // <<== here, change comma closing parenthesis
by way, statement vulnerable sql injection
. can avoid if parameterized values. eg,
string sql = "insert team (teamid,teamcity,teammascot) values (?, ?, ?, ?)" pst = conn.preparestatement(sql); pst.setstring(1, txtid.gettext()); pst.setstring(2, txtname.gettext()); pst.setstring(3, txtaddress.gettext());