baizixue 2015-12-30 07:34 采纳率: 0%
浏览 1915

java连接数据库 更新JTable问题

//下面是我的table
class Store_table

{
Object data[][];
Object name[]={"商品编号","商品名称","价格","上架日期","保质日期","库存"};
Store_table(String bq)
{

String url = "jdbc:sqlserver://127.0.0.1:1433;databaseName=Store;IntegratedSecurity=True";//
// Declare the JDBC objects.    
Connection con=null;    
Statement sql=null;    
ResultSet rs=null; 

try {    
    // Establish the connection.    
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");    
    con = DriverManager.getConnection(url);    

    sql = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);    
    rs = sql.executeQuery("SELECT  * FROM 仓库 where 标签='"+bq+"'");    
    rs.last();
    data=new Object[rs.getRow()][6];
    // Iterate through the data in the result set and display it.    
    int i=0;
    rs.beforeFirst();
    while (rs.next()) {    

        long number=rs.getLong("商品编号");
        String name=rs.getString("商品名称");
        float price=rs.getFloat("价格");
        Date date=rs.getDate("上架日期");
        int staydate=rs.getInt("保质日期");
        int repertory=rs.getInt("库存"); 
        data[i][0]=number;
        data[i][1]=name;
        data[i][2]=price;
        data[i][3]=date;
        data[i][4]=staydate;
        data[i][5]=repertory;
        i++;

    }    
}    

// Handle any errors that may have occurred.    
catch (Exception e) {    
    e.printStackTrace();    

}    

finally {    
    if (rs != null)    
        try {    
            rs.close();    
        } catch (Exception e) {    
        }    
    if (sql != null)    
        try {    
            sql.close();  
        } catch (Exception e) {    
        }    
    if (con != null)    
        try {    
            con.close();    
        } catch (Exception e) {    
        }    
} 



}

}

//我用JComboBox调用和更新JTable

        public void actionPerformed(ActionEvent arg0) {

            String a=comboBox.getSelectedItem().toString();

            store=new Store_table(a);
            //把store放进JTable
            ta=new JTable(store.data,store.name)
            {
                public boolean isCellEditable(int row, int column) {return false;};//设置列表数据不能修改
                };
            //把ta列表放进滚动列表
                ta.setModel(model);
            JScrollPane jsp=new JScrollPane(ta);
            //JScrollPane jsp=new JScrollPane(store);
            //getContentPane().add(jsp,BorderLayout.CENTER);    
            ta.setBackground(Color.ORANGE);
            jsp.setBackground(Color.GRAY);
            //jsp.setBackground(Color.ORANGE);
            contentPane.add(jsp);
            jsp.setBounds(0, 41, 743, 252);


        }
    });

怎么让JTable在JComboBox改动的时候更新呢?
据说需要setModel 再repaint 再updateUI
可是具体怎么做呢?

  • 写回答

1条回答 默认 最新

  • fengshi1234567 2015-12-31 02:30
    关注

    table不用每次都重新new一个,每次刷新用的都是同一个table, 所以new Jtable是在JComboBox的actionPerformed()之外,在动作之内只需更新table的model并repaint和updateUI. 你可以每次刷新都新建一个model:
    DefaultTableModel model=new DefaultTableModel(store.data, store.name);
    table.setModel(model);
    table.repaint();
    table.updateUI();
    或者新建一个model类,在Store_table(String bq)里面将数据用list的方式保存:
    public class StoreTableModel extends AbstractTableModel{

    private String[] names = {"商品编号","商品名称","价格","上架日期","保质日期","库存"};
    private List<Object> list = new ArrayList<Object>();
    
    public StoreTableModel() {
    
    }
    
    public void setRowData(List list) {
        this.list = list;
        fireTableDataChanged();
    }
    
    @Override
    public int getRowCount() {
        if(list.isEmpty())
            return -1;
        return list.size();
    }
    
    @Override
    public int getColumnCount() {
        return names.length;
    }
    
    @Override
    public String getColumnName(int columnIndex) {
        return names[columnIndex];
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (!list.isEmpty()) {
             return list.get(rowIndex)[columnIndex];
       }
        return null;
    }
    
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    } 
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办