Sandeep Chakraborty
Sandeep Chakraborty

Reputation: 23

Data not displayed in jtable

Hi I am trying to retrieve data from the database and display in the jtable but I am unable to do it. The code is given below. Can anyone please help me.

//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();
                }
            }
        });

I am using MS Access. I have used Prepared Statement to retrieve the data but it is not displaying the data.

Regards Sandeep

Upvotes: 0

Views: 59

Answers (2)

Sandeep Chakraborty
Sandeep Chakraborty

Reputation: 23

Hi I have solved the issue. I have changed the code. Below is the modification part, I have used the following codes: -

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));

For this I have used rs2xml.jar file. Regards Sandeep

Upvotes: 0

camickr
camickr

Reputation: 324118

JTable jtblFindDate=new JTable(tbldata,columnhead);

What is that statement supposed to do? Do you actually have valid data in both of those variables? If not then the table will be created with no rows or columns.

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

That code doesn't do anything because you don't then use the columnHead to create a new TableModel.

tbldata.add(row);

That may add data to the Vector, but again you don't use the Vector to create a TableModel

Check out the TableFromDatabaseExample code found in Table From Database for generic code to load a JTable from a database.

Upvotes: 1

Related Questions