我不能让它工作......我怎样才能从抽象模型内的数据库中获取数据?我真的需要帮助谢谢..我尝试了不同的解决方案,但没有成功...有人可以帮我谢谢...即使只是链接上帝保佑!
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();
}
});
}
}