Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to fill values from table to text file when I select the row in table.
But with date value, it failed.
It only shows date in txt_date = 1/1/1970.
How can I do?
Thanks for your help.
Here is my code:
Java
private void tbl_userMouseClicked(java.awt.event.MouseEvent evt) {                                      
        // TODO add your handling code here:
         try 
        {
            int row = tbl_user.getSelectedRow();
            String Table_click = (tbl_user.getModel().getValueAt(row, 0).toString());
            String sql = "select * from Userinfo where Pumid='"+Table_click+"' ";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            if(rs.next())
            {
                String add1 = rs.getString("Pumid");
                txt_pumpid.setText(add1);
                
                Date add2 = rs.getDate("Date");
                txt_date.setDate(add2);
                
                String add3 = rs.getString("Ap_suat");
                txt_ap_suat.setText(add3);
                String add4 = rs.getString("Nhiet_do");
                txt_nhiet_do.setText(add4);
                String add5 = rs.getString("Volumeflow");
                txt_volume.setText(add5);
                String add6 = rs.getString("RotationalSpeed");
                txt_speed.setText(add6);
                
            }
        }
        catch (Exception e) 
        {
            JOptionPane.showMessageDialog(null, e);
        }
        finally
        {
            try {
                rs.close();
                pst.close();
            } 
            catch (Exception e) 
            {
            }
        }
    }                                     
Posted
Comments
Richard Deeming 10-Feb-15 12:26pm    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Richard MacCutchan 10-Feb-15 13:16pm    
The only way to work this out is to step through the code with your debugger and check the values of the returned variables at each step.
Pandu Rang 12-Feb-15 8:36am    
What you expecting in date value? is you expect the date to be in some specific format?
Member 10390715 13-Feb-15 2:49am    
Thank you. I format in "d/MM/yyyy".
For example,I add date to database is "13/2/2015" and I want retrieve this value in textfile when I click mouse in table. But when I use this code:

Date add2 = rs.getDate("Date");
txt_date.setDate(add2);

I only receive date is 1/1/1970. I don't know how to fix it.



Pandu Rang 13-Feb-15 4:05am    
Which date you are using, are you using java.util.Date or java.sql.Date, please see the below java docs for java.sql.Date:
http://docs.oracle.com/javase/7/docs/api/java/sql/Date.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900