该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
try {
//获取连接
Class.forName("com.mysql.jdbc.Driver");
Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/test_db","root","123456");
//建立查询条件
String sql = "select * from test_image";
PreparedStatement pstm = conn.prepareStatement(sql);
//执行查询
ResultSet rs = pstm.executeQuery();
// 计算有多少条记录
int count = 0;
while(rs.next()){
count++;
}
rs = pstm.executeQuery();
// 将查询获得的记录数据,转换成适合生成JTable的数据形式
Object[][] info = new Object[count][4];
count = 0; while(rs.next()){
info[count][0] = rs.getString("imgname");
info[count][1] = rs.getBlob("img");
info[count][2] = rs.getString("jdnum");
info[count][3] = rs.getString("jdtime");
count++;
}
JFrame f = new JFrame();
String[] title = {"图片名","结果图片","耗时","角点数"};
JTable table = new JTable(info, title);
JTableHeader jth = table.getTableHeader(); table.setPreferredScrollableViewportSize(new Dimension(600, 300));
JScrollPane scrollPane = new JScrollPane(table);
f.getContentPane().add(scrollPane, BorderLayout.CENTER);
f.setTitle("表格测试窗口");
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
} catch(ClassNotFoundException cnfe){
JOptionPane.showMessageDialog(null,"数据源错误","错误",JOptionPane.ERROR_MESSAGE);
}catch(SQLException sqle){
JOptionPane.showMessageDialog(null,"数据操作错误","错误",JOptionPane.ERROR_MESSAGE);
} }