简体   繁体   English

在java中显示jTable中的第一行SQL

[英]show the first SQL row in jTable in java

jTable doesn't show the first row. jTable 不显示第一行。 I created SQL table with list of files in directory and show in jTable in Java.我用目录中的文件列表创建了 SQL 表,并在 Java 中显示在 jTable 中。 I see the table, but I cant see only the first row.我看到了表格,但我只能看到第一行。

Example: In directory I have 20 files.示例:在目录中我有 20 个文件。 code insert into the SQL table all of 20 files, but in jTable show only last 19 files.代码将所有 20 个文件插入到 SQL 表中,但在 jTable 中仅显示最后 19 个文件。

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:创建的 jTable:

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?我正在寻找同样的问题,但我找不到 :( 你能帮我吗?

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.您不需要 while 循环。 The DbUtils method will read all the data from the ResultSet into the TableModel. DbUtils 方法会将 ResultSet 中的所有数据读入 TableModel。

The problem with your code is that the while (rs.next) reads the first row and ignores it.您的代码的问题在于while (rs.next)读取第一行并忽略它。 The the DbUtils method then reads the rest of the data. DbUtils 方法然后读取其余的数据。

So the code should simply be:所以代码应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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