存储数百个表/模型

时间:2017-06-02 18:37:10

标签: java database

我知道这些信息无处不在,但它很难选择正确的方法。所以即使把我指向正确的地方也是如此。

我正在为他输入股票交易的客户开发软件,并将各种数据输出到表格中。没有任何类型的访问库存数据,它都是内部的,仅供他个人使用。

我坚持的是他希望能够在周一到周五进行洗牌,或查看一年前输入的数据。那么我将如何存储每日表数据。

我应该将表格模型保存到文件还是数据库?也许我不知道有不同的方法。

如果这对任何人都很重要,那么这些表是10列宽,大约有900行。

编辑*

在这里回答更多代码的请求是我已经解决的问题。

MainFrame() {
    super("Stack Overflow Stock Software");

    model.addColumn("Exchange");
    model.addColumn("Inv/Sec");
    model.addColumn("Name");
    model.addColumn("Qty");
    // model.addColumn("Fd");
    model.addColumn("Average Cost");
    model.addColumn("Market");
    model.addColumn("Balance");
    // model.addColumn("Interest");
    model.addColumn("Today's PL");
    model.addColumn("Total Pl");
    // model.addColumn("Multiplier");
    // model.addRow(data);
    table = new JTable(model);

    modelTotal.addColumn("Balance");
    modelTotal.addColumn("Today PL");
    modelTotal.addColumn("Total PL");
    table2 = new JTable(modelTotal);

    setLayout(new BorderLayout());

    // JComboBox<String> comboBox = new JComboBox<>(exchangeStrings);
    // sorter = new TableRowSorter<TableModel>(table.getModel());

    JPanel panel1 = new JPanel();
    text1 = new JTextField(5);
    text1.setText("ABA");
    text1.setHorizontalAlignment(SwingConstants.CENTER);
    text2 = new JTextField(12);
    text2.setText("1000");// quanity
    text2.setHorizontalAlignment(SwingConstants.CENTER);
    text3 = new JTextField(10);
    text3.setText("10.00");// price
    text3.setHorizontalAlignment(SwingConstants.CENTER);

    button1 = new JButton("Buy");
    button1.addActionListener(this);
    button2 = new JButton("Sell");
    button2.addActionListener(this);

    JScrollPane scrollPane = new JScrollPane(table);
    JScrollPane scrollPane2 = new JScrollPane(table2);

    // sets table settings
    table.setFillsViewportHeight(true);
    table.getColumnModel().getColumn(0).setMaxWidth(80);
    table.getColumnModel().getColumn(1).setPreferredWidth(80);
    table.getColumnModel().getColumn(2).setPreferredWidth(180);
    table.getColumnModel().getColumn(4).setPreferredWidth(24);
    table.getColumnModel().getColumn(6).setPreferredWidth(24);
    table.getColumnModel().getColumn(7).setPreferredWidth(24);
    // table.getColumnModel().getColumn(9).setPreferredWidth(0);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
    setCellsAlignment(table, SwingConstants.CENTER);

    setCellsAlignment(table2, SwingConstants.CENTER);

    // add panels/buttons/etc to page
    add(panel1, BorderLayout.PAGE_START);
    panel1.setPreferredSize(new Dimension(1280, 50));
    add(scrollPane, BorderLayout.CENTER);
    add(scrollPane2, BorderLayout.PAGE_END);
    scrollPane2.setPreferredSize(new Dimension(1280, 50));

    // add(scrollPane, BorderLayout.CENTER);
    panel1.add(comboBox, BorderLayout.SOUTH);
    panel1.add(text1, BorderLayout.SOUTH);
    panel1.add(text3, BorderLayout.SOUTH);// price
    panel1.add(text2, BorderLayout.SOUTH);
    panel1.add(button1, BorderLayout.NORTH);
    panel1.add(button2, BorderLayout.NORTH);

    setMenuBar();
    modelTotal.addRow(data2);

    setExtendedState(MAXIMIZED_BOTH);
    setMinimumSize(new Dimension(1280, 485));
    setSize(640, 485);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

}

private void setMenuBar() {...}
   /*Set the menu bar up*/

public static void setCellsAlignment(JTable table, int alignment) {
    DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
    rightRenderer.setHorizontalAlignment(alignment);

    TableModel tableModel = table.getModel();

    for (int columnIndex = 0; columnIndex < tableModel.getColumnCount(); columnIndex++) {
        table.getColumnModel().getColumn(columnIndex).setCellRenderer(rightRenderer);
    }
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource() == button1) {// if button 1 pressed to buy
        purchaseMade = true;

        // input data in data object for adding or combining.
        String comboString = comboBox.getSelectedItem().toString();
        String stringCombo = comboString;
        data[0] = stringCombo;
        data[1] = text1.getText();
        data[2] = text1.getText(); // turn into long string
        data[3] = text2.getText();// quanity
        data[5] = text3.getText();// price
        data[4] = text3.getText();// average market price
        double String1 = Double.parseDouble(data[3].toString());
        double String2 = Double.parseDouble(data[4].toString());
        data[6] = String1 * String2;

        double dataQuanity = Double.parseDouble(data[3].toString());
        double dataPrice = Double.parseDouble(data[4].toString());
        data[7] = dataQuanity * dataPrice * -1;
        data[8] = dataQuanity * dataPrice * -1;

        data[4] = Double.parseDouble(data[4].toString());
        data[4] = df.format(data[4]);

        data[5] = Double.parseDouble(data[5].toString());
        data[5] = df.format(data[5]);

        data[6] = Double.parseDouble(data[6].toString());
        data[6] = df.format(data[6]);

        data[7] = Double.parseDouble(data[7].toString());
        data[7] = df.format(data[7]);

        data[8] = Double.parseDouble(data[8].toString());
        data[8] = df.format(data[8]);

        // add data and continue on
        model.addRow(data);
        dataCount++;
        combineData(model);
        sortData(table);
    }

    if (e.getSource() == button2) {
        purchaseMade = false;

        // input data in data object for adding or combining.
        data[0] = comboBox.getSelectedItem();// exchange
        data[1] = text1.getText();// symbol
        data[2] = text1.getText(); // turn into long string//name
        data[3] = text2.getText();// quanity
        data[4] = text3.getText();// average price
        data[5] = text3.getText();// closing bid / market
        data[9] = 1;

        // data 5 can wait
        double String1 = Double.parseDouble(data[3].toString());
        double String2 = Double.parseDouble(data[4].toString());
        data[6] = String1 * String2;
        // pl
        double dataQuanity = Double.parseDouble(data[3].toString());
        double dataPrice = Double.parseDouble(data[4].toString());
        data[7] = dataQuanity * dataPrice * -1;
        data[8] = dataQuanity * dataPrice * -1;

        // add data and continue on
        dataCount++;
        combineData(model);
        sortData(table);
    }

}

private void combineData(DefaultTableModel model2) {
    /*This section of codes adds the new data to a table or combines it with a duplicate entry*/
}

private void sortData(JTable table) {...}
     /*This is where I sort my table*/

private void calculateLowerData(DefaultTableModel lowerModel, DefaultTableModel dataModel) {...} 
    /* This is where I calculate a number not relevant to the question*/


private void printTable() {...}
    /* This is where I print my data to a table */

}`

1 个答案:

答案 0 :(得分:1)

在偏好关系数据库中选择您更熟悉的数据库,并使用日期时间列作为索引以加快搜索速度

使用数据库更好,因为您可以获取指定参数的数据

如果仅供个人使用,您可以选择您正在使用的操作系统的本地数据库,例如SQL Server,并确保执行备份

拥抱