我正在尝试从mysql表单中读取数据特定的表调用菜单此表包含四行我只需要读取4行,如项目名称,价格,类型,类别,以在控制台中以字符串形式打印像表格视图
我尝试创建一个方法,但是我失败了,就像这个方法
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());
}
这是我的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.
}
}
答案 0 :(得分:0)
您有多个列,而不仅仅是一个字符串。 (至少,如果你在查询中SELECT * FROM
)
制作Menu values[] = new Menu[c];
在循环中分配
for (int i = 0; rs.next(); i++) {
Menu m = new Menu();
// m.setName(rs.getString(1));
values[i] = m;
}