【发布时间】:2017-05-10 11:04:47
【问题描述】:
我正在尝试将整数值(存储在 MongoDB 中)打印到 jtable 中。 这是 MongoDB 条目之一:
Inserted Document: 108
{
"_id": {
"$oid": "5912effdf9c13f1ab9377eb6"
},
"SrNo": "53",
"Brand": "Vivo",
"Product Name": "Y51L",
"Price": 10000,
"Brand Reputation": 7,
"Features": 10,
"Rating": 4
}
我可以打印产品名称、品牌和 srno 等字符串值。但我无法打印评级、功能等整数值。
代码如下:
JTable table = new JTable();
String[] columns = new String[]{"SrNo","Brand","Product Name","Price"};
DefaultTableModel model = new DefaultTableModel(columns,0);
JScrollPane scrollPane = new JScrollPane(table);
frame2.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame2.setBounds(100, 100, 677, 392);
frame2.setVisible(true);
frame2.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
DBCursor cursor = coll.find();
//int i=1;
while (cursor.hasNext())
{
DBObject obj = cursor.next();
String SrNo = (String)obj.get("SrNo");
//ObjectId id = (ObjectId)obj.get("_id");
String Brand = (String)obj.get("Brand");
String ProductName = (String)obj.get("Product Name");
//String price = (String)obj.get("Price");
//String price = Integer.toString((String)obj.get("Price"));
//int BrandReputation = Integer.parseInt((String)obj.get("Brand Reputation"));
//String brandrep = String.valueOf(BrandReputation);
//int Features = Integer.parseInt((String)obj.get("Features"));
//int Ratings = Integer.parseInt((String)obj.get("Rating"));
model.addRow(new Object[]{SrNo,Brand,ProductName});
}
table.setModel(model);
cursor.close();
mongoClient.close();
在 while 循环中,您可以看到注释的代码行,这些都是我尝试打印整数值的所有方法,但都失败了。还有其他方法可以将整数值打印到 jtable 中吗?
【问题讨论】:
-
有没有其他方法可以将整数值打印到 jtable 中? ==
getColumnClass,它也需要在数据库端存储正确的数据类型(以避免在运行时出现任何parseXxx) -
从未接触过 MongoDB,但是 int
BrandReputation = Integer.parseInt((String)obj.get("Brand Reputation"));在存储整数"Brand Reputation" : 7的情况下看起来非常愚蠢和矛盾,AFAIK 几乎所有的 JDBC 都可以正确地接受 data_types
标签: java mongodb swing jframe jtable