数据未显示在jtable中

时间:2015-08-07 16:03:37

标签: java swing 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来检索数据,但它没有显示数据。

此致 和Sandeep

2 个答案:

答案 0 :(得分:1)

JTable jtblFindDate=new JTable(tbldata,columnhead);

该声明应该做什么?你真的在这两个变量中都有有效的数据吗?如果没有,那么将创建没有行或列的表。

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

查看表格数据库中的TableFromDatabaseExample代码,了解从数据库加载JTable的通用代码。

答案 1 :(得分:0)

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

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文件。 问候 和Sandeep