Results 1 to 4 of 4
  1. #1
    LearnJava82 is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Problem fetching data from database using JList

    Hello everyone,
    This is my first Thread, and I hope that I will learn alot from this place! I'm learning Java on my own, and I'm pushing myself to be a better Java programmer. Anyway, I'm creating a bank application where I can use the search text field to find customers in the database(I'm using MySQL database). When I type in a letter to search a customer by its last name, the application does fetches the data from the database and shows the results in a JList. In my case, I have a total of 5 records in my database, which 4 last names starts with the letter "S" and one starts with the letter "M". Once the rows or results are in the JList, I can click on one row and automatically fills the first name, last name, city, and etc text filds with the proper information. However, there are two issues that I'm having:

    1. In my case, if I search a last name by the letter "S," my JList will show only 4 records from the database. When I select one row from the JList, it should automatically fills all the text fields with the proper information. However, if I select the first row in the JList, my text fields will always fill with the information with the last row in the JList. It doesn't matter which row I select in the JList, the text fields will alway fills the information with the last row.
    2. If I search the letter "S", I get 4 records, which is correct. However, when the application is still running, and I decided to search the letter "M," I get one record, which is also correct, but if I try to search the letter "S" again, I get only one record. Why? It should be 4.


    Java Code:
    // Implement List Selection Listener
    private class listener implements ListSelectionListener{
    			@Override
    			public void valueChanged(ListSelectionEvent event) {
    						try	{
    							connect = DriverManager.getConnection(url,user,password);
    							SQLStatement = connect.createStatement();
    							String select = "SELECT * FROM customerinfo WHERE LastName LIKE '"+ txtSearch.getText().trim() + "%'";
    							rows = SQLStatement.executeQuery(select);
    
    							if(!event.getValueIsAdjusting()){
    								
    								while(rows.next()){
    										setTextFields(rows.getInt("CustomerID"), rows.getString("FirstName"), rows.getString("LastName"), rows.getString("MiddleInitial"),
    											rows.getString("Street"), rows.getString("City"), rows.getString("State"), rows.getInt("ZipCode"), rows.getInt("Phone"),
    											rows.getString("Email"));
    								}
    							}
    		
    						} catch(Exception ex) {
    							JOptionPane.showMessageDialog(null, "Cannot get field values");
    						}
    			}
    				
    		}
    I know it is the rows.next() that is causing issue number 1.

    Here is what I'm trying to accomplish:
    Google Image Result for http://docs.oracle.com/javaee/5/tutorial/doc/figures/dukesbank-appClient.gif

    Thanks in advance!
    Sorry if the thread is long!

  2. #2
    LearnJava82 is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problem fetching data from database using JList

    Don't you use ListSelectionListener for selecting data in a JList instead of using ActionListener? I want to be able to select one row at a time, and when I select one row in the JList, it should fire an event, and fill all the text field. What do you mean "database code should be in a background thread." Does this website has another section/forum for databases?

  3. #3
    LearnJava82 is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: Problem fetching data from database using JList

    Nevermind, I see the JDBC forum...sorry!

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

    Default Re: Problem fetching data from database using JList

    The main part of my answer was wrong, so I am deleting it. Sorry. The part about the database code should be in a background thread is more of a Swing issue than a database issue. You should look into using a SwingWorker for this. You can find out more about SwingWorker here: Concurrency in Swing.

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2012, 10:54 AM
  2. Replies: 1
    Last Post: 01-25-2012, 05:10 PM
  3. java insert data into database problem
    By chyeeqi in forum NetBeans
    Replies: 3
    Last Post: 03-29-2010, 01:36 AM
  4. Replies: 3
    Last Post: 03-19-2010, 12:59 AM
  5. problem with fetching data
    By payal.mitra86 in forum JDBC
    Replies: 0
    Last Post: 05-24-2008, 01:29 PM

Posting Permissions

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