java—单击按钮不会从数据库检索数据

fv2wmkja  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(291)

所以我试着制作一个框架,当你点击按钮后,它会显示数据库中每个项目的列表,但实际上当我点击它时,什么都不会显示。

public ArrayList show(JTextArea tData) {
        ArrayList<Book> books=new ArrayList<>();
        try{
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            polaczenie =DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=master",
                    "sa", "student");
            try(Statement stmt = connection.createStatement();) {
                ResultSet rs = stmt.executeQuery("{call dbo.showBook}");
                while (rs.next()) {
                        books.add(new Book(
                           rs.getString("bookNumber"),
                           rs.getString("title"),
                           rs.getString("author"),
                           rs.getString("publicationYear"),
                           rs.getString("publisher"))
                    );
                }
            }                        
        }   
        catch (ClassNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Error "+ex.getMessage(), "Error", 0);
        }  
        catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Error "+ex.getMessage(), "Error", 0);
        }  
        return books;
    }
private void bShowActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        DataBook db=new DataBook();
        db.show(tData);
    }
wljmcqd8

wljmcqd81#

你的 show 方法将返回 ArrayList<Book> 所以:
您是否已处理此项以显示列表中的数据?
你的数据库有数据吗?
“抱歉,我不能添加评论,因为我的声誉<50”希望它能帮助你。

package test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class App extends JFrame {
    JPanel contentPane;
    JButton action;
    JList<Book> listBooks;

    public App() {
        this.prepareGUI();
    }

    public void prepareGUI() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(200,  200);

        contentPane = new JPanel();
        this.setContentPane(contentPane);

        action = new JButton("Action");
        contentPane.add(action);

        listBooks = new JList<Book>();
        contentPane.add(listBooks);

        action.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // Data
                Book[] books = new Book[5];
                for (int i = 0; i < books.length; i++) {
                    books[i] = new Book("book " + i, "@@");
                }

                listBooks.setListData(books);

            }
        });
    }

    public static void main(String[] args) {
        App app = new App();

        app.setVisible(true);
    }

}

希望这对你有帮助!!!

相关问题