SQL从数据库填充表

时间:2014-10-29 11:54:44

标签: java swing user-interface jtable abstracttablemodel

我在尝试填充jtable时遇到问题。我已经搜遍过,似乎无法找到我的代码的解决方案,我将不胜感激任何帮助。

以下是获取信息的方法:

public class MovieDAO extends Dao implements MovieDAOInterface{

    @Override
    public List<Movie> getAllMovies() {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Movie> movies = new ArrayList<>();

        try {
            con = getConnection();

            String query = "Select * from Movie";
            ps = con.prepareStatement(query);
            rs = ps.executeQuery();

            while (rs.next()) {
                Movie AllMovies = new Movie(rs.getInt("MovieId"), rs.getString("MovieTitle"), rs.getString("MovieGenre"), rs.getInt("MovieLicence"));
                movies.add(AllMovies);
            }
        } catch (SQLException e) {
            System.out.println("Exception occured in the getAllMovies() method");
            e.getMessage();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (ps != null) {
                    ps.close();
                }
                if (con != null) {
                    freeConnection(con);
                }
            } catch (SQLException e) {
                System.out.println("Exception occured in the finally section of the getAllMovies() method");
                e.getMessage();
            }
        }

        return movies;
    }
}

这是我打电话的地方,并且问题不会显示在桌子上;

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)    {                                         

    System.out.println("Test Find All Movies");
    MovieDAO dao = new MovieDAO();
    List<Movie> movies = dao.getAllMovies();
    if (movies.isEmpty()) {
        System.out.println("List is empty");
    } else {
        for (Movie m : movies) {

            DisplayMovies.setModel(m.toString());
        }
    }

}              

0 个答案:

没有答案