如何仅将我的数据库(NetBeans上的JB)中的选定列显示到在JFrame中创建的表中?

问题描述 投票:0回答:1

我希望JFrame中的表格仅打印与不同用户相关的内容。

例如,仅dessert显示在菜单上。我创建的代码仅用于显示甜点,但是一旦我尝试显示vegetarianside菜,表就会显示所有内容。 (main课程也是如此。

请帮助从素食者/非素食者中分离出主要/副食品/甜点!

my database

//For dessert - works
//first JFrame
        public void getSelection(){
                 try{
                     while (myresObj.next()){
                         try {
                            int id = myresObj.getInt("Id");
                            String name = myresObj.getString("Name");
                            String type = myresObj.getString("Type");
                            //int rating= myresObj.getInt("Rating");
                            Boolean vegetarian = myresObj.getBoolean("Vegetarian");
                            Boolean shownonmenu = myresObj.getBoolean("Shownonmenu");

                            if(type.equals("Dessert")&&shownonmenu){
                                usersFood.setModel(DbUtils.resultSetToTableModel(myresObj));
                            }

                         } catch (SQLException ex){
                            Logger.getLogger(UserDessertV.class.getName()).log(Level.SEVERE, null, ex);
                         }


                 }   

                 }catch(SQLException e){

                 }

        }

            public void getConnection(){
            try{
                myconObj = DriverManager.getConnection("jdbc:derby://localhost:1527/MenuApp", "Anna", "1234");
                mystatObj = myconObj.createStatement();
                myresObj = mystatObj.executeQuery("Select * from Anna.Food");

            }
            catch (SQLException e){
                e.printStackTrace();
            }
        }

        //Main- doesnt work
        //second JFrame
        public void getSelection(){
                 try{
                     while (myresObj.next()){
                         try {
                            int id = myresObj.getInt("Id");
                            String name = myresObj.getString("Name");
                            String type = myresObj.getString("Type");
                            //int rating= myresObj.getInt("Rating");
                            Boolean vegetarian = myresObj.getBoolean("Vegetarian");
                            Boolean shownonmenu = myresObj.getBoolean("Shownonmenu");

                            if(type.equals("Main")&&!vegetarian&&shownonmenu){
                                usersFood.setModel(DbUtils.resultSetToTableModel(myresObj));
                            }

                         } catch (SQLException ex){
                            Logger.getLogger(UserMainNV2.class.getName()).log(Level.SEVERE, null, ex);
                         }          
                }   

                }catch(SQLException e){

                }

        }

            public void getConnection(){
            try{
                myconObj = DriverManager.getConnection("jdbc:derby://localhost:1527/MenuApp", "Anna", "1234");
                mystatObj = myconObj.createStatement();
                myresObj = mystatObj.executeQuery("Select * from Anna.Food");

            }
            catch (SQLException e){
                e.printStackTrace();
            }
        }
java swing jtable
1个回答
0
投票

您有两种方法:

  1. 在您的SQL中不要使用“ select *”。相反,您可以构建SQL字符串以仅返回所需的列。

  2. 如果在SQL中使用“ select *”,则需要在创建JTable之后从JTable中删除不需要的列。为此,您可以使用TableColumnModel.removeColumn(...)方法。您可以使用TableColumnModel.getColumn(...)方法删除该列。

© www.soinside.com 2019 - 2024. All rights reserved.