java - How to restrict JtextField from putting extra spaces in database? -
whenever add values in database through jtexfield saves spaces in sql database there way restrict wide spaces , save text added in textfield?
try { string query = "insert items (item_name, category_id, item_price, item_description, stock) values (?,?,?,?,?) ;"; preparedstatement pst = con.preparestatement(query); pst.setstring(1, textfield_1.gettext()); pst.setstring(2, textfield_2.gettext()); pst.setstring(3, textfield_3.gettext()); pst.setstring(4, textfield_4.gettext()); pst.setstring(5, textfield_5.gettext()); pst.execute(); joptionpane.showmessagedialog(null, "data saved"); pst.close(); } catch (exception e) { e.printstacktrace(); }
eliminates leading , trailing spaces:
using string's trim() method
textfield_1.gettext().trim();
using regex
textfield_1.gettext().replaceall("^\\s+|\\s+$", "");
Comments
Post a Comment