2012-06-21 49 views
-1

我无法使其工作......我如何从抽象模型中的数据库中获取数据?我真的需要帮助谢谢..我尝试了不同的解决方案,但没有成功...有人可以帮助我谢谢...甚至只是链接上帝保佑!Java Swing从数据库中获取数据错误

import java.awt.Dimension; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.ResultSetMetaData; 
import java.sql.SQLException; 
import java.sql.Statement; 

import javax.swing.BoxLayout; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTable; 
import javax.swing.JTextField; 
import javax.swing.ListSelectionModel; 
import javax.swing.RowFilter; 
import javax.swing.SpringLayout; 
import javax.swing.SwingConstants; 
import javax.swing.event.DocumentEvent; 
import javax.swing.event.DocumentListener; 
import javax.swing.table.AbstractTableModel; 
import javax.swing.table.TableRowSorter; 

public class ConsolidateInvoices extends JPanel { 

    private JTable table; 
    private JTextField Name; 
    private JTextField BusinessPartner; 
    private TableRowSorter<Model> sorter; 
    private String[] columnNames; 
    private Object[][] data; 

    public ConsolidateInvoices() { 

     JPanel form = new JPanel(new SpringLayout()); 

     JLabel lblName = new JLabel("Name:", SwingConstants.TRAILING); 
     form.add(lblName); 
     Name = new JTextField(); 
     form.add(Name); 
     JLabel lblBusinessPartner = new JLabel("Business Partner:", 
       SwingConstants.TRAILING); 
     form.add(lblBusinessPartner); 
     BusinessPartner = new JTextField(); 

     // Whenever filterText changes do the filter method 
     BusinessPartner.getDocument().addDocumentListener(
       new DocumentListener() { 
        public void changedUpdate(DocumentEvent e) { 
         newFilter(); 
        } 

        public void insertUpdate(DocumentEvent e) { 
         newFilter(); 
        } 

        public void removeUpdate(DocumentEvent e) { 
         newFilter(); 
        } 
       }); 

     lblBusinessPartner.setLabelFor(BusinessPartner); 
     form.add(BusinessPartner); 
     SpringUtilities.makeCompactGrid(form, 2, 2, 6, 6, 6, 6); 
     add(form); 

     try { 

      Class.forName("org.postgresql.Driver"); 
      Connection connection = null; 
      connection = DriverManager.getConnection(
        "jdbc:postgresql://127.0.0.1:5432/adempiere2", 
        "postgres", "postgres"); 

      // Read data from a table 
      String query = "Select c_invoice_id, documentno " 
        + "from adempiere.c_invoice"; 
      Statement stmt = connection.createStatement(); 
      ResultSet rst = stmt.executeQuery(query); 
      ResultSetMetaData md = rst.getMetaData(); 
      int columns = md.getColumnCount(); 
      int rows = 1; 

      //new Boolean(false) 

      columnNames = new String[columns]; 
      // Get column names 
      for (int i = 1; i <= columns; i++) { 
       columnNames[i-1] = md.getColumnName(i); 
      } 

      System.out.println(rows); 

      data = new Object[rows][columns]; 
      // Get row data 
      while (rst.next()) { 
       int ctr = 0; 
       for (int i = 1; i <= columns; i++) { 
        data[ctr][i-1] = rst.getObject(i); 
       } 
       ctr++; 
      } 

      rst.close(); 
      stmt.close(); 
      connection.close(); 

     } catch (SQLException e) { 

     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } 

     // Create a separate panel for the table 
     setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 
     table = new JTable(new Model()); 
     // Sorter 
     Model model = new Model(); 
     sorter = new TableRowSorter<Model>(model); 
     table.setRowSorter(sorter); 
     table.setPreferredScrollableViewportSize(new Dimension(700, 700)); 
     table.setFillsViewportHeight(true); 

     // To make the selection single 
     table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 

     // To create the scroll panel 
     JScrollPane scrollPane = new JScrollPane(table); 
     // initColumnSizes(table); 
     // setUpSportColumn(table, table.getColumnModel().getColumn(2)); 
     add(scrollPane); 
    } 

    // The filter method used for the business partner 
    private void newFilter() { 
     RowFilter<Model, Object> rf = null; 
     // If current text doesn't parse, don't update. 
     try { 
      rf = RowFilter.regexFilter(BusinessPartner.getText(), 0); 
     } catch (java.util.regex.PatternSyntaxException e) { 
      return; 
     } 
     sorter.setRowFilter(rf); 
    } 

    class Model extends AbstractTableModel { 

     public int getColumnCount() { 
      return columnNames.length; 
     } 

     public int getRowCount() { 
      return data.length; 
     } 

     public String getColumnName(int col) { 
      return columnNames[col]; 
     } 

     public Object getValueAt(int row, int col) { 
      return data[row][col]; 
     } 

     // To make the column a check box 
     public Class getColumnClass(int c) { 
      return getValueAt(0, c).getClass(); 
     } 

     public boolean isCellEditable(int row, int col) { 
      return false; 
     } 

     public void setValueAt(Object value, int row, int col) { 
      data[row][col] = value; 
      fireTableCellUpdated(row, col); 
     } 

    } 

    private static void showWindow() { 
     // Create and set up the window. 
     JFrame frame = new JFrame("Table"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     // Create and set up the content pane. 
     ConsolidateInvoices newContentPane = new ConsolidateInvoices(); 
     newContentPane.setOpaque(true); // content panes must be opaque 
     frame.setContentPane(newContentPane); 
     // Display the window. 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       showWindow(); 
      } 
     }); 
    } 

} 
+0

究竟是什么失败?显示堆栈跟踪。只显示有问题的部分,而不是整个程序。 –

+0

[Stack Overflow不是研究助理](http://meta.stackexchange.com/a/128553/177097) –

+0

[Stack Overflow不会读取所有](http://meta.stackexchange.com/ a/129787/137961) - 将问题归结为一小段代码,说明您看到的错误。通常你会通过这样做的本质来发现问题。 –

回答

2

我想这个问题是在这里:

table = new JTable(new Model()); 
    // Sorter 
    Model model = new Model(); 
    sorter = new TableRowSorter<Model>(model); 
    table.setRowSorter(sorter); 

你不使用相同的模式,为表和分拣机。什么有关:

Model model = new Model(); 
    sorter = new TableRowSorter<Model>(model); 
    table = new JTable(model); 
    table.setRowSorter(sorter); 

而且我发现this similar post所述OP建议使用:

sorter.setSortsOnUpdates(true); 

和替换fireTableCellUpdated(row, col);(在setValueAt())有:

fireTableRowsUpdated(0, data.size() - 1); 

如果你看看这个Oracle example我们可以看到一个有趣的评论:

 // Normally, one should call fireTableCellUpdated() when 
     // a value is changed. However, doing so in this demo 
     // causes a problem with TableSorter. The tableChanged() 
     // call on TableSorter that results from calling 
     // fireTableCellUpdated() causes the indices to be regenerated 
     // when they shouldn't be. Ideally, TableSorter should be 
     // given a more intelligent tableChanged() implementation, 
     // and then the following line can be uncommented. 
     // fireTableCellUpdated(row, col); 
+0

谢谢我要去试试:) –

3

虽然没有说明是什么问题,我看到已经有几个问题:

table = new JTable(new Model()); 
    // Sorter 
    Model model = new Model(); 

创建两个型号,而你倒是应该使用相同的实例。现在你可能会得到这个罚款,因为他们共享相同的data

} catch (SQLException e) { 
     // Always log or print an exception 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
     // If this exception occurs, the rest of the code is pretty much useless since you won't have a connection to the DB. You should rather forward this exception as an error 
    } 


    // To make the column a check box 
    public Class getColumnClass(int c) { 
     return getValueAt(0, c).getClass(); 
    } 

如果您没有值,您将收到一个异常(ArrayIndexOutOfBounds或NullPointerException)。这些信息应该基于ResultMetadataSet

public boolean isCellEditable(int row, int col) { 
     return false; 
    } 

    public void setValueAt(Object value, int row, int col) { 
     data[row][col] = value; 
     fireTableCellUpdated(row, col); 
    } 

如果你说你的单元格不可编辑,不需要实现setValueAt,它将永远不会被调用。

什么是与你的for循环?:

for (int i = 1; i <= columns; i++) { 
     columnNames[i-1] ... 

只需使用:

for (int i = 0; i < columns; i++) { 
     columnNames[i] ... 

清洁,更简单,更容易出错。

+0

感谢您的建议,我知道代码真的很乱...因为我太强调再次修复它哈哈..谢谢...但我可以问一些抽象集扩展模型如何从数据库中获取数据?我应该使用数组还是向量?谢谢我有点困惑,因为这是我第一次使用jframe ...谢谢 –

+0

@KevinTan你遇到什么问题与当前的代码?我看不到什么不工作。您可以使用二维数组来存储值,这不是问题。 –

+0

嗨Guillaume ...我想我能解决它..虽然我使用了矢量...非常感谢您的帮助:)我可能会再问,但你真的帮助我,谢谢:) –