数据未显示在 jtable 中

Data not displayed in jtable

您好,我正在尝试从数据库中检索数据并显示在 jtable 中,但我做不到。代码如下。谁能帮帮我。

//Dialog Creation for Finding Data
        jdlgFind=new JDialog(frame,"FIND DATA",true);
        jdlgFind.setFont(new Font("Times New Roman", Font.PLAIN, 12));
        jdlgFind.setSize(650,500);
        findpanel=new JPanel();
        findpanel.setBackground(new Color(144, 238, 144));
        jdlgFind.setContentPane(findpanel);
        findpanel.setLayout(new FlowLayout());
        findpanel.setFont(new Font("Times New Roman", Font.PLAIN, 12));

        txtGetMobileno=new JTextField(15);
        txtGetLandlineno=new JTextField(15);
        txtGetPlaceofBirth=new JTextField(15);

        JTable jtblFindDate=new JTable(tbldata,columnhead);
        jspFindPanel=new JScrollPane(jtblFindDate);
        jtblFindDate.setRowSelectionAllowed(true);
        jtblFindDate.setFont(new Font("Times New Roman", Font.PLAIN, 11));
        jtblFindDate.setPreferredSize(new Dimension(600, 500));

        findpanel.add(new JLabel("Mobile No. please : ")).setFont(new Font("Times New Roman", Font.BOLD, 12));
        findpanel.add(txtGetMobileno).setFont(new Font("Times New Roman", Font.PLAIN, 12));
        findpanel.add(new JLabel("Landline No. please : ")).setFont(new Font("Times New Roman", Font.BOLD, 12));
        findpanel.add(txtGetLandlineno).setFont(new Font("Times New Roman", Font.PLAIN, 12));
        findpanel.add(new JLabel("Place Of Birth please : ")).setFont(new Font("Times New Roman", Font.BOLD, 12));
        findpanel.add(txtGetPlaceofBirth).setFont(new Font("Times New Roman", Font.PLAIN, 12));
        findpanel.add(jspFindPanel);

        txtGetMobileno.setDocument(new TextFieldLimit(11));
        txtGetMobileno.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae) 
            {
                findMobileno=null;
                findMobileno=txtGetMobileno.getText();
                txtGetLandlineno.setEditable(false);
                txtGetPlaceofBirth.setEditable(false);

                if(findMobileno.length()!=0)
                {
                    try
                    {
                        dbcon=new DB_Connection();
                        PreparedStatement pstmt=dbcon.DB_Connection("//F://eclipse_Luna_64_Development_Workspace//Project JAVA//LIC_AGENCY_TRACKER//DATABASE//LIC_DATA_TRACKER.accdb").prepareStatement("select Inquiry_Master_Data.Inquiry_Master_Name as [Inquirer Name],Inquiry_Master_Data.Inquiry_Master_Mobile_Number as [Inquirer Mobile No],Inquiry_Master_Data.Inquiry_Master_Landline_Number as [Inquirer Landline No],"
                                + "Inquiry_Master_Data.Inquiry_Master_Place_Of_Work as [Inquirer Work Place] from Inquiry_Master_Data where Inquiry_Master_Data.Inquiry_Master_Mobile_Number='"+findMobileno+"'");
                        ResultSet rset=pstmt.executeQuery();
                        ResultSetMetaData rsetmetadata=rset.getMetaData();
                        int col=rsetmetadata.getColumnCount();

                        columnhead=new Vector(col);
                        columnhead.add("Inquirer Name");
                        columnhead.add("Inquirer Mobile No");
                        columnhead.add("Inquirer Landline No");
                        columnhead.add("Inquirer Work Place");

                        tbldata=new Vector();
                        row=new Vector();               
                        while(rset.next())
                        {
                            row=new Vector(col);
                            for(int i=1;i<=col;i++)
                                {
                                    row.add(rset.getString(i));
                                }
                            tbldata.add(row);
                        }
                    }
                    catch(Exception e)
                    {
                        JOptionPane.showMessageDialog(null, "Data not found.");
                        txtGetMobileno.setText("");
                        txtGetLandlineno.setEditable(true);
                        txtGetPlaceofBirth.setEditable(true);
                        txtGetMobileno.requestFocus();
                    }
                }
                else
                {
                    txtGetLandlineno.requestFocus();
                }
            }
        });

我正在使用 MS Access。我已经使用 Prepared Statement 检索数据,但它没有显示数据。

问候 桑迪普

JTable jtblFindDate=new JTable(tbldata,columnhead);

该语句应该做什么?您实际上在这两个变量中都有有效数据吗?如果不是,那么将创建没有行或列的 table。

columnhead=new Vector(col);
columnhead.add("Inquirer Name");
columnhead.add("Inquirer Mobile No");
columnhead.add("Inquirer Landline No");
columnhead.add("Inquirer Work Place");

该代码不执行任何操作,因为您随后没有使用 columnHead 创建新的 TableModel。

tbldata.add(row);

这可能会向 Vector 添加数据,但您同样不会使用 Vector 创建 TableModel

查看 Table 从数据库中找到的 TableFromDatabaseExample 代码以获取从数据库加载 JTable 的通用代码。

您好,我已经解决了这个问题。我已经更改了代码。下面是修改部分,我使用了以下代码:-

dbcon=new DB_Connection();
                        PreparedStatement pstmt=dbcon.DB_Connection("//F://eclipse_Luna_64_Development_Workspace//Project JAVA//LIC_AGENCY_TRACKER//DATABASE//LIC_DATA_TRACKER.accdb").prepareStatement("select Inquiry_Master_Data.Inquiry_Master_Name as [Inquirer Name],Inquiry_Master_Data.Inquiry_Master_Mobile_Number as [Inquirer Mobile No],Inquiry_Master_Data.Inquiry_Master_Landline_Number as [Inquirer Landline No],"
                                + "Inquiry_Master_Data.Inquiry_Master_Place_Of_Work as [Inquirer Work Place] from Inquiry_Master_Data where Inquiry_Master_Data.Inquiry_Master_Place_Of_Work='"+findPlaceofBirth+"'");
                        ResultSet rset=pstmt.executeQuery();
                        jtblFindData.setModel(DbUtils.resultSetToTableModel(rset));

为此,我使用了 rs2xml.jar 文件。 问候 桑迪普