2013-07-02 71 views
-2

這是我的團隊項目部分。當我輸入客戶ID並按OK時,客戶詳細信息以表格格式顯示。該表包含以下列:星期日,費率,星期六, 費率,其他,費率類似。JTable運行時間計算?

我的問題是我想輸入整數,計算它,然後將其存儲在數據庫中。在表中運行時間我想輸入數字,計算並需要存儲在數據庫中。該怎麼辦?提前致謝。

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."); 
    } 



} 

}

+3

_I想輸入數字,計算[和]存儲在數據庫中._什麼阻止你?你的'UPDATE'語句在哪裏?您要求某人使用支持不佳的驅動程序,針對不可訪問的數據庫上的隱含架構調試/修改冗長的代碼。 L&F應該是不相關的,只需要一個數據庫屬性。請修改您的問題以包含顯示問題的[sscce](http://sscce.org/)。 – trashgod

+0

我剛剛編輯了code.when我運行這些代碼它得到成功執行,但我不給我適當的輸出,因爲我想要它。現在該怎麼做,請告訴我? –

回答

2

我怎麼沒看到你的INSERT可以成功:括號不均衡,以及setValueAt() -were其文本實際調用,回報void;檢查日誌。您似乎插入了單個屬性,而Customer關係的值爲11.如果您的意思是UPDATETotal屬性,請注意您的架構違反了normal form,因爲Total取決於其他屬性的子集。諮詢你的小組關於糾正模式。將sum1的公式重新分解爲一個方法,只要顯示Total,就可以重複使用該方法。

+0

請在你的系統中調試這些代碼。當表出現我手動輸入列前列從哪裏列開始從星期天和結束率和整個計算顯示在總列.. 和我的團隊得到了盡興如何做這些代碼.. @ trashgod 如果你能幫我解決這些問題 :)請在你的系統中運行.. –

+0

也考慮一個'model.addRow()'方法,而不是'model.getDataVector()'。 – trashgod

0

我不認爲你的代碼是正確的。它如何添加行或列。我認爲當您完成將所有內容添加到表模型時,需要提供table model,並且還會創建一個新的function來計算總和。還要在構造函數中添加列,即只添加一次。諮詢你的小組。

+0

你對這些問題有什麼建議嗎? 實際上,如果我們有任何想法或解決方案,那麼我們都不知道任何更多的東西,然後我們編輯我們的代碼。 但你也幫我解決了這些問題。謝謝你的幫助... –

+0

'DefaultTableModel'是一個'TableModel'。 – trashgod

+0

可以給我從那個鏈接的教程鏈接,我可以瞭解更多...? –