Wednesday 15 September 2010

mysql - Java PreparedStatement Wilcard doesn't work -


I have DDBB with a table user and I want to get the field user_id Trying to search for user_name and user_pass . Therefore, when I run the following query:

  Select 'user_id`, from' user_pass` to 'user' where 'user_name` is like'% aName% ';   

Gives it, i.e. aName = "John":

  + --------- + ------- - --- + | User_id | User_pass | + --------- + ----------- + | 5 | "1234" | + --------- + ----------- +   

OK, so I want to prepare it, because of this I The function has been created:

  private end string QUERY_GETUSERNAME2 = "select user_id`,` user_fname` "'user'" + "WHERE like 'user_fname` ?;"; Private string [] [] getUsersInv (string usrame) {ArrayList & lt; String []> AlAux = new ArrayList (); String [] [] rate = null; Try {PreparedStatement st = _conn.prepareStatement (QUERY_GETUSERNAME2); St.setString (1, "'%" + usrName + "%'"); Results set rs = st.executeQuery (); While (rsnext ()) {string [] asAux = {String.valueOf (rs.getInt (1)), rsgetString (2)}; AlAux.add (asAux); } // while} cache (eclipse e) {e.printStackTrace (System.out); } Finally {if (! AlAux.isEmpty ()) {ret = new string [alAux.size ()] [alAux.get (0). Length]; For (int i = 0; i & lt; alAux.size (); i ++) rate [i] = alAux.get (i); } // fi} return strike; }   

As you can see, the function returns the string [] [], so I check in the previous function if the return is not empty:

  Public Zero Entry Inputs Invalel Modell (JetTable Table, String User) {Default Teleble Model Model = (Default Table Model) Table.GetModel (); String [] [] line = getUsersInv (user); If (line! = Null) model AddRow (line); }   

and this function is a call from the listener for the pocket button:

  Private Zero addUserActionPerformed (java.awt.event.ActionEvent evt) {if (ComboUsers .getSelectedIndex ()! = 0) {New Users (_conn) .insertUsersInvTableModel (_target, String.valueOf (comboUsers.getSelectedItem ())); _target.validate (); _target.repaint (); SetVisible (wrong); }}   

As you can imagine, JDialog is with JComboBox with all the users listed below. AUTO_INCREMENT like table user , there will be some interval (or may be) in user_id , and the only way to build JComboBox without user_id JComboBox For the index.

But, the problem is that whenever I take an item from JComboBox, and I user_id and user_pass based on the selected item (and not Same index), ResultSet is always tap.

Any thoughts? Thank you.

replaced

  st.setString (1, "'%' ' + UsrName + "% '");  
  st.setString (1, "%" + usrName + "%") with   

;

Single quotes are automatically added by created locations with quotation marks % usrname% instead of the string '% usrname%'

No comments:

Post a Comment