Java SQL从数据库填充表
我在尝试填充jtable时遇到问题。我已经搜索了所有地方,似乎无法找到我的代码的解决方案,我将感谢任何形式的帮助 以下是我获取信息的方法:Java SQL从数据库填充表,java,swing,user-interface,jtable,abstracttablemodel,Java,Swing,User Interface,Jtable,Abstracttablemodel,我在尝试填充jtable时遇到问题。我已经搜索了所有地方,似乎无法找到我的代码的解决方案,我将感谢任何形式的帮助 以下是我获取信息的方法: public class MovieDAO extends Dao implements MovieDAOInterface{ @Override public List<Movie> getAllMovies() { Connection con = null; PreparedStatemen
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());
}
}
}
我已经搜索了所有地方-单击abstracttablemodel标记链接尝试更多。在那里你会发现更多的例子。甚至是默认的表格模型。一般见。有一节介绍如何在AWT线程上创建表模型而不执行对db的查询。使用SwingWorker。就像前面的注释一样。了解表格和表格模型如何在Swing中工作。