0

Sorry for my bad English, I only can speak a little, and I cannot find answers in Chinese websites for my question.

This is the complete table, it has 9 rows.

But my program worked like this, only query for 6 rows.

This is my code, where is the mistake?

{
    jScrollPane1 = new JScrollPane();
    getContentPane().add(jScrollPane1);
    jScrollPane1.setBounds(37, 79, 517, 247);
    {
        Vector rowData,columnNames;
        PreparedStatement ps=null;
        Connection ct=null;
        ResultSet rs = null;

        columnNames=new Vector();
        columnNames.add("配件供应商");
        columnNames.add("配件名称");
        columnNames.add("配件价格");
        columnNames.add("入库时间");
        columnNames.add("出库时间");

        rowData=new Vector();
        try
        {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/honsun?&serverTimezone=UTC";
            String user="root";
            String password="root";
            ct=DriverManager.getConnection(url,user,password);
            ps=ct.prepareStatement("select * from parts");
            rs=ps.executeQuery();
            while(rs.next())
            {
                Vector hang=new Vector();
                hang.add(rs.getString(1));
                hang.add(rs.getString(2));
                hang.add(rs.getString(3));
                hang.add(rs.getDate(4));
                hang.add(rs.getDate(5));
                rowData.add(hang);
            }
            rs.close();
            ps.close();
            ct.close();
        }
        catch (Exception e)
        {e.printStackTrace();}
        /* finally
        {                       
            try {
                if(rs!=null)
                {
                rs.close();
                }
                if(ps!=null){
                    ps.close();
                }
                if(ct!=null){
                    ct.close();
                }
            } catch (SQLException e) 
            {
                e.printStackTrace();
            }
        }  */
        jTable1 = new JTable(rowData,columnNames);          
        jScrollPane1.setViewportView(jTable1);
        jScrollPane1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • After the code `rowData.add(hang);` add a line `System.out.println(hang);` and run the program. See what is printing on the console? – prasad_ Jul 20 '18 at 01:18
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Remove the dependence on the DB by hard coding some data. 2) `jScrollPane1.setBounds(37, 79, 517, 247);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jul 20 '18 at 01:51
  • thanks you for your help. – liu lingrui Jul 25 '18 at 07:10

0 Answers0