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();  } 

spaces enter image description here

trim function enter image description here

eliminates leading , trailing spaces:

  1. using string's trim() method

    textfield_1.gettext().trim();

  2. using regex

    textfield_1.gettext().replaceall("^\\s+|\\s+$", "");


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -