0

I am creating a method that will allow jTable1 to be filled with values in my SQL database named 'Saved_Recipes'.

I have tried the following code; however, an error is occurring.

private void FillTableRecipes() {
    try {
        Connection connTable = DriverManager.getConnection("jdbc:mysql://localhost:3306/saved_recipes", "root", "nerdswonka");

        ResultSet rslol = null;
        String fillT1 = "SELECT * FROM saved_recipes";
        PreparedStatement pstTbl = connTable.prepareStatement(fillT1);
        rslol = pstTbl.executeQuery();
        jTable1.setModel((TableModel) rslol);

    } catch(SQLException e) {
        JOptionPane.showMessageDialog(null, "Error in connecting to the SQL Database");
    }
}

When I run the program, the following error arrises:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: com.mysql.cj.jdbc.result.ResultSetImpl cannot be cast to javax.swing.table.TableModel
at brewing_system.ia.Home_Page.FillTableRecipes(Home_Page.java:150)
at brewing_system.ia.Home_Page.<init>(Home_Page.java:29)
at brewing_system.ia.Home_Page$4.run(Home_Page.java:198)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:749)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:719)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

How do I solve this problem and get my program to run?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

0 Answers0