Java编程怎么把list里的数据加到JTable表格呢
要做一个图书管理系统,下面是我写的一个dao类里的获取全部图书的方法,现在我想用BookManger类里用一个JTable表格显示,该怎么实现,网上看了很多例子看不懂啊,...
要做一个图书管理系统,下面是我写的一个dao类里的获取全部图书的方法,现在我想用 BookManger类里用一个JTable表格显示,该怎么实现,网上看了很多例子看不懂啊,求大神给代码
public List getAllBook(){
List list = new ArrayList();
Connection con = null;
PreparedStatement pt = null;
ResultSet rs = null;
boolean flag = false;
con = Conn.getConnection();
try {
pt = con.prepareStatement("select * from booksm");
rs = pt.executeQuery();
while(rs.next()){
Book book = new Book();
book.setId(rs.getInt(1));
book.setISBNNum(rs.getInt(2));
book.setName(rs.getString(3));
book.setAuthor(rs.getString(4));
book.setPress(rs.getString(5));
book.setType(rs.getString(6));
book.setPrice(rs.getDouble(7));
book.setInventory(rs.getInt(8));
book.setSum(rs.getInt(9));
list.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
} 展开
public List getAllBook(){
List list = new ArrayList();
Connection con = null;
PreparedStatement pt = null;
ResultSet rs = null;
boolean flag = false;
con = Conn.getConnection();
try {
pt = con.prepareStatement("select * from booksm");
rs = pt.executeQuery();
while(rs.next()){
Book book = new Book();
book.setId(rs.getInt(1));
book.setISBNNum(rs.getInt(2));
book.setName(rs.getString(3));
book.setAuthor(rs.getString(4));
book.setPress(rs.getString(5));
book.setType(rs.getString(6));
book.setPrice(rs.getDouble(7));
book.setInventory(rs.getInt(8));
book.setSum(rs.getInt(9));
list.add(book);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
} 展开
1个回答
推荐于2017-11-27
展开全部
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.table.AbstractTableModel;
public class BookTableModel extends AbstractTableModel {
private List<Book> data = new ArrayList<Book>();
@Override
public int getRowCount() {
return data.size();
}
@Override
public int getColumnCount() {
//根据实际情况返回列数
return 3;
}
@Override
public String getColumnName(int column) {
//根据实际情况返回列名
if (column == 0)
return "ID";
else if (column == 1)
return "ISBNNum";
return "Name";
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Book book = data.get(rowIndex);
if (columnIndex == 0)
return book.getId();
else if (columnIndex == 1)
return book.getISBNNum();
return book.getName();
}
public void setData(List<Student> data) {
if (data == null)
throw new IllegalArgumentException("参数data不能为null。");
this.data = data;
fireTableDataChanged();
}
}
BookTableModel tableModel = new BookTableModel ();
JTable table = new JTable(tableModel);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询