JTable运行时计算?

时间:2013-07-02 15:43:55

标签: java swing ms-access jdbc odbc

这是我的小组项目部分。当我输入客户ID并按OK时,客户详细信息将以表格格式显示。该表包含以下列:星期日,费率,星期六, 率,其他,率如。

我的问题是我想输入整数,计算它然后将其存储在数据库中。表格中的运行时间我想输入数字,计算并需要存储在数据库中。该怎么办?提前谢谢。

 import java.awt.BorderLayout;
 import java.awt.EventQueue;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.sql.*;
 import javax.swing.*;
 import javax.swing.event.CellEditorListener;
 import javax.swing.event.ChangeEvent;
 import javax.swing.table.DefaultTableModel;
 public class TestTable 
 {
public static void main(String[] args) 
{
    TestTable testTable = new TestTable();
}
public TestTable() 
{
    EventQueue.invokeLater(new Runnable() 
    {
        @Override
        public void run() 
        {
            try
            {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            }
            JFrame frame = new JFrame("Testing");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(new TestTable.Bill());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}    
public class Bill extends JPanel implements ActionListener ,CellEditorListener  
{
    JTextField textFieldId;
    JLabel l1;
    JLabel l2;
    JButton b1,b2,b3;
    JTextField sun,sunr,sat,satr,oth,othr;
    float sum1,totall;
    ResultSet rs1 = null;
    DefaultTableModel model = new DefaultTableModel();       
    private int rows;
    Double value;
    public Bill() 
    {
        setLayout(new BorderLayout());            
        JPanel fields = new JPanel();
        textFieldId = new JTextField(10);
        l1 = new JLabel("New Customer Entry :-");
        l2 = new JLabel("Customer Id");
        b1 = new JButton("OK");
        b2 = new JButton("Calculate");
        b3 = new JButton("Print");
        fields.add(l2);
        fields.add(textFieldId);
        fields.add(b1);
        fields.add(b2);
        fields.add(b3);
        add(fields, BorderLayout.NORTH);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        // Don't forget to add a table.
        add(new JScrollPane(new JTable(model)));
    }
    @Override
    public void actionPerformed(ActionEvent e) 
    {
        System.out.println("You clicked the button");
        Connection con;
        if (e.getSource() == b1) 
        {
            PreparedStatement ps = null;
            Statement stmt = null;
            try 
            {

                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con = DriverManager.getConnection("jdbc:odbc:devendra");
                ps = con.prepareStatement("SELECT  * FROM Customer where Customer_Id = ?");
                // You must bind the parameter with a value...
                ps.setString(1, textFieldId.getText());
                rs1 = ps.executeQuery();
                model.addColumn("Customer_Id");
                model.addColumn("Customer_Name");
                model.addColumn("Contact");
                model.addColumn("Paper_Name");
                model.addColumn("Sunday");
                model.addColumn("Rate");
                model.addColumn("Satarday");
                model.addColumn("Rate");
                model.addColumn("Other Day");
                model.addColumn("Rate");
                model.addColumn("Price");
                model.setRowCount(0);
                while (rs1.next()) 
                {                           
                        model.addRow(new Object[]{rs1.getString(1),rs1.getString(2),rs1.getString(3),rs1.getString(4),rs1.getString(5),
                            rs1.getString(6),rs1.getString(7),rs1.getString(8),rs1.getString(9),rs1.getString(10)});
                }
             //   Vector data = model.getDataVector();

                JOptionPane.showMessageDialog(null, "You successfully Enter the Entry");
            } 
            catch (SQLException s) 
            {
                System.out.println("SQL code does not execute.");
                JOptionPane.showMessageDialog(null, "Please Enter the Detail Correctly");
            } catch (Exception exp) 
            {
                JOptionPane.showMessageDialog(this, "Failed to perform query: " + exp.getMessage());
            } finally 
            {
                try {
                    ps.close();
                } 
                catch (Exception ex)
                {
                }
            }                
        } 
        if (e.getSource() == b2)     
        {
             int rowCount = model.getRowCount();


             for(int i =1; i<rowCount;i++)
             {
                 Double valuea = (Double) model.getValueAt(i, 5 );
                 Double valueb = (Double) model.getValueAt(i, 6 );
                 Double valuec = (Double) model.getValueAt(i, 7 );
                 Double valued = (Double) model.getValueAt(i, 8 );
                 Double valuee = (Double) model.getValueAt(i, 9 );
                 Double valuef = (Double) model.getValueAt(i, 10 ); 
                  double value; 
                  value = Double.parseDouble( String.valueOf( valuea ) ) * Double.parseDouble( String.valueOf( valueb ) ); 
                  model.setValueAt( value, i, 10 ); 
             }

    }

}

    @Override
    public void editingStopped(ChangeEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void editingCanceled(ChangeEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }



}

}

2 个答案:

答案 0 :(得分:2)

我看不到你的INSERT如何成功:括号不平衡,而setValueAt() - 实际调用的文本 - 返回void;检查日志。您似乎在插入单个属性,而Customer关系有11.如果您的UPDATE属性为Total,请注意您的架构违反了normal form,其中Total取决于其他属性的子集。有关更正架构的信息,请咨询您的小组。将sum1的公式重新计算到一个方法中,只要Total显示就可以重复使用。

答案 1 :(得分:0)

我认为您的代码不正确。如何添加行或列。我认为在将所有内容添加到表模型时需要提供table model,还需要创建一个计算总和的新function。还要在构造函数中添加列,即只添加一次。咨询您的小组。