【发布时间】:2017-09-13 00:37:03
【问题描述】:
我已经制作了一个 Pogram,我必须将数据库中的选定列存储到 JTable 中,并且我还必须将 JTextField 中的一些数据存储到相同的 JTable 中我已经尝试了很多东西,但它没有得到正确的结果显示错误,如java.lang.ArrayIndexOutOfBoundsException:0 源代码太长无法粘贴,但我粘贴了一些有用的行,让您了解我的问题。
数据库有一些列,如:
/*** ProductInfo Table******//
CREATE TABLE ProductInfo (
P_Code VARCHAR (45) PRIMARY KEY
NOT NULL,
P_Name VARCHAR (45),
Category VARCHAR (45),
SubCategory VARCHAR (45) ,
Description VARCHAR (500),
Cost_Price VARCHAR (45),
Selling_Price VARCHAR (45),
Reorder_Point INTEGER (10),
Opening_Stock INTEGER (10),
Discount INTEGER (10),
VAT INTEGER (10),
P_Image BLOB,
}
这是我必须存储信息的JTable
/*** StockInfo Table******//
CREATE TABLE StockInfo (
P_Code VARCHAR (45) ,
P_Name VARCHAR (45),
Description VARCHAR (500),
Selling_Price VARCHAR (45),
Opening_Stock INTEGER (10),
Discount INTEGER (10),
VAT INTEGER (10),
Date Date,
Pay_Due Varchar(45),
Tot_Pay varchar(45),
}
public void InsertDataToStockInfo()
{
try
{
ProductList obj=new ProductList(); //it is that JTable from where i have to fetch some column.
model=(DefaultTableModel)obj.table.getModel();
for(int i=0;i<obj.table.getColumnCount();i++)
{
String P_Code=model.getValueAt(i,0).toString();
String P_Name=model.getValueAt(i,1).toString();
String Description=model.getValueAt(i,4).toString();
String Selling_Price=model.getValueAt(i,6).toString();
String qnty=model.getValueAt(i, 8).toString();
int Qnty=Integer.parseInt(qnty);
String discount=model.getValueAt(i,9).toString();
int Discount=Integer.parseInt(discount);
String vAT=model.getValueAt(i,10).toString();
int VAT=Integer.parseInt(vAT);
query="Insert into StockInfo (P_Code,P_Name,Description,Selling_Price,Qnty,Discount,VAT)"
+ "Select P_Code,P_Name,Description,Selling_Price,Opening_Stock,Discount,VAT from ProductInfo where P_Code='"+P_Code+"'";
PStat=con.prepareStatement(query);
PStat.setString(1,P_Code);
PStat.setString(2,P_Name);
PStat.setString(3,Description);
PStat.setString(4,Selling_Price);
PStat.setInt(5,Qnty);
PStat.setInt(6,Discount);
PStat.setInt(7,VAT);
// here i also want to add some more information from JTextfield into the columns in JTable is it possible.
PStat.addBatch();
}
PStat.executeBatch();
JOptionPane.showMessageDialog(null,"Data Saved from ProductInfo");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e);
}
finally
{
try
{
PStat.close();
res.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e);
}
}
}
【问题讨论】:
-
1) 请在句子的开头使用大写字母来表示单词 I,以及像
ArrayList或 Oracle 这样的专有名称。但是就这样吧。 以大写字母开头的每个单词不会使信息更清晰,反而让我想结束问题并帮助其他人。 2) 为了尽快获得更好的帮助,请发帖minimal reproducible example 或Short, Self Contained, Correct Example。 -
i正在迭代表模型的列索引,但在调用getValueAt()时您将其用作行索引参数。也许您打算写i<obj.table.getRowCount()来作为您的循环限制?