JAVA Table Components + rs.beforefirst () use

Source: Internet
Author: User

Code:

1 Importjava.awt.BorderLayout;2 Importjava.awt.event.ActionEvent;3 ImportJava.awt.event.ActionListener;4 ImportJava.awt.event.WindowAdapter;5 Importjava.awt.event.WindowEvent;6 Importjava.sql.CallableStatement;7 Importjava.sql.Connection;8 ImportJava.sql.ResultSet;9 Importjava.sql.SQLException;Ten Importjava.sql.Statement; One  A ImportJavax.swing.JButton; - ImportJavax.swing.JFrame; - ImportJavax.swing.JOptionPane; the ImportJavax.swing.JPanel; - ImportJavax.swing.JScrollPane; - Importjavax.swing.JTable; - ImportJavax.swing.table.DefaultTableModel; + ImportJavax.swing.table.JTableHeader; -  +  Public classTestTableextendsJFrameImplementsactionlistener{ A  atJScrollPane jsp =NewJScrollPane ();//the panel where the table is placed -Jtableheader jth =NewJtableheader ();//Table Header -JTable Jtab;//Table -JButton Jbquery =NewJButton ("Query"); -JButton Jbdel =NewJButton ("Delete"); -      inGetcon GC =NewGetcon ();//Getcon is the class that implements the JDBC connection, see????  - Connection con; to Statement SQL; + ResultSet rs; -      the      Public Static voidMain (string[] args) { *         Newtesttable (); $     }Panax Notoginseng      - testtable () { the          This. SetSize (400,300); +          This. setlayout (NewBorderLayout ()); A          This. Add (jsp,borderlayout.center); theJPanel j =NewJPanel (); + J.add (jbquery); - J.add (Jbdel); $          This. Add (J,borderlayout.south); $Jbquery.addactionlistener ( This); -Jbdel.addactionlistener ( This); -          This. Addwindowlistener (NewWindowadapter () { the              Public voidwindowclosing (windowevent arg0) { -System.exit (0);Wuyi             }             the         });  -SetVisible (true);  Wu     } -  About      Public voidactionperformed (ActionEvent e) { $          if(E.getsource () = =jbquery) {             -                 Try { -Con =Gc.getcon ();  -                     //setting parameters to enable the result set to be restored Asql =con.createstatement (resultset.type_scroll_insensitive,resultset.concur_read_only); +                     //querying all information in the database Myhotel in the hotel table thers = Sql.executequery ("SELECT * from Myhotel. Hotel "); -                     intCount = 0;//count is used to record the number of rows in a table $                      while(Rs.next ()) { thecount++; the                     } the                     //Create an array of Count row 3 columns to fit the data in the table theString[][] Info =NewString[count][3]; -Count = 0; in                     //before the result set has been scanned through the hotel table, use Rs.beforefirst () to enable the result set to rescan the full table to store the data in the info array the Rs.beforefirst (); the                      while(Rs.next ()) { AboutInfo[count][0] = rs.getstring ("Hname"); theINFO[COUNT][1] = rs.getstring ("Rmtype"); theINFO[COUNT][2] = rs.getstring ("Price"); thecount++; +                     } - con.close (); the                 BayiString[] title = {"Hotel name", "Room Type", "Price/Yuan"};//The title array is used to load the contents of the table header the                     //make a table non-editable theDefaultTableModel TableModel =NewDefaultTableModel (info,title) { -                          Public BooleanisCellEditable (intRowintcolumn) { -                             return false; the                         } the                     }; the                  theJtab =NewJTable (TableModel); -jth =Jtab.gettableheader (); the Jsp.getviewport (). Add (jtab); the}Catch(SQLException E1) { the e1.printstacktrace ();94                 } the}Else if(E.getsource () = =Jbdel) { the                  //Get the selected row the                 introw =Jtab.getselectedrow ();98DefaultTableModel model =(DefaultTableModel) Jtab.getmodel (); About                 //gets the value of the first to second column of the selected row -Object hname = model.getvalueat (row, 0);101Object Rmtype = model.getvalueat (row, 1);102                 //Remove the selected row from the table103 model.removerow (row);104                  the                 Try {106Con =Gc.getcon (); 107                     //call a stored procedure to delete the corresponding row in the database108CallableStatement cs = Con.preparecall ("{call dbo. Delfromhotel (?,?)} ");109Cs.setstring (1, hname.tostring ()); theCs.setstring (2, rmtype.tostring ());111 cs.executeupdate (); theJoptionpane.showmessagedialog (NULL, "Delete success! ");113}Catch(SQLException E1) { the e1.printstacktrace (); the                 } the             }117                     118     }119      -}

:

JAVA Table Components + rs.beforefirst () use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.