日期:2014-05-20 浏览次数:20830 次
public class XSTable extends JFrame{
/**
*
*/
private static final long serialVersionUID = 4681526555438628510L;
public static void main(String[] args) {
new XSTable();
}
Connection conn = null ;
Statement st = null ;
ResultSet rs = null;
public XSTable(){
JTable table=new JTable();
DefaultTableModel model = (DefaultTableModel)table.getModel();
final String[][] data={ };
try {
conn =JdbcUtils.getConnection(); //我写了工具类,注册链接释放的代码
st = conn.createStatement();
rs = st.executeQuery("select*from CanKao_table");
while(rs.next()){
sop.Sop("连接成功"); //工具类中的输出控制台
for(int i=1;i<=model.getRowCount();i++){
data[0][1] = "姓名";
data[0][2] = "学号";
data[0][3] = "性别";
data[0][4] = "班级";
data[0][5] = "地址";
data[i][1] = rs.getString(1).trim();
data[i][2] = rs.getString(2).trim();
data[i][3] = rs.getString(3).trim();
data[i][4] = rs.getString(4).trim();
data[i][5] = rs.getString(5).trim();
sop.Sop("连接成功");
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally{
JdbcUtils.free(rs, st, conn);
}
table.setModel(model);
table.setPreferredScrollableViewportSize(new Dimension(500,500));
JScrollPane scrollPane=new JScrollPane(table);
this.getContentPane().add(scrollPane,BorderLayout.CENTER);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
this.pack();
this.setVisible(true);
}
}
public static void main(String[] args) throws Exception{ new XWTable(); }