简体   繁体   中英

Data not updating in JFrame in Java Netbeans

In my email management system, I am retrieving data from MySQL database into Java netbeans. But it is only showing the data for one person. It should move on to next row on each rs.next(), but it is not seemingly. Following code is written in the "Next" Button, which moves on to the next selection in a JList.

try {
     DefaultListModel model = new DefaultListModel();
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/email management system","root", "ok" );
        Statement stmt = con.createStatement();
         ResultSet rs = stmt.executeQuery("select * from inbox,curlogin where inbox.rmail like curlogin.mail;");
       String fname="";

         list.setSelectedIndex((list.getSelectedIndex()+1));
         rs.next();

            fname = rs.getString("name");
            String sender = rs.getString("First");
        String reciever = rs.getString("recipent");
             String sub = rs.getString("subject");
        String msg = rs.getString("message");
        String mail = rs.getString("Email");
        Date d = rs.getDate("Date");
        Timestamp ls = rs.getTimestamp("Seen");
        Time t = rs.getTime("Time");

          subject.setText(sub);
            message.setText(msg);
            date.setText(""+d);
            time.setText(""+t);
            lseen.setText(""+ls);
            email.setText(mail);


        // TODO add your handling code here:
    } catch (ClassNotFoundException | SQLException ex) {
        Logger.getLogger(inbox.class.getName()).log(Level.SEVERE, null, ex);
    }   

还行吧 在这里它应该显示来自Adesh的消息,但它正在显示来自管理员的消息。

user2642282's answer is technically correct but it is lacking explanation and user2642282 misunderstood what your attempting to do. Your half way to the solution with your code but it requires some changes. Lets start with this:

rs.next() 

What your doing here is accessing the result set and normally you would write your code like this to loop through all the returned results:

ResultSet rs = stmt.executeQuery("your SQL statement here");
while (rs.next()) {
    fname = rs.getString("name");
    //...all your original code here
    email.setText(mail);     
}

Notice how the code is basically exactly the same as your original code except we added the while loop. Normally this would allow you to write out, display, or store each record (email in this case) returned by the query however you want/ need to do something different. So I'll explain what options I think you have next but first here is a link to the Java Docs that will teach everything you need to know about the above code.

Option 1 - Traditional Way
The way most email desktop app's handle this is simply to load all the emails and store them on the file system. This would mean that you need to change your code to load all emails at certain times. Maybe at startup and then every 60 seconds after that check for new messages (usually you should limit checking to no lower than 5 minutes). Then when a user opens a message you load it from its file. When they press next or previous you simply load in the next or previous email from the file system. So your email app is more of a file reader that parses email files and loads their content into your window. You will need to devise a filing system as well. For example naming and saving the emails as their timestamp making it easy to find the next and previous emails. You will also have to deal with deleted emails.

Option 2 - Easy but Bad Practice
Without having to change your code to much you could track which row from the database you read last and run the SQL query every time the user presses the next or previous buttons. You would use the same code your originally posted but add the following:

  • Check for when next() returns false meaning there is no next or previous email
  • Add in a variable that keeps track of the last row you read from in the database
  • Change your SQL query to only return the next or previous row

Option 3 - Hybrid
You could attempt to change your code to the one I showed you and do a hybrid of option 2. What you would do is track the next and previous 10 rows from the database for example. So lets say you last read from row 50. You would run the code I gave you and save all the emails from row 40 to row 60. You have two options for saving the emails:

  • File system which creates a bit more work but saves memory.
  • As objects that could take a lot of memory but is easy and fast.

EDIT - Answer to question from comments
Here is how I imagine your code working using option 2 or 3. Notice that ... means I left out code you already have but would just get in the way of this example:

int lastRowRead = 0; // starts as 0 when the program starts
//...all your original code here
ResultSet rs = stmt.executeQuery("...LIMIT "+ lastRowRead +",20;");
if(rs.next()!=false) {
    fname = rs.getString("name");
    //...all your original code here
    email.setText(mail);     
}

This code is for option 3. The int lastRowRead should be a global variable that you can keep track of and change each time you run a query to get more messages. You will need to figure out how to catch errors, for example trying to query a row number that doesn't exist. I won't help you code this because it is a good learning opportunity. NOTE: In case you are not familiar with SQL LIMIT here is another example:

....LIMIT 40, 20;

What this means is go to row 40 in the table and read the next 20 rows. So it will return emails from 40 to 60. So if you want to go the super easy but not at all efficient route your SQL query could have:

...LIMIT 40, 1;

This will only return the row number requested. So all you have to do is track which row you last read from and add or subtract 1 from it and run the query again to get the next or previous email.

You are missing a loop that iterates over the resultset. The call

rs.next() 

gives you the first row of your SQL result. Try to loop over the results using until rs.next() returns false

two points:

one: if JList current selected value has not changed, when you click next button , you should change your sql , then you can see one's email one by one. at the end of your sql, you should add

select * from inbox,curlogin where inbox.rmail like curlogin.mail limit #{offset} 1

to fix your sql . the offset is a veriable from 0,1,2,3.... it represents one's email from 1 to N

two: you haven't show some event code about JList after selected value was changed. if it was changed , your should get the selected item, and pass it to your sql to filter person's email. so your sql should accept person's name .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM