Results 1 to 2 of 2
  1. #1
    anson920520 is offline Member
    Join Date
    Mar 2014
    Posts
    1
    Rep Power
    0

    Default How to insert String query into a JTabel

    Hello guys,
    I am beginner of JDBC, i have been successfully implement my database into my java program and displayed it inside a textarea
    but now i want those items inside a JTable for future development but i am unlikely to finish it without help



    So this is my code(The JTabel code start by line 37):

    Java Code:
    public class CheckoutSection extends JFrame implements ActionListener{
       private ImageIcon myIcon = new ImageIcon("F:\\Final Year project\\FYPLOGO2.jpg");
       private Connection con; 
       private Statement st; //this three attrubutes is for database
       private Statement st1; // for another attrubutes
       private ResultSet rs;
       private ResultSet rs1;
       JTable table;
       JLabel label;
       
       JTextField textbox;
       String driverName="com.mysql.jdbc.Driver";
       String url = "jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull";
       String userName ="root";
       String password ="anson134679852";
       String[] columnNames = {"Food number:"+"Food Name:"};
       //String[] columnNames1 = {""+""};
       //JTextArea textarea = new JTextArea (2,25);
       //JScrollPane scrollPane = new JScrollPane(textarea = new JTextArea());
       //add(scrollPane, BorderLayout.CENTRE);
         // private TextField jtfMessage = new TextField(10);
        public CheckoutSection() {
            try{
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull","root","anson134679852");
                st = con.createStatement();//to create statments which is goin to print out
                st1 = con.createStatement();
            }catch(Exception ex){
                System.out.println("Error" + ex);//error msg only
            }
            
          //this is the Jtable section
             setLayout(new FlowLayout());
            
            String[] heading = {"FOOD NUMBER","FOOD NAME" ,"FOOD PRICE"};
            
            Object[][] data ={
               // {"SELECT * FROM finalyearproject.maindishes"}, <---- i try to insert it in query way but it's not working
               // {"output"}, <------try to use the return value but fail too
               
                    
            };
            table = new JTable(data, heading);
            table.setPreferredScrollableViewportSize(new Dimension(300,30));
            table.setFillsViewportHeight(true);
            
            JScrollPane scrollPane = new JScrollPane(table);
            add(scrollPane);
    
               //this is the Jtable section
            
            
             //System.out.println("tester3");
            setLayout(new GridLayout(1, 4 ,5 ,5));
            add(new JLabel(myIcon));
            FlowLayout flowManager = new FlowLayout();
            setLayout(flowManager);
            //setBackground(Color.WHITE);
            JTextArea  output= new JTextArea();
            output.setText(getData());
            add(output);
            
            JTextArea output1 = new JTextArea();
            output1.setText(getData2());
            add(output1);
            
            //the database will display all the starter, maindishes, soups of specific restaunrant and allow people to select it
            // including a tickbox and scroll bar
              JCheckBox testButton = new JCheckBox("");
              JCheckBox testButton2 = new JCheckBox("");
              JCheckBox testButton3 = new JCheckBox("");
              JCheckBox testButton4 = new JCheckBox("");
              //testButton.setMnemonic(KeyEvent.VK_C);
              //testButton.setSelected(false);
              add(testButton); //so when the customer tick the boxes it will appear in the test field
              //add(testButton2);
            //  add(testButton3);
             // add(testButton4);
              testButton.addActionListener(new ActionListener(){
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    System.out.println("the check is working"); //To change body of generated methods, choose Tools | Templates.
                }
              });          
            JButton checkOutButton = new JButton("Check Out");
            //checkOutButton.setLayout(flowManager);//should posistion later
            //CheckButton.addActionListeneer(checkOutButton);//NEED TO INPUT SOME FUNCTION i got an example now :)
            add(checkOutButton);
            checkOutButton.addActionListener(this);
            
            
           
            
            
            
            
            
        }
             public void addActionListeneer(JButton checkOutButton) {
            /*   System.out.println("tester1");
            //this.setVisible(false); i want to close the current page and open the next page
             CheckoutSection checkOutFrame = new CheckoutSection();
             checkOutFrame.setTitle("EatQuick");
             checkOutFrame.setLocationRelativeTo(null);
             checkOutFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             checkOutFrame.setSize(700, 400);
             checkOutFrame.setVisible(true);
              System.out.println("tester2");
             this.setVisible(false);
             
               String test = "167";
               if(searchButton==searchButton)
               {
                   textarea.setText(test);
               }
                }*/
             
    
    }
           public void actionPerformed(ActionEvent e) {
           System.out.println("New button press");
            System.out.println("tester4");
            //this.setVisible(false); i want to close the current page and open the next page
            PaySection paysectionFrame = new PaySection();
             paysectionFrame.setTitle("EatQuick");
             paysectionFrame.setLocationRelativeTo(null);
             paysectionFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             paysectionFrame.setSize(700, 400);
             paysectionFrame.setVisible(true);
             this.setVisible(false);
            }
           public String getData(){
               String output ="";
              try{
                  String query = "SELECT * FROM finalyearproject.soups;";
                  rs = st.executeQuery(query);//privde sting query
                  System.out.println("Records from Database");
                  while(rs.next()){//while loop get them and print it out 
                      String FoodNumber= rs.getString("Food_number");
                      String FoodName= rs.getString("Food_Name");
                      String FoodPrice = rs.getString("Food_Price");
                      //String NumberOfRestaurant = rs.getString("Number_of_restaurant");
                      output += "Food number:"+FoodNumber+"     "+"Food Name:"+FoodName +"    "+FoodPrice + "\n";
                      //System.out.println("Food number:"+FoodNumber+"     "+"Food Name:"+FoodName +"    "+FoodPrice);
                  }         
              }catch(Exception ex){
                  System.out.println(ex);
              } 
              return output; 
           }
           
           public String getData2(){
                String output1 ="";
              try{
                  String query = "SELECT * FROM finalyearproject.maindishes;";
                  rs1 = st1.executeQuery(query);//privde sting query
                  System.out.println("Records from Database");
                  while(rs1.next()){//while loop get them and print it out 
                      String FoodNumber= rs1.getString("Food_number");
                      String FoodName= rs1.getString("Food_Name");
                      String FoodPrice = rs1.getString("Food_Price");
                      //String NumberOfRestaurant = rs.getString("Number_of_restaurant");
                      output1 += "Food number:"+FoodNumber+"     "+"Food Name:"+FoodName +"    "+FoodPrice + "\n";
                      //System.out.println("Food number:"+FoodNumber+"     "+"Food Name:"+FoodName +"    "+FoodPrice);
                  }
              }catch(Exception ex){
                  System.out.println(ex);
              } 
              return output1; 
    
           }
    
    }
    Many thanks guys

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default Re: How to insert String query into a JTabel

    Check out: Table From Database « Java Tips Weblog for some ideas.

    The TableFromDatabase.java example is probably the simplest way to start.

Similar Threads

  1. dynamic insert query statement
    By NandiniGarg in forum JDBC
    Replies: 4
    Last Post: 12-09-2010, 10:11 AM
  2. Replies: 12
    Last Post: 11-26-2010, 08:33 AM
  3. Updating JTabel
    By drwk in forum New To Java
    Replies: 2
    Last Post: 01-17-2010, 12:52 PM
  4. insert query in odbc
    By lipun4u in forum Advanced Java
    Replies: 3
    Last Post: 07-21-2009, 09:14 AM
  5. Replies: 3
    Last Post: 02-28-2009, 09:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •