如何从Java中将选定的列从数据库获取到JTable

时间:2017-09-13 00:37:03

标签: java mysql swing jtable

我已经制作了一个图表,我必须将所选列从数据库存储到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 个答案:

答案 0 :(得分:0)

首先,您必须从数据库获取数据,它将为您提供列号/列名称和具体数据

public DefaultTableModel CreateTableModel(){
  //--

     String query = "select field1,field2...";
     ResultSet results = statment.executeQuery(query);

//--
 }

现在创建一个方法来获取columns_name或只是自己命名并将其添加到模型

DefaultTableModel model = new DefaultTableModel();
String[] ColumnNamesCollections = GetColumnsNameByQuery(query);
model.setColumnIdentifiers(ColumnNamesCollections);

现在只需迭代结果集并填写表格

while(results.next())
{
   Vector<Objetc> element = new Vector<Object>

   for(counter=0;counter<ColumnNamesCollections.size();counter++)
   {

      element.add(results.getObjetc(counter))
   }

   model.addRow(element);
}
DefaultTableModel model = CreateTableModel();
java.swing.JTable myTable = new java.swing.JTable(model)