使用多个结果集java jdbc

时间:2016-06-27 16:22:56

标签: java jdbc

我使用了四个不同的结果集和四个单独的语句来创建表。其中一个结果集工作正常,而另一个给出空值。 resultset1仅给出集合中的第一个值。有什么方法可以解决这个问题吗?

以下是代码:

package userInterface;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

import reu.FileMod;
import reu.ImageSend;
import reu.ColumnAutoSizer;

public class TabOne extends JPanel implements ActionListener {
private JButton button1, button2;
public static JTable table;
private ImageSend img;
private JPanel orderPanel;
private Connection connection;
private ResultSet result1, result2, result3, result4;
private String[] teacher, hall, batch, days;

public TabOne() {

    img = new ImageSend();
    orderPanel = new JPanel();
    button1 = new JButton("Save");
    button2 = new JButton("Refresh");
    button2.setIcon(img.createImage("/image/update.png"));
    button2.setBorderPainted(false);
    button2.setFocusPainted(false);
    button2.setContentAreaFilled(false);

    button1.setIcon(img.createImage("/image/small_save.png"));
    button1.setBorderPainted(false);
    button1.setFocusPainted(false);
    button1.setContentAreaFilled(false);
    button1.addActionListener(this);
    table = new JTable();
    createTabOne();

}

private void createTabOne() {

    setOrderPanel();

    setLayout(new BorderLayout());

    // panel border

    setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    Dimension dim = getPreferredSize();
    dim.height = 100;

    // panel color

    setBackground(new Color(46, 139, 87));

    table.setRowHeight(22);
    table.setFont(new Font("times new roman", Font.PLAIN, 16));
    table.getTableHeader().setFont(
            new Font("times new roman", Font.BOLD, 16));
    /*
     * table.getModel().addTableModelListener(new TableModelListener() {
     * public void tableChanged(TableModelEvent e) {
     * ColumnAutoSizer.sizeColumnsToFit(table); } });
     */
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    add(new JScrollPane(table), BorderLayout.CENTER);

    // Table Mod

    orderPanel.setPreferredSize(dim);
    add(orderPanel, BorderLayout.SOUTH);

}

private void setOrderPanel() {

    Border inner = BorderFactory.createTitledBorder("Order Section");
    Border outer = BorderFactory.createEmptyBorder(5, 5, 5, 5);

    orderPanel.setBorder(BorderFactory.createCompoundBorder(outer, inner));

    orderPanel.setLayout(new GridBagLayout());

    orderPanel.setBackground(new Color(147, 197, 114));

    GridBagConstraints gc = new GridBagConstraints();

    gc.weightx = 0;
    gc.weighty = 0;
    gc.gridx = 1;
    gc.gridy = 1;
    gc.fill = GridBagConstraints.NONE;

    button1.setFont(new Font("times new roman", Font.PLAIN, 15));
    orderPanel.add(button1, gc);

    // gc.weightx = 0.5;
    // gc.weighty = 0.5;
    gc.gridx = 2;
    gc.gridy = 1;
    button2.setFont(new Font("times new roman", Font.PLAIN, 15));
    orderPanel.add(button2, gc);

}

public void actionPerformed(ActionEvent e) {

    /*
     * JButton ob = (JButton) e.getSource(); FileMod.saveFile();
     */

    exeQuery();
}

private void disconnect() throws Exception {
    if (connection != null)
        connection.close();

}

private void connect() throws Exception {
    Class.forName("com.mysql.jdbc.Driver");

    String url = "jdbc:mysql://localhost:3306/school";

    connection = DriverManager.getConnection(url, "root", "");

}

public void exeQuery() {

    try {

        connect();

        String day, hall, batch, name;

        DefaultTableModel tableModel;
        String[] head = { "Day", "Hall Name", "Batch", "Lecturer Name" };
        tableModel = new DefaultTableModel() {
            public boolean isCellEditable(int arg0, int arg1) {
                return false;
            };
        };
        tableModel.setColumnIdentifiers(head);
        table.setModel(tableModel);

        day = null;
        name = null;
        hall = null;
        batch = null;

        getTeacher();
        getHall();
        getBatch();
        getDays();

        while (result4.next()) {

            day = result4.getString(2);

            while (result2.next()) {

                hall = result2.getString(2);

                if (result3.next()) {
                    batch = result3.getString(2);
                } else {
                    batch = "null";
                }

                if (result1.next()) {
                    name = result1.getString(3);
                    name = name + " " + result1.getString(2);
                } else {
                    name = "null";
                }
            }

            tableModel.addRow(new String[] { day, hall, batch, name });
        }

        disconnect();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void getTeacher() throws Exception {

    PreparedStatement search1 = connection
            .prepareStatement("select * from lecturer");
    result1 = search1.executeQuery();

}

public void getHall() throws Exception {

    PreparedStatement search2 = connection
            .prepareStatement("select * from hall");
    result2 = search2.executeQuery();

}

public void getBatch() throws Exception {

    PreparedStatement search3 = connection
            .prepareStatement("select * from batch");
    result3 = search3.executeQuery();

}

public void getDays() throws Exception {

    PreparedStatement search4 = connection
            .prepareStatement("select * from day_of_week");
    result4 = search4.executeQuery();

}

}

结果:

enter image description here

0 个答案:

没有答案