Java 操作MySQL数据库

用来存储数据的类

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo;

/**
 *
 * @author Zhang Tingkuo
 */
public class Country {

    private String code;
    private String name;
    private String continent;
    private int population;

    public Country() {
    }

    public Country(String code, String name, String continent, int population) {
        this.code = code;
        this.name = name;
        this.continent = continent;
        this.population = population;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContinent() {
        return continent;
    }

    public void setContinent(String continent) {
        this.continent = continent;
    }

    public int getPopulation() {
        return population;
    }

    public void setPopulation(int population) {
        this.population = population;
    }
}

用来操作数据库的类

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package demo;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author Zhang Tingkuo
 */
public class Manager {

    private String code;
    private String name;
    private String continent;
    private String population;
    private String node;
    private ArrayList<Country> countryList = new ArrayList<Country>();

    public Manager(String code, String name, String continent, String population, String node) {
        this.code = code;
        this.name = name;
        this.continent = continent;
        this.population = population;
        this.node = node;
    }

    public void Insert(String code, String name, String continent, String population) throws Exception {

        //0.SQL
        String sql = "Insert into Country(Code,Name,Continent,Population) values(?,?,?,?)";
        System.out.println("sql successfully.");
        logFile("sql successfully.");

        //1.Driver
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("driver successfully.");
        logFile("driver successfully.");

        //2.Connection
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/world", "admin", "admin");
        System.out.println("connecte successfully.");
        logFile("connecte successfully.");

        //3.Statement
        PreparedStatement statement = connection.prepareStatement(sql);
        System.out.println("statement successfully.");
        logFile("statement successfully.");

        //4.Result
        statement.setString(1, code);
        statement.setString(2, name);
        statement.setString(3, continent);
        statement.setString(4, population);
        System.out.println(statement);
        logFile("" + statement);

        //5
        statement.executeUpdate();
        System.out.println("insert successfully.");
        logFile("insert successfully.");

        //6.Close
        statement.close();
        connection.close();
        System.out.println("close successfully.");
        logFile("close successfully.");
    }

    public void Delete(String code, String name, String continent, String population) throws Exception {

        //0.SQL
        String sql = "delete from Country where Code = ?";
        System.out.println("sql successfully.");
        logFile("sql successfully.");

        //1.Driver
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("driver successfully.");
        logFile("driver successfully.");

        //2.Connection
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/world", "admin", "admin");
        System.out.println("connecte successfully.");
        logFile("connecte successfully.");

        //3.Statement
        PreparedStatement statement = connection.prepareStatement(sql);
        System.out.println("statement successfully.");
        logFile("statement successfully.");

        //4.Result
        statement.setString(1, code);
        System.out.println(statement);
        logFile("" + statement);


        //5
        statement.executeUpdate();
        System.out.println("delete successfully.");
        logFile("delete successfully.");

        //6.Close
        statement.close();
        connection.close();
        System.out.println("close successfully.");
        logFile("close successfully.");
    }

    public void Update(String code, String name, String continent, String population) throws Exception {

        //0.SQL
        String sql = "Update Country set Code = ?, Name = ?, Continent = ?, Population = ? where Code = ?";
        System.out.println("sql successfully.");
        logFile("sql successfully.");

        //1.Driver
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("driver successfully.");
        logFile("driver successfully.");

        //2.Connection
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/world", "admin", "admin");
        System.out.println("connecte successfully.");
        logFile("connecte successfully.");

        //3.Statement
        PreparedStatement statement = connection.prepareStatement(sql);
        System.out.println("statement successfully.");
        logFile("statement successfully.");


        //4.Result
        statement.setString(1, code);
        statement.setString(2, name);
        statement.setString(3, continent);
        statement.setString(4, population);
        statement.setString(5, code);
        System.out.println(statement);
        logFile("" + statement);

        //5
        statement.executeUpdate();
        System.out.println("update successfully.");
        logFile("update successfully.");

        //6.Close
        statement.close();
        connection.close();
        System.out.println("close successfully.");
        logFile("close successfully.");
    }

    public void Seach(String code, String name, String continent, String population, String node) throws Exception {

        //0.SQL
        String sql = "Select Code,Name,Continent,Population From Country where " + node + " like ? order by Name";
        System.out.println("sql successfully.");
        logFile("sql successfully.");

        //1.Driver
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("driver successfully.");
        logFile("driver successfully.");

        //2.Connection
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/world", "admin", "admin");
        System.out.println("connecte successfully.");
        logFile("connecte successfully.");

        //3.Statement
        PreparedStatement statement = connection.prepareStatement(sql);
        System.out.println("statement successfully.");
        logFile("statement successfully.");

        //
        if (node.endsWith("Code")) {
            statement.setString(1, "%" + code + "%");
        } else if (node.endsWith("Name")) {
            statement.setString(1, name + "%");
        } else if (node.endsWith("Continent")) {
            statement.setString(1, "%" + continent + "%");
        } else if (node.endsWith("Population")) {
            statement = connection.prepareStatement("Select Code,Name,Continent,Population From Country where Population "
                    + population + " order by Name");
        }
        System.out.println(statement);
        logFile("" + statement);

        //4.Result
        ResultSet result = statement.executeQuery();
        System.out.println("search successfully.");
        logFile("search successfully.");

        //5.deal 
        while (result.next()) {
            countryList.add(new Country(
                    result.getString("Code"),
                    result.getString("Name"),
                    result.getString("Continent"),
                    result.getInt("Population")));
        }
        System.out.println("arraylist successfully.");
        logFile("arraylist successfully.");


        //6.Close
        result.close();
        statement.close();
        connection.close();
        System.out.println("close successfully.");
        logFile("close successfully.");
    }

    public void logFile(String string) throws Exception {

        BufferedWriter outputFile = new BufferedWriter(new FileWriter("log.txt", true));
        outputFile.write(new Date() + ":" + string + "\r\n");
        outputFile.close();

    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContinent() {
        return continent;
    }

    public void setContinent(String continent) {
        this.continent = continent;
    }

    public String getPopulation() {
        return population;
    }

    public void setPopulation(String population) {
        this.population = population;
    }

    public ArrayList<Country> getCountryList() {
        return countryList;
    }

    public void setCountryList(ArrayList<Country> countryList) {
        this.countryList = countryList;
    }
}

主界面的类

  1 /*
  2  * To change this template, choose Tools | Templates
  3  * and open the template in the editor.
  4  */
  5 package demo;
  6 
  7 import javax.swing.JOptionPane;
  8 import javax.swing.table.DefaultTableModel;
  9 
 10 /**
 11  *
 12  * @author Zhang Tingkuo
 13  */
 14 public class WorldManager extends javax.swing.JFrame {
 15 
 16     private String code;
 17     private String name;
 18     private String continent;
 19     private String population;
 20     private String node;
 21     private int index;
 22 
 23     /**
 24      * Creates new form WorldManager
 25      */
 26     public WorldManager() {
 27         initComponents();
 28         this.setLocationRelativeTo(null);
 29         this.setResizable(false);
 30     }
 31 
 32     /**
 33      * This method is called from within the constructor to initialize the form.
 34      * WARNING: Do NOT modify this code. The content of this method is always
 35      * regenerated by the Form Editor.
 36      */
 37     @SuppressWarnings("unchecked")
 38     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
 39     private void initComponents() {
 40 
 41         buttonGroup1 = new javax.swing.ButtonGroup();
 42         jPanel1 = new javax.swing.JPanel();
 43         jLabel1 = new javax.swing.JLabel();
 44         jLabel2 = new javax.swing.JLabel();
 45         jLabel3 = new javax.swing.JLabel();
 46         jLabel4 = new javax.swing.JLabel();
 47         txtCode = new javax.swing.JTextField();
 48         txtName = new javax.swing.JTextField();
 49         txtContinent = new javax.swing.JTextField();
 50         txtPopulation = new javax.swing.JTextField();
 51         btnSearch = new javax.swing.JButton();
 52         btnInsert = new javax.swing.JButton();
 53         btnUpdate = new javax.swing.JButton();
 54         btnDelete = new javax.swing.JButton();
 55         jScrollPane2 = new javax.swing.JScrollPane();
 56         tableDisplay = new javax.swing.JTable();
 57         jPanel2 = new javax.swing.JPanel();
 58         rbtInsert = new javax.swing.JRadioButton();
 59         rbtSearch = new javax.swing.JRadioButton();
 60         rbtDelete = new javax.swing.JRadioButton();
 61         rbtUpdate = new javax.swing.JRadioButton();
 62         cmbNode = new javax.swing.JComboBox();
 63 
 64         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
 65         setTitle("Countyr Manager");
 66 
 67         jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
 68 
 69         jLabel1.setText("Code :");
 70 
 71         jLabel2.setText("Name :");
 72 
 73         jLabel3.setText("Continent :");
 74 
 75         jLabel4.setText("Population :");
 76 
 77         txtCode.setText("CHN");
 78 
 79         txtName.setText("China");
 80 
 81         txtContinent.setText("Asia");
 82 
 83         txtPopulation.setText("> 10000");
 84 
 85         btnSearch.setText("Search");
 86         btnSearch.setEnabled(false);
 87         btnSearch.addActionListener(new java.awt.event.ActionListener() {
 88             public void actionPerformed(java.awt.event.ActionEvent evt) {
 89                 btnSearchActionPerformed(evt);
 90             }
 91         });
 92 
 93         btnInsert.setText("Insert");
 94         btnInsert.setEnabled(false);
 95         btnInsert.addActionListener(new java.awt.event.ActionListener() {
 96             public void actionPerformed(java.awt.event.ActionEvent evt) {
 97                 btnInsertActionPerformed(evt);
 98             }
 99         });
100 
101         btnUpdate.setText("Update");
102         btnUpdate.setEnabled(false);
103         btnUpdate.addActionListener(new java.awt.event.ActionListener() {
104             public void actionPerformed(java.awt.event.ActionEvent evt) {
105                 btnUpdateActionPerformed(evt);
106             }
107         });
108 
109         btnDelete.setText("Delete");
110         btnDelete.setEnabled(false);
111         btnDelete.addActionListener(new java.awt.event.ActionListener() {
112             public void actionPerformed(java.awt.event.ActionEvent evt) {
113                 btnDeleteActionPerformed(evt);
114             }
115         });
116 
117         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
118         jPanel1.setLayout(jPanel1Layout);
119         jPanel1Layout.setHorizontalGroup(
120             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
121             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
122                 .addContainerGap()
123                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
124                     .addComponent(btnSearch, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
125                     .addComponent(btnInsert, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
126                     .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
127                         .addComponent(jLabel4)
128                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
129                         .addComponent(txtPopulation, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE))
130                     .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
131                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
132                             .addComponent(jLabel3)
133                             .addComponent(jLabel2)
134                             .addComponent(jLabel1))
135                         .addGap(18, 18, 18)
136                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
137                             .addComponent(txtCode)
138                             .addComponent(txtName)
139                             .addComponent(txtContinent)))
140                     .addComponent(btnDelete, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
141                     .addComponent(btnUpdate, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
142                 .addContainerGap())
143         );
144         jPanel1Layout.setVerticalGroup(
145             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
146             .addGroup(jPanel1Layout.createSequentialGroup()
147                 .addContainerGap()
148                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
149                     .addComponent(jLabel1)
150                     .addComponent(txtCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
151                 .addGap(18, 18, 18)
152                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
153                     .addComponent(jLabel2)
154                     .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
155                 .addGap(18, 18, 18)
156                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
157                     .addComponent(jLabel3)
158                     .addComponent(txtContinent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
159                 .addGap(18, 18, 18)
160                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
161                     .addComponent(jLabel4)
162                     .addComponent(txtPopulation, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
163                 .addGap(18, 18, 18)
164                 .addComponent(btnInsert)
165                 .addGap(18, 18, 18)
166                 .addComponent(btnDelete)
167                 .addGap(18, 18, 18)
168                 .addComponent(btnUpdate)
169                 .addGap(18, 18, 18)
170                 .addComponent(btnSearch)
171                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
172         );
173 
174         tableDisplay.setModel(new javax.swing.table.DefaultTableModel(
175             new Object [][] {
176 
177             },
178             new String [] {
179                 "Code", "Name", "Continent", "Population"
180             }
181         ));
182         tableDisplay.setGridColor(new java.awt.Color(0, 0, 204));
183         tableDisplay.setSelectionBackground(new java.awt.Color(51, 255, 204));
184         tableDisplay.setSelectionForeground(new java.awt.Color(255, 0, 0));
185         tableDisplay.addMouseListener(new java.awt.event.MouseAdapter() {
186             public void mouseClicked(java.awt.event.MouseEvent evt) {
187                 tableDisplayMouseClicked(evt);
188             }
189         });
190         jScrollPane2.setViewportView(tableDisplay);
191 
192         jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
193 
194         buttonGroup1.add(rbtInsert);
195         rbtInsert.setText("Insert");
196         rbtInsert.addActionListener(new java.awt.event.ActionListener() {
197             public void actionPerformed(java.awt.event.ActionEvent evt) {
198                 rbtInsertActionPerformed(evt);
199             }
200         });
201 
202         buttonGroup1.add(rbtSearch);
203         rbtSearch.setText("Search");
204         rbtSearch.addActionListener(new java.awt.event.ActionListener() {
205             public void actionPerformed(java.awt.event.ActionEvent evt) {
206                 rbtSearchActionPerformed(evt);
207             }
208         });
209 
210         buttonGroup1.add(rbtDelete);
211         rbtDelete.setText("Delete");
212         rbtDelete.addActionListener(new java.awt.event.ActionListener() {
213             public void actionPerformed(java.awt.event.ActionEvent evt) {
214                 rbtDeleteActionPerformed(evt);
215             }
216         });
217 
218         buttonGroup1.add(rbtUpdate);
219         rbtUpdate.setText("Update");
220         rbtUpdate.addActionListener(new java.awt.event.ActionListener() {
221             public void actionPerformed(java.awt.event.ActionEvent evt) {
222                 rbtUpdateActionPerformed(evt);
223             }
224         });
225 
226         cmbNode.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Code", "Name", "Continent", "Population" }));
227 
228         javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
229         jPanel2.setLayout(jPanel2Layout);
230         jPanel2Layout.setHorizontalGroup(
231             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
232             .addGroup(jPanel2Layout.createSequentialGroup()
233                 .addContainerGap()
234                 .addComponent(rbtInsert)
235                 .addGap(18, 18, 18)
236                 .addComponent(rbtDelete)
237                 .addGap(18, 18, 18)
238                 .addComponent(rbtUpdate)
239                 .addGap(18, 18, 18)
240                 .addComponent(rbtSearch)
241                 .addGap(18, 18, 18)
242                 .addComponent(cmbNode, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
243                 .addContainerGap())
244         );
245         jPanel2Layout.setVerticalGroup(
246             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
247             .addGroup(jPanel2Layout.createSequentialGroup()
248                 .addContainerGap(10, Short.MAX_VALUE)
249                 .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
250                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
251                         .addComponent(rbtInsert)
252                         .addComponent(rbtDelete)
253                         .addComponent(rbtUpdate)
254                         .addComponent(rbtSearch))
255                     .addComponent(cmbNode, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
256                 .addContainerGap())
257         );
258 
259         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
260         getContentPane().setLayout(layout);
261         layout.setHorizontalGroup(
262             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
263             .addGroup(layout.createSequentialGroup()
264                 .addContainerGap()
265                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
266                     .addComponent(jScrollPane2)
267                     .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
268                 .addGap(18, 18, 18)
269                 .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
270                 .addContainerGap())
271         );
272         layout.setVerticalGroup(
273             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
274             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
275                 .addContainerGap()
276                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
277                     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
278                     .addGroup(layout.createSequentialGroup()
279                         .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
280                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
281                         .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
282                 .addContainerGap())
283         );
284 
285         pack();
286     }// </editor-fold>                        
287 
288     private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {                                          
289         // Search
290         DefaultTableModel tableModel = (DefaultTableModel) tableDisplay.getModel();
291         code = txtCode.getText();
292         name = txtName.getText();
293         continent = txtContinent.getText();
294         population = txtPopulation.getText();
295         node = cmbNode.getSelectedItem().toString();
296 
297         //ClearTable
298         if (tableDisplay.getRowCount() != 0) {
299             ((DefaultTableModel) tableDisplay.getModel()).getDataVector().removeAllElements();
300         }
301 
302         try {
303             Manager manager = new Manager(code, name, continent, population, node);
304             manager.Seach(code, name, continent, population, node);
305             for (int i = 0; i < manager.getCountryList().size(); i++) {
306                 tableModel.addRow(new Object[]{
307                             manager.getCountryList().get(i).getCode(),
308                             manager.getCountryList().get(i).getName(),
309                             manager.getCountryList().get(i).getContinent(),
310                             manager.getCountryList().get(i).getPopulation()});
311             }
312         } catch (Exception e) {
313             JOptionPane.showMessageDialog(this, e, "Error", JOptionPane.ERROR_MESSAGE);
314         }
315     }                                         
316 
317     private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {                                          
318         // TODO add your handling code here:
319         code = txtCode.getText();
320         name = txtName.getText();
321         continent = txtContinent.getText();
322         population = txtPopulation.getText();
323 
324         try {
325             Manager manager = new Manager(code, name, continent, population, node);
326             manager.Insert(code, name, continent, population);
327         } catch (Exception e) {
328             JOptionPane.showMessageDialog(this, e, "Error", JOptionPane.ERROR_MESSAGE);
329         }
330     }                                         
331 
332     private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
333         // Update data
334         code = txtCode.getText();
335         name = txtName.getText();
336         continent = txtContinent.getText();
337         population = txtPopulation.getText();
338         try {
339             Manager manager = new Manager(code, name, continent, population, node);
340             manager.Update(code, name, continent, population);
341         } catch (Exception e) {
342             JOptionPane.showMessageDialog(this, e, "Error", JOptionPane.ERROR_MESSAGE);
343         }
344     }                                         
345 
346     private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
347         // Delete Data
348         code = txtCode.getText();
349         name = txtName.getText();
350         continent = txtContinent.getText();
351         population = txtPopulation.getText();
352         try {
353             Manager manager = new Manager(code, name, continent, population, node);
354             manager.Delete(code, name, continent, population);
355         } catch (Exception e) {
356             JOptionPane.showMessageDialog(this, e, "Error", JOptionPane.ERROR_MESSAGE);
357         }
358     }                                         
359 
360     private void rbtInsertActionPerformed(java.awt.event.ActionEvent evt) {                                          
361         // TODO add your handling code here:
362         txtPopulation.setText("000");
363         btnInsert.setEnabled(true);
364         btnDelete.setEnabled(false);
365         btnUpdate.setEnabled(false);
366         btnSearch.setEnabled(false);
367     }                                         
368 
369     private void rbtDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
370         // TODO add your handling code here:
371         btnInsert.setEnabled(false);
372         btnDelete.setEnabled(true);
373         btnUpdate.setEnabled(false);
374         btnSearch.setEnabled(false);
375     }                                         
376 
377     private void rbtUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
378         // TODO add your handling code here:
379         btnInsert.setEnabled(false);
380         btnDelete.setEnabled(false);
381         btnUpdate.setEnabled(true);
382         btnSearch.setEnabled(false);
383     }                                         
384 
385     private void rbtSearchActionPerformed(java.awt.event.ActionEvent evt) {                                          
386         // TODO add your handling code here:
387         btnInsert.setEnabled(false);
388         btnDelete.setEnabled(false);
389         btnUpdate.setEnabled(false);
390         btnSearch.setEnabled(true);
391     }                                         
392 
393     private void tableDisplayMouseClicked(java.awt.event.MouseEvent evt) {                                          
394         // TODO add your handling code here:
395         index = tableDisplay.getSelectedRow();
396 
397         txtCode.setText(tableDisplay.getValueAt(index, 0).toString());
398         txtName.setText(tableDisplay.getValueAt(index, 1).toString());
399         txtContinent.setText(tableDisplay.getValueAt(index, 2).toString());
400         txtPopulation.setText(tableDisplay.getValueAt(index, 3).toString());
401     }                                         
402 
403     /**
404      * @param args the command line arguments
405      */
406     public static void main(String args[]) {
407         /* Set the Nimbus look and feel */
408         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
409         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
410          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
411          */
412         try {
413             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
414                 if ("Nimbus".equals(info.getName())) {
415                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
416                     break;
417                 }
418             }
419         } catch (ClassNotFoundException ex) {
420             java.util.logging.Logger.getLogger(WorldManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
421         } catch (InstantiationException ex) {
422             java.util.logging.Logger.getLogger(WorldManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
423         } catch (IllegalAccessException ex) {
424             java.util.logging.Logger.getLogger(WorldManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
425         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
426             java.util.logging.Logger.getLogger(WorldManager.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
427         }
428         //</editor-fold>
429 
430         /* Create and display the form */
431         java.awt.EventQueue.invokeLater(new Runnable() {
432             @Override
433             public void run() {
434                 new WorldManager().setVisible(true);
435             }
436         });
437     }
438     // Variables declaration - do not modify                     
439     private javax.swing.JButton btnDelete;
440     private javax.swing.JButton btnInsert;
441     private javax.swing.JButton btnSearch;
442     private javax.swing.JButton btnUpdate;
443     private javax.swing.ButtonGroup buttonGroup1;
444     private javax.swing.JComboBox cmbNode;
445     private javax.swing.JLabel jLabel1;
446     private javax.swing.JLabel jLabel2;
447     private javax.swing.JLabel jLabel3;
448     private javax.swing.JLabel jLabel4;
449     private javax.swing.JPanel jPanel1;
450     private javax.swing.JPanel jPanel2;
451     private javax.swing.JScrollPane jScrollPane2;
452     private javax.swing.JRadioButton rbtDelete;
453     private javax.swing.JRadioButton rbtInsert;
454     private javax.swing.JRadioButton rbtSearch;
455     private javax.swing.JRadioButton rbtUpdate;
456     private javax.swing.JTable tableDisplay;
457     private javax.swing.JTextField txtCode;
458     private javax.swing.JTextField txtContinent;
459     private javax.swing.JTextField txtName;
460     private javax.swing.JTextField txtPopulation;
461     // End of variables declaration                   
462 }

原文地址:https://www.cnblogs.com/zhangtingkuo/p/2809341.html