1

I am trying to display the database records in the Jtable but i m not getting the code right. I m using IDE netbeans and database is mysql. I can see the panel and the scroll pane but the table is not displayed. I think something is wrong in the table properties or dont know if its invisible. My code is as follows:

try{        
    panel_paylist.setVisible(true);
    String dbUrl = "jdbc:mysql://localhost/hostel";
    String dbClass = "com.mysql.jdbc.Driver";
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn=DriverManager.getConnection(dbUrl,"root","17121990");
    System.out.println("Connected!!!!");
    MainScreen obj = new MainScreen(conn); 

    String[] columnNames = {"First Name",
                    "Last Name",
                    "Amount Recvd.",
                    "Date","Cheque/cash","cheque no","Balance Amt.","Total Amt.",
                    "Vegetarian"};

    ResultSet rs = null;
    Statement sql= null;
    ArrayList<Object[]> data = new ArrayList<>();
    String query="SELECT firstname,lastname, amountreceivd,dte,creditcashcheque,cheque_no,balance_amt, totalamount,Remark FROM payment;";
    sql = con.createStatement();
    sql.executeQuery(query);
    rs = sql.getResultSet();

    while(rs.next()){
                Object[] row = new Object[]{rs.getString(1), 
                rs.getString(2), 
                rs.getInt(3),
                rs.getString(4),
                rs.getString(5),
                rs.getString(6),
                rs.getInt(7),
                rs.getInt(8),
                rs.getString(9)};
                data.add(row);
            }
    Object[][] realData = data.toArray(new Object[data.size()][]);
    table_paylist= new JTable(realData, columnNames);
    scroll_paylist= new JScrollPane(table_paylist);        
    table_paylist.setPreferredScrollableViewportSize(new Dimension(800, 200));
    table_paylist.setFillsViewportHeight(true);
    panel_paylist.setLayout(new BorderLayout());
    panel_paylist.add(scroll_paylist, BorderLayout.CENTER); 
    }
    catch(Exception e)
    {

    }

please help

2 Answers 2

1

first of all you are missing port number of localhost. change

"jdbc:mysql://localhost/hostel"

to

"jdbc:mysql://localhost:3306/hostel";

second point dont use semicolon(;) at the end of sql string. i. e. remove semicolon from

"SELECT firstname,lastname, amountreceivd,dte,creditcashcheque,cheque_no,balance_amt, totalamount,Remark FROM payment;"

and you can also try this one

public DefaultTableModel PlayList() throws ClassNotFoundException,SQLException,ParseException
    {

        String[] columnNames = {"First Name","Last Name","Amount Recvd.","Date","Cheque/cash","cheque no","Balance Amt.","Total Amt.","Vegetarian"};

        DefaultTableModel dtm = new DefaultTableModel(columnNames , 0);

        dtm.setColumnCount(9);

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080/hostel","root","17121990");
        PreparedStatement ps1 = null;
        ResultSet rs1 = null;

        try
            {
                Class.forName("com.mysql.jdbc.Driver");
                ps1=conn.prepareStatement("SELECT firstname,lastname,mountreceivd,dte,creditcashcheque,cheque_no,balance_amt,totalamount,Remark FROM payment");
                rs1 = ps1.executeQuery();
                while(rs1.next())
                {
                   dtm.addRow(new Object[]{rs1.getString(1)
                                            rs.getString(2), 
                                            rs.getInt(3),
                                            rs.getString(4),
                                            rs.getString(5),
                                            rs.getString(6),
                                            rs.getInt(7),
                                            rs.getInt(8),
                                            rs.getString(9)});
                }
            }
        finally
            {
                rs1.close();
                ps1.close();
                conn.close();
            }
        return dtm;
    }

now use PlayList() method as a argument of your jtable's setmodel method;

i. e. jtable.setmodel(PlayList());

Sign up to request clarification or add additional context in comments.

2 Comments

I tried it but still its not working. I think i have made some mistakes. Anyways thank you for ur help
thanks a lot i got the output had made silly mistake in the code.
0

Use rs2xml third party Jar file to display query results.This is very helpful

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.