Search code examples
javaswingjtablesql-server-2014-express

show the first SQL row in jTable in java


jTable doesn't show the first row. I created SQL table with list of files in directory and show in jTable in Java. I see the table, but I cant see only the first row.

Example: In directory I have 20 files. code insert into the SQL table all of 20 files, but in jTable show only last 19 files.

My code:

public void ba() throws SQLException{       
     String dirPath = "c:/Users/hajdukri/Desktop/Source folder";
    File dir = new File(dirPath);

    File[] files = dir.listFiles();


      String sqll = "SELECT * FROM t1;";
            st = con.prepareStatement(sqll);
            rs = st.executeQuery();
      String sql2 = "DELETE FROM t1";      
            st = con.prepareStatement(sql2);
            st.executeUpdate();

    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm kk");
    java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());

    if (files.length == 0){
        System.out.println("The directory is empty");
    } else {
        for (File aFile : files) {
            int fileslength = 0;
            String S = null; 

            if(aFile.length()>1024 || (aFile.length()<(1024*1024))){
                fileslength =(int) (aFile.length()/1024);
                S = "KB";
            }    
            if(aFile.length()>(1024*1024)){
                fileslength =(int) (aFile.length()/(1024*1024));
                S = "MB";
            }
            if(aFile.length()<1024){
                fileslength =(int) (aFile.length());
                S = "B";
            }
            String createDate = sdf.format(aFile.lastModified());
            String remark = "Remark";

            String sql = "INSERT INTO t1 (date,login,name,size,time,action) VALUES ('"+createDate+"'"+","+"'"+userName+"'"+","+"'"+aFile.getName()+"'"+","+"'"+fileslength+S+"'"+","+"'"+sdf.format(aFile.lastModified())+"'"+","+"'"+remark+"')";
            st = con.prepareStatement(sql);
            st.executeUpdate(); 
        }
  }
  try {         
        String sql = "SELECT * FROM t1";
        st = con.prepareStatement(sql);
        rs = st.executeQuery();
        while(rs.next())
                 {
                     TableModel model = DbUtils.resultSetToTableModel(rs);
                     jTable1.setModel(model);
                     jTable1.scrollRectToVisible(jTable1.getCellRect(jTable1.getRowCount()-1, 0, true));
                 }
  } catch (SQLException ex) {
             Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
         }
  }

jTable created:

jTable1.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {
            {null, null, null, null, null, null},
            {null, null, null, null, null, null},
            {null, null, null, null, null, null},
            {null, null, null, null, null, null},
            {null, null, null, null, null, null},
            {null, null, null, null, null, null},
            {null, null, null, null, null, null},
        },
        new String [] {
            "Date", "Login", "Name", "Size", "File", "Action"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Byte.class, java.lang.Object.class, java.lang.String.class
        };
        boolean[] canEdit = new boolean [] {
            false, false, false, false, false, false
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    jTable1.setEditingColumn(1);
    jTable1.setEditingRow(1);
    jTable1.setSelectionBackground(new java.awt.Color(0, 153, 51));
    jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            jTable1MouseClicked(evt);
        }
    });

How can I set visible the first row too? I'm looking for same problems but I can't find :( Could you help me please?


Solution

  • while(rs.next())
    {
        TableModel model = DbUtils.resultSetToTableModel(rs);
        jTable1.setModel(model);
        jTable1.scrollRectToVisible(jTable1.getCellRect(jTable1.getRowCount()-1, 0, true));
    }
    

    You don't need a while loop. The DbUtils method will read all the data from the ResultSet into the TableModel.

    The problem with your code is that the while (rs.next) reads the first row and ignores it. The the DbUtils method then reads the rest of the data.

    So the code should simply be:

    rs = st.executeQuery();
    TableModel model = DbUtils.resultSetToTableModel(rs);
    ...