如何将我的wamp表上的所有值显示给JTable

时间:2014-03-22 02:14:05

标签: java jtable wamp

我想将我的Wamp服务器表中的所有数据显示给我的JTable。 只显示第一个数据。 (我是Java的新手)

这个程序使用JTextField可以很好地工作,但是在JTable上它不起作用。

使用JTable:

Account Number/FirstName/MiddleName/LastName

1111-11       / Erwin   / Walker  /   Miles 
private class ListenviewDepositor implements ActionListener
{
        public void actionPerformed(ActionEvent event)
        {
            ListAccount.setVisible(true);
            try
            {
                ResultSet result = null;

             Class.forName("com.mysql.jdbc.Driver").newInstance();  

             Connection conn = DriverManager.getConnection(CONNECTION_URL);
             Statement stmt = conn.createStatement();
            try
             {

                 result = stmt.executeQuery("SELECT * FROM mist1");
             }
             catch(SQLException e)
             {
                 System.out.println("SQLException:" + e.getMessage() );
                 System.out.println("SQLState:" + e.getMessage() );
                 System.out.println("VendorError:" + e.getMessage() );
             }
             while(result.next())
             {
                    String AccNum = result.getString("AccountNumber");
                    String fNtext = result.getString("FirstName");
                    String mNtext = result.getString("MiddleName");
                    String lNtext = result.getString("LastName");



                    String columnNames[] = { "Account Number ", "First Name", "Last Name","Middle Name" };
                    String dataValues[][]=

                    {
                        {AccNum,fNtext,lNtext,mNtext}

                    };

                    table = new JTable( dataValues, columnNames );

                    scrollPane = new JScrollPane( table );
                    ListAccount.add(scrollPane);

             }

            }
            catch(Exception e)
            {
                System.out.println("Holy Crap" );
                System.out.println( e.getMessage() );
                e.printStackTrace();
                System.out.println("Remember Mandatory Fields");
            }
        }
}

1 个答案:

答案 0 :(得分:0)

问题可能在循环条件下。

while(result.next())
             {
                    String AccNum = result.getString("AccountNumber");
                    String fNtext = result.getString("FirstName");
                    String mNtext = result.getString("MiddleName");
                    String lNtext = result.getString("LastName");



                    String columnNames[] = { "Account Number ", "First Name", "Last Name","Middle Name" };
                    String dataValues[][]=

                    {
                        {AccNum,fNtext,lNtext,mNtext}

                    };

                    table = new JTable( dataValues, columnNames );

                    scrollPane = new JScrollPane( table );
                    ListAccount.add(scrollPane);

             }