The darkroom meets the lights on time.
Import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.Vector;import javax.swing.*;import javax.swing.table.DefaultTableModel / / layout manager of the form: boundary, grid, / / the table model collection class connects to the database / / what actually stores data is the table model public class W extends JFrame implements ActionListener {/ / Parameter properties JButton an1; JLabel bq; JTextField wbk; JPanel mb;// panel composition: container, put components, the default layout manager is: streaming layout JTable bg; JScrollPane gd; Vector jilu,ziduan; Connection con;// connection database PreparedStatement ps / execute SQl statement ResultSet rs;// DefaultTableModel bgmx; public W () {/ / Northern an1=new JButton ("query"); an1.addActionListener (this); bq=new JLabel ("Please enter user name:"); wbk=new JTextField (10); mb=new JPanel (); mb.add (bq); mb.add (wbk); mb.add (an1); this.add (mb,BorderLayout.NORTH); / / Central ziduan=new Vector () Ziduan.add ("student number"); ziduan.add ("name"); ziduan.add ("gender"); ziduan.add ("age"); ziduan.add ("place of origin"); ziduan.add ("department name"); jilu=new Vector (); / / connect to the database try {Class.forName ("com.mysql.jdbc.Driver"); / / load driver String url= "jdbc:mysql://localhost:3306/student"; String user= "root" String password= "000000"; con=DriverManager.getConnection (url, user, password); if (contextual null) {System.out.println ("Database connection successful");} ps=con.prepareStatement ("delete from c where xuehao='xs002'"); ps.executeUpdate (); ps=con.prepareStatement ("select * from c"); rs=ps.executeQuery (); while (rs.next ()) {Vector hang=new Vector (); hang.add (rs.getString (1)) Hang.add (rs.getString (2)); hang.add (rs.getString (3)); hang.add (rs.getInt (4)); hang.add (rs.getString (5)); hang.add (rs.getString (6)); jilu.add (hang);} catch (Exception e) {} finally {try {if (rswatches null) {rs.close ();} if (psometric null) {ps.close () } if (contextual null) {con.close ();} catch (Exception e) {}} bgmx=new DefaultTableModel (jilu,ziduan); bg=new JTable (bgmx); gd=new JScrollPane (bg); this.add (gd); this.setTitle ("this is a form"); this.setSize (350350); this.setVisible (true); this.setDefaultCloseOperation (EXIT_ON_CLOSE);} public static void main (String [] args) {new W () } @ Override public void actionPerformed (ActionEvent e) {/ / TODO Auto-generated method stub if (e.getSource () = = an1) {JOptionPane.showConfirmDialog (this, "are you sure you want to close the horse?") ;}