-2

I am trying to read data from mysql form a specific table call Menu this table contain four rows I just need to read 4 rows only like item name, price, type, category as to Print it in console in a form of String Like a table view

I tried to create a method do this but I failed, like this method

public static void getMenuItems(String tableName) {
    try {
        setConnection();
        Statement stmt = con.createStatement();
        ResultSet rs;

        String strSelect = "select from " + tableName;
        rs = stmt.executeQuery(strSelect);
        // make the selection at the last row
        rs.last();
        int c = rs.getRow();
        rs.beforeFirst();

        String values[] = new String[c];

        while (rs.next()) {
            int i = 0;
            values[i] = rs.getString(1);
            i++;
        }
        con.close();
        System.out.println(values);
    } catch (SQLException e) {
        Tools.msgBox(e.getMessage());
    }

and this is my Menu.Class

public class Menu implements mainData{
    private int Menu_Id;
    private String Name;
    private float Price;
    private String Type;
    private String Category;

    public int getMenu_Id() {
        return Menu_Id;
    }

    public void setMenu_Id(int Menu_Id) {
        this.Menu_Id = Menu_Id;
    }

    public String getName() {
        return Name;
    }

    public void setName(String Name) {
        this.Name = Name;
    }

    public float getPrice() {
        return Price;
    }

    public void setPrice(float Price) {
        this.Price = Price;
    }

    public String getType() {
        return Type;
    }

    public void setType(String Type) {
        this.Type = Type;
    }

    public String getCategory() {
        return Category;
    }

    public void setCategory(String Category) {
        this.Category = Category;
    }
           //have here some method
.....................................
//Here is a method called getCustomRows
    @Override
    public void getCustomRows(String statement, JTable table) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Mahmoud Al-Haroon
  • 2,239
  • 7
  • 36
  • 72
  • 1
    *I failed*: what makes you think that? You get an exception, right? Then why not reading its message and stack trace? Surely it will tell you what is wrong, and where? – JB Nizet Apr 07 '18 at 19:29
  • Yeah I Actually got nullException point when I ran project as the msgBox said that No Suitable driver found for jdbc.......... how ever I installed MySQL JDBC @ cricket_007 – Mahmoud Al-Haroon Apr 07 '18 at 19:38
  • 2
    So, maybe you should think about fixing that, by downloading the driver and adding it to the classpath. Or at least by googling the error message you get. Error messages give useful information. Read them. – JB Nizet Apr 07 '18 at 19:39
  • I tried that and find that am didn't instal the jdbc library I followed the steps but I have the same exception.... If u need me to show u all code it's ok @cricket_007 – Mahmoud Al-Haroon Apr 07 '18 at 19:41
  • Why are you tagging me?? https://stackoverflow.com/questions/2839321/connect-java-to-a-mysql-database/2840358#2840358 – OneCricketeer Apr 07 '18 at 19:49
  • Am So Sorry I Need to tag @JB Nizet but I taged you wrongly am sorry – Mahmoud Al-Haroon Apr 07 '18 at 19:51

1 Answers1

0

You have multiple columns, not just a single string. (At least, if you did SELECT * FROM in your query)

Make a Menu values[] = new Menu[c];

Assign that in a loop

 for (int i = 0; rs.next(); i++) {
        Menu m = new Menu();
        // m.setName(rs.getString(1));
        values[i] = m;
 }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245