Java,MySQL - 在GUI中显示数据库命令

时间:2016-04-02 12:01:02

标签: java mysql swing user-interface

我想在我的程序(查看汽车)中创建按钮,该按钮将显示带表的新JFrame。我想用MySQL数据库中的数据填充表格。当我启动程序时,它可以正常工作,但是当我尝试单击应该显示我的JFrame表的按钮时,它会显示一些我不理解的错误。我在Java中比较新,所以如果我的代码中有一些愚蠢的东西,那就很抱歉。你能简单解释一下我的错误以及我应该改变什么吗? (抱歉我的英语不好,这不是我的第一语言)

Main JFrame

主要课程代码:

public class MyJFrame {

    private JFrame frame;

    public static void main(String[] args) {
        String dbHost="localhost";
        String dbDatabase="cars";
        String dbUser = "root";
        String dbPassword = "";
        ResultSet result = null;

        try {
            // register driver
            Class.forName("com.mysql.jdbc.Driver");
            // Make Connection Url
            String connectionUrl = "jdbc:mysql://" + dbHost
                        + "/" + dbDatabase
                        + "?user=" + dbUser
                        + "&password=" + dbPassword;
            //open Connection
            Connection conn = DriverManager.getConnection(connectionUrl);
            // Code to create sql and run it will go here
            // create SQL
            if(CarDAO.option == 1){

            }else if(CarDAO.option == 2){
                String sql = CarDAO.sql;
                // prepare Statement
                PreparedStatement ps = conn.prepareStatement(sql);
                ResultSet rs = ps.executeQuery();
            }
            // prepare Statement
            //PreparedStatement ps = conn.prepareStatement(sql);
            // execute SQL
            //DatabaseMetaData meta = conn.getMetaData();
            // close connection
            conn.close();
        }catch (ClassNotFoundException cnfe){
            throw new RuntimeException(cnfe);
        }catch (SQLException sqle) {
        throw new RuntimeException(sqle);
        }


        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MyJFrame window = new MyJFrame();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    public MyJFrame() {
        initialize();
    }


    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 472, 346);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JButton btnNewButton = new JButton("Create Car");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                frame.dispose();
                CreateCar createCar = new CreateCar();
                createCar.setVisible(true);
            }
        });
        btnNewButton.setBounds(10, 125, 135, 46);
        frame.getContentPane().add(btnNewButton);

        JButton btnNewButton_1 = new JButton("Search Car For Sale");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        btnNewButton_1.setBounds(10, 68, 135, 46);
        frame.getContentPane().add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("Update Entry");
        btnNewButton_2.setBounds(10, 182, 135, 46);
        frame.getContentPane().add(btnNewButton_2);

        JButton btnNewButton_4 = new JButton("Sold Car");
        btnNewButton_4.setBounds(10, 239, 135, 46);
        frame.getContentPane().add(btnNewButton_4);

        JButton btnNewButton_3 = new JButton("View Cars");
        btnNewButton_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                frame.dispose();
                ShowCars showCars = new ShowCars();
                showCars.setVisible(true);
            }
        });
        btnNewButton_3.setBounds(10, 11, 135, 46);
        frame.getContentPane().add(btnNewButton_3);

        JLabel lblNewLabel = new JLabel("Choose your option");
        lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 17));
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setBounds(160, 11, 190, 46);
        frame.getContentPane().add(lblNewLabel);
    }
}

ShowCars JFrame代码:

public class ShowCars extends JFrame {

    private JPanel contentPane;
    private JTable table;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ShowCars frame = new ShowCars();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ShowCars() {
        CarDAO carDAO = new CarDAO();
        table = new JTable();
        carDAO.showCars(table);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 489, 400);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(10, 11, 453, 339);
        contentPane.add(scrollPane);

        table = new JTable();
        scrollPane.setViewportView(table);
    }
}

carDAO.showCars方法:

public void showCars (JTable table){
    try{
        Connection conn = null;
        String sql = "select * from cars";
        // prepare Statement
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        table.setModel(DbUtils.resultSetToTableModel(rs));
    }catch (Exception e) {
        e.printStackTrace();
    }
    option = 2;
}

错误消息:

java.lang.NullPointerException
    at databaseProject.CarDAO.showCars(CarDAO.java:64)
    at databaseProject.ShowCars.<init>(ShowCars.java:44)
    at databaseProject.MyJFrame$4.actionPerformed(MyJFrame.java:117)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

0 个答案:

没有答案