JFrame访问数据库中未显示的数据

时间:2013-12-20 02:57:15

标签: java swing jdbc jframe windowbuilder

我还有其他类来执行检索数据的任务。我希望我的jframe显示它们但是某个地方有一个很大的问题导致程序显示空白页面! 这是代码:

package mySystem;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.sql.*;
import java.util.ArrayList;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.RowSpec;

public class DisplayCustomers extends JFrame {

    private JPanel contentPane;
    private static Connection con;
    private static ArrayList<Customer> customers;
    private ArrayList<JLabel> name;
    private ArrayList<JLabel> sureName;
    private ArrayList<JButton> accept;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    con = DriverManager.getConnection(
                            "jdbc:mysql://my_url", 
                            "user", "pass");
                    Item item = new Item(1987,con);
                    customers = LookUp.findCustomers(item.getId(), con);
                    DisplayCustomers frame = new DisplayCustomers(customers,item,con);
                    frame.setVisible(true);

                } catch (SQLException ex){
                    ex.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();

                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public DisplayCustomers(ArrayList<Customer> customers,Item item,Connection con) 
            throws SQLException {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 583, 409);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(new GridLayout(customers.size(), 4, 2, 5));

        name = new ArrayList<JLabel>();
        sureName = new ArrayList<JLabel>();
        accept = new ArrayList<JButton>();
        Customer buff = null;

        for(int i=0;i < customers.size();i++) {
            buff = customers.get(i); // for the sake of readability

            name.add(new JLabel(buff.getName()));
            sureName.add(new JLabel(buff.getSureName()));
            String butMsg = "Accept";
            if(buff.isValid())
                butMsg = "Customer is invlid";
            accept.add(new JButton(butMsg));
            contentPane.add(name.get(i));
            contentPane.add(sureName.get(i));
            contentPane.add(accept.get(i));


        }
        con.close();


    }
    public void displayError(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 583, 409);
        contentPane = new JPanel();
        setContentPane(contentPane);
        JLabel error = new JLabel("Exception Thrown");
        contentPane.add(error);
    }
}

我继续使用windowbuilder运行它,但按钮说它没有编译,它只是预览窗口。所以我不确定如何正确运行它!此外,当我在构造函数中放置新的Connection时,它仍然提供有关未找到驱动程序的错误,即使它已作为.jar文件添加到我的构建路径中,并且每个其他非Jframe类运行正常。这是我第一次使用Jframe,它给了我一个非常艰难的时间。任何帮助表示赞赏。

0 个答案:

没有答案