Results 1 to 9 of 9
  1. #1
    hash004 is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default [SOLVED] jtable mysql search

    Hi guys,

    I've followed this tutorial to create an application:
    A simple MySQL client in NB

    Everything works fine. I was trying to add a search feature by allowing the user to input the search in a textfield and choose an option from a combo box to search in a specific column then hit the search button.


    but it doesn't seem to work. rest of the class is pretty much coded the same as the tutorial.

    Any clues or help anyone can give me? Would really appreciate the help
    Last edited by hash004; 09-10-2011 at 01:52 PM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: jtable mysql search

    May I suggest the perfect poor men's debugger: System.out.println( ... ). Sprinkle in a couple of those statements and check if the correct values are fetched from the database and if they populate your table.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    hash004 is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default Re: jtable mysql search

    I did:
    Java Code:
    System.out.println(searchStr);
    I ran the program and searched for 4 in the project ID i got the following:
    Java Code:
    SELECT * FROM Projects WHERE Project_ID LIKE '%4%';
    I typed in asda instead this time and chose Project Name from the combo box but i got the following statement:
    Java Code:
    SELECT * FROM Projects WHERE Project_ID LIKE '%%';
    I tried it several times. The first query will always be right but after that the combo box value in the statement will stay as Project_ID no matter what I choose from the actual combo box and the value between the % will always be empty after the first go and all of this with the jtable still unaffected. I'm really confused :S
    Last edited by hash004; 09-07-2011 at 08:36 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: jtable mysql search

    Aren't you forgetting a call to removeAll()? See the code in the tutorial to see what I mean.

  5. #5
    hash004 is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default Re: jtable mysql search

    Quote Originally Posted by Fubarable View Post
    Aren't you forgetting a call to removeAll()? See the code in the tutorial to see what I mean.
    I'm pretty sure that code only removes the input values in the textfields so they are empty right after you press the button. Didn't think it was necessary for the search button.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: jtable mysql search

    Quote Originally Posted by hash004 View Post
    I'm pretty sure that code only removes the input values in the textfields so they are empty right after you press the button. Didn't think it was necessary for the search button.
    No, what your code and that code both do is to call the initComponents() method which I'm guessing re-populates the GUI with components, and I wonder if this can't be done if you don't first remove the already visible ones. But I can't say for sure since I don't use NetBeans to generate my GUI code.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: jtable mysql search

    Don't create new Swing components.

    Instead update the existing table. First you create a new TableModel from the ResultSet, then you simply invoke:

    Java Code:
    table.setModel( newlyCreatedModel );

  8. #8
    hash004 is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default Re: jtable mysql search

    Quote Originally Posted by camickr View Post
    Don't create new Swing components.

    Instead update the existing table. First you create a new TableModel from the ResultSet, then you simply invoke:

    Java Code:
    table.setModel( newlyCreatedModel );
    thanks for that. I went back to the exact same code from the tutorial to test out my search button and it worked. the problem I'm still having is the mysql query its generating is incorrect after the first go.

    For example if I type in N in the search field and choose name from the combo box and press the search button the following mysql statement is generated:

    Java Code:
    SELECT * FROM cars WHERE name LIKE '%N%';
    the jtable will show the relevant rows. if I perform a second search, putting Audi in the search field and choosing vendor from the combo box. when I press search the following statement is generated:

    Java Code:
    SELECT * FROM cars WHERE carid LIKE '%%';
    instead of vendor its searching in the carid and the search value is missing. I figured out the reason why its choosing carid is because its the first option in the combo box but I don't know why its doing that. And I have no clue why the search value is missing from the statement.

    I really appreciate the help :)
    Last edited by hash004; 09-10-2011 at 01:53 PM.

  9. #9
    hash004 is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default Re: jtable mysql search

    Problem solved. I commented out
    Java Code:
    initComponents();
    that actually created a new combo box to select from (thats the way the logic is working in my head lol)

    thanks for all the help guys really appreciate it ^_^

Similar Threads

  1. Jtable and Mysql
    By ehsan in forum AWT / Swing
    Replies: 1
    Last Post: 08-13-2011, 10:14 PM
  2. Search box JSP mySQL easy command
    By slipdiegoknot in forum New To Java
    Replies: 8
    Last Post: 03-23-2011, 08:19 AM
  3. Jtable gives error when connecting to mySQL
    By Mv.c9 in forum NetBeans
    Replies: 0
    Last Post: 03-07-2011, 05:11 PM
  4. jtable and report from mysql database
    By cisilej in forum NetBeans
    Replies: 0
    Last Post: 02-18-2011, 08:40 AM
  5. Confusion between Lucene and MySQL fulltext search
    By rehinagrasia in forum Advanced Java
    Replies: 2
    Last Post: 01-10-2011, 10:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •