mysql如何在Java中将所选列从数据库获取到JTable
我已经做了一个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,
}
这就是我必须存储信息的地方
/*** 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);
}
}
}
共 (0) 个答案