首页 > 代码库 > JAVA 表格组件 + rs.beforeFirst()使用

JAVA 表格组件 + rs.beforeFirst()使用

代码:

  1 import java.awt.BorderLayout;
  2 import java.awt.event.ActionEvent;
  3 import java.awt.event.ActionListener;
  4 import java.awt.event.WindowAdapter;
  5 import java.awt.event.WindowEvent;
  6 import java.sql.CallableStatement;
  7 import java.sql.Connection;
  8 import java.sql.ResultSet;
  9 import java.sql.SQLException;
 10 import java.sql.Statement;
 11 
 12 import javax.swing.JButton;
 13 import javax.swing.JFrame;
 14 import javax.swing.JOptionPane;
 15 import javax.swing.JPanel;
 16 import javax.swing.JScrollPane;
 17 import javax.swing.JTable;
 18 import javax.swing.table.DefaultTableModel;
 19 import javax.swing.table.JTableHeader;
 20 
 21 public class TestTable extends JFrame implements ActionListener{
 22 
 23     JScrollPane jsp = new JScrollPane();//放置表格的面板
 24     JTableHeader jth = new JTableHeader();//表头
 25     JTable jtab ;//表格    
 26     JButton jbQuery = new JButton("查询");
 27     JButton jbDel = new JButton("删除");
 28     
 29     GetCon gc = new GetCon();//GetCon为实现了jdbc连接的类,具体参见???
 30     Connection con;
 31     Statement sql;
 32     ResultSet rs;
 33     
 34     public static void main(String[] args) {
 35         new TestTable();
 36     }
 37     
 38     TestTable() {
 39         this.setSize(400,300);
 40         this.setLayout(new BorderLayout());
 41         this.add(jsp,BorderLayout.CENTER);
 42         JPanel j = new JPanel();
 43         j.add(jbQuery);
 44         j.add(jbDel);
 45         this.add(j,BorderLayout.SOUTH);
 46         jbQuery.addActionListener(this);
 47         jbDel.addActionListener(this);
 48         this.addWindowListener(new WindowAdapter() {
 49             public void windowClosing(WindowEvent arg0) {
 50                 System.exit(0);
 51             }            
 52         });            
 53         setVisible(true);    
 54     }
 55 
 56     public void actionPerformed(ActionEvent e) {
 57          if(e.getSource() == jbQuery){            
 58                 try {
 59                     con = gc.getCon();        
 60                     //设置参数使结果集能还原
 61                     sql = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
 62                     //查询数据库MyHotel中Hotel表中的所有信息
 63                     rs = sql.executeQuery("select * from MyHotel..Hotel ");
 64                     int count = 0;//count用来记录表中的行数    
 65                     while(rs.next()){
 66                     count++;
 67                     }
 68                     //建立一个count行3列的数组来装表中数据
 69                     String[][] info = new String[count][3];
 70                     count = 0;
 71                     //之前结果集已经扫描完一遍Hotel表,使用rs.beforeFirst()来使结果集可以重新扫描全表来将数据存入info数组
 72                     rs.beforeFirst();
 73                     while(rs.next()){
 74                     info[count][0] = rs.getString("Hname");
 75                     info[count][1] = rs.getString("Rmtype");
 76                     info[count][2] = rs.getString("Price");
 77                     count++;
 78                     }
 79                     con.close();
 80                 
 81                     String[] title = {"酒店名","房间类型","价格/元"};//title数组用来装表头内容
 82                     //使表格不可编辑
 83                     DefaultTableModel tableModel = new DefaultTableModel(info,title){
 84                         public boolean isCellEditable(int row,int column) {
 85                             return false;
 86                         }
 87                     };
 88                 
 89                     jtab = new JTable(tableModel);
 90                     jth = jtab.getTableHeader();
 91                     jsp.getViewport().add(jtab);
 92                 }catch (SQLException e1) {
 93                     e1.printStackTrace();
 94                 }
 95          }else if(e.getSource() == jbDel) {
 96                  //得到选中的行    
 97                 int row = jtab.getSelectedRow();
 98                 DefaultTableModel model = (DefaultTableModel)jtab.getModel();
 99                 //得到选中行的第一、二列的值
100                 Object hname = model.getValueAt(row, 0);
101                 Object rmtype = model.getValueAt(row, 1);
102                 //从表格中删除选中行
103                 model.removeRow(row);
104                 
105                 try {
106                     con = gc.getCon();    
107                     //调用存储过程删除数据库中相应的行
108                     CallableStatement cs = con.prepareCall("{call dbo.DelFromHotel(?,?)}");
109                     cs.setString(1, hname.toString());
110                     cs.setString(2, rmtype.toString());
111                     cs.executeUpdate();
112                     JOptionPane.showMessageDialog(null, "删除成功!");
113                 } catch (SQLException e1) {
114                     e1.printStackTrace();
115                 }
116             }
117                     
118     }
119     
120 }

 

效果图:

技术分享

 

JAVA 表格组件 + rs.beforeFirst()使用