我有一个名为tbl_income和tbl_expenses的表... tbl_income包含四列和四行,tbl_expenses包含下图中的四列和三行链接。
我想在下面的java swing JTable链接图中mank单表。
我怎么能这样做......抱歉英语不好..
这就是我尝试过的:
go build
tblincome有单行但行回复
答案 0 :(得分:2)
select * from income left join expenses on income.id = expenses.id
然后我使用+----+------------+----------------+--------+----+------------+------------+--------+--+
| id | dateValue | particular | amount | id | dateValue | particular | amount | |
+----+------------+----------------+--------+----+------------+------------+--------+--+
| 1 | 2017-02-02 | Cash Deposit | 1 | 1 | 2017-05-07 | Factory | 6 | |
| 2 | 2017-02-03 | Cheque Deposit | 2 | 2 | 2017-06-02 | Staff | 7 | |
| 3 | 2017-02-08 | Cash Deposit | 3 | 3 | 2017-06-03 | Travel | 8 | |
| 4 | 2017-02-07 | Product Sales | 4 | | | | | |
+----+------------+----------------+--------+----+------------+------------+--------+--+
查询数据库并得到:
JTable
基于当前可用的信息可能是我能做的最好的
您可以使用Most simple code to populate JTable from ResultSet或Retrieving Data from JDBC Database into Jtable之类的内容来构建full outer join
...作为可用示例
如果第二个表有比第一个表更多的行..记录将不会显示哪个是第二个表中更多的id - raman dangol 8小时前
此外,如果ID不一致,记录将丢失。这就是为什么我说这不是一个微不足道的问题
然后在这些情况下,像FULL OUTER JOIN这样的东西会更有用。但是,根据我的信息,MySQL不支持select * from income left join expenses on income.id = expenses.id union select * from income right join expenses on income.id = expenses.id where income.id is null
,因为这会很有用。但是,基于How to do a FULL OUTER JOIN in MySQL?我们可以做类似的事情......
+----+------------+----------------+--------+----+------------+------------+--------+
| id | datevalue | particular | amount | id | datevalue | particular | amount |
+----+------------+----------------+--------+----+------------+------------+--------+
| 1 | 2017-02-02 | Cash Deposit | 1.0 | 1 | 2017-05-07 | Factory | 6.0 |
| 2 | 2017-02-03 | Cheque Deposit | 2.0 | 2 | 2017-06-02 | Staff | 7.0 |
| 3 | 2017-02-03 | Cash Deposit | 3.0 | 3 | 2017-06-03 | Travel | 8.0 |
| 4 | 2017-02-03 | Product Sales | 4.0 | 4 | 2017-10-01 | Test 1 | 10.0 |
| 5 | 2017-10-02 | Test 2 | 20.0 | | | | |
+----+------------+----------------+--------+----+------------+------------+--------+
可以生成像
这样的东西select income.id, income.datevalue, income.PARTICULAR, income.AMOUNT,
expenses.id, expenses.datevalue, expenses.PARTICULAR, expenses.AMOUNT
from income join expenses on income.id = expenses.id
union all
select income.id, income.datevalue, income.PARTICULAR, income.AMOUNT,
null, null, null, null
from INCOME where not exists (select expenses.id from expenses where expenses.id = income.id)
union all
select null, null, null, null,
expenses.id, expenses.datevalue, expenses.PARTICULAR, expenses.AMOUNT
from expenses where not exists (select income.id from income where income.id = expenses.id)
或者,如果您希望将事物与“选定的一方”保持一致,例如
+----+------------+----------------+--------+----+------------+------------+--------+
| id | datevalue | particular | amount | id | datevalue | particular | amount |
+----+------------+----------------+--------+----+------------+------------+--------+
| 1 | 2017-02-02 | Cash Deposit | 1.0 | 1 | 2017-05-07 | Factory | 6.0 |
| 2 | 2017-02-03 | Cheque Deposit | 2.0 | 2 | 2017-06-02 | Staff | 7.0 |
| 3 | 2017-02-03 | Cash Deposit | 3.0 | 3 | 2017-06-03 | Travel | 8.0 |
| 4 | 2017-02-03 | Product Sales | 4.0 | 4 | 2017-10-01 | Test 1 | 10.0 |
| | | | | 5 | 2017-10-02 | Test 2 | 20.0 |
+----+------------+----------------+--------+----+------------+------------+--------+
可以产生类似......
的东西views
在一天结束时,它仍然是一个数据库问题。
为简单起见,您可以创建一个或多个数据库...
String s = generateString();
Log.d(TAG, "result is : " + s);
...
,以便简单地查询
答案 1 :(得分:1)
这是一个解决方案,可以单独提取两个表并将它们集成到一个公共TableModel
中。
public class MyTableModel extends AbstractTableModel implements TableModel {
private List<Data> inclomeList;
private List<Data> expenseList;
private void setData(List<Data> list, Data data) {
int rows = getRowCount();
int row = list.size();
list.add(data);
if(row < rows) {
fireTableRowsUpdated(row, row);
}
else {
fireTableRowsInserted(row, row);
}
}
public void setIncomeData(Data data) {
if(inclomeList == null) {
inclomeList = new ArrayList<>();
}
setData(inclomeList, data);
}
public void setExpenseData(Data data) {
if(expenseList == null) {
expenseList = new ArrayList<>();
}
setData(expenseList, data);
}
@Override
public String getColumnName(int column) {
switch (column) {
case 0:
case 3:
return "Date";
case 1: return "Income";
case 4: return "Expenses";
case 2:
case 5:
return "Amount";
default:
return super.getColumnName(column);
}
}
@Override
public int getRowCount() {
if(inclomeList == null || expenseList == null) {
if(inclomeList != null) {
return inclomeList.size();
}
else if(expenseList != null) {
return expenseList.size();
}
return 0;
}
return Math.max(inclomeList.size(), expenseList.size());
}
@Override
public int getColumnCount() {
return 6;
}
@Override
public Object getValueAt(int row, int column) {
Data inclome = null;
Data expense = null;
if(inclomeList != null && inclomeList.size() > row) {
inclome = inclomeList.get(row);
}
if(expenseList != null && expenseList.size() > row) {
expense = expenseList.get(row);
}
switch (column) {
case 0: return inclome != null ? inclome.getDate() : "";
case 1: return inclome != null ? inclome.getName() : "";
case 2: return inclome != null ? inclome.getAmount() : "";
case 3: return expense != null ? expense.getDate() : "";
case 4: return expense != null ? expense.getName() : "";
case 5: return expense != null ? expense.getAmount() : "";
}
return null;
}
public void update() {
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
Database database = new Database();
inclomeList = database.getData(Database.TBL_INCOME);
expenseList = database.getData(Database.TBL_EXPENSES);
return null;
}
@Override
protected void done() {
try {
get();
fireTableDataChanged();
}
catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
};
worker.execute();
}
}
使用数据库的界面:
public class Database {
public static final String TBL_INCOME = "tbl_income";
public static final String TBL_EXPENSES = "tbl_expenses";
private Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:mysql://192.168.40.5/test", "root", "");
}
public List<Data> getData(String tbl_name) throws SQLException {
try (Connection connection = getConnection()) {
String query = "select * from " + tbl_name;
try(ResultSet rs = connection.createStatement().executeQuery(query)) {
List<Data> list = new ArrayList<>();
while (rs.next()) {
Data data = new Data();
data.setDate(rs.getDate("date"));
data.setName(rs.getString("particular"));
data.setAmount(rs.getDouble("amount"));
list.add(data);
}
return list;
}
}
}
}
MainFrame.java
public class MainFrame extends JFrame {
private JTable table = new JTable(new MyTableModel());
public MainFrame() throws HeadlessException {
super("MainFrame");
createGUI();
}
private void createGUI() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setPreferredSize(new Dimension(600, 400));
JButton addI = new JButton("+");
addI.addActionListener(e -> ((MyTableModel)table.getModel()).setIncomeData(new Data()));
JButton addE = new JButton("+");
addE.addActionListener(e -> ((MyTableModel)table.getModel()).setExpenseData(new Data()));
JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 5, 0));
panel.add(addI);
panel.add(addE);
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
add(panel, BorderLayout.PAGE_END);
pack();
setLocationRelativeTo(null);
((MyTableModel)table.getModel()).update();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new MainFrame().setVisible(true));
}
}