📄 cardfile.java
字号:
/* * Copyright 1996 John Wiley & Sons, Inc. All Rights Reserved. Reproduction * or translation of this work beyond that permitted in Section 117 of the 1976 * United States Copyright Act without the express written permission of the * copyright owner is unlawful. Requests for further information should be * addressed to Permissions Department, John Wiley & Sons, Inc. The * purchaser may make back-up copies for his/her own use only and not for * distribution or resale. The Publisher assumes no responsibility for errors, * omissions, or damages, caused by the use of this software or from the use * of the information contained herein. * */import java.awt.*;import java.lang.*;import java.util.*;import msql.*;public class CardFile extends Frame { // instantiate a new Msql object Msql msql = new Msql(); // the TextField objects for each column TextField txt_name, txt_address, txt_city, txt_state, txt_zip, txt_country, txt_phone, txt_id; String formstate; // indicates whether we are adding or editing // String name, address, city, state, zip, country, phone; // a layout manager for the Frame GridBagLayout layoutmangler = new GridBagLayout(); // each of the buttons and a Panel to hold them Button next, previous, quit, save, newrow, edit, cancel, delete; Panel actionbuttons; // the Vector holds the primary key (customer id) for // each row in the table. currentRow holds the index // of the one we are currently viewing int currentRow = 0; Vector cardfileKeys = new Vector(); // this hash is used to map column names to the // corresponding objects (the TextFields) Hashtable columnmap = new Hashtable(); // the default constructor public CardFile() { // set the layout for the frame this.setLayout(layoutmangler); // this is the offset within the GridBagLayout. If // I want the next object on a different line, I // postincrement. If not, I don't. int i = 0; // the name field // // first, I add an anonymous text label to the Frame using // the AddToFrame method. Note that it uses column 0, and // row i (currently zero). I don't want the textfield // to appear on a different row, so I don't postincrement i. AddToFrame(new Label("Name:"), 0, i); // now, I instantiate txt_name. This is a textfield with 20 // spaces. Then, I add it to the frame, and insert a reference // to it in the columnmap, keyed by 'name', which is the name // of the column in the CardFile table txt_name = new TextField(20); txt_name.setEditable(false); AddToFrame(txt_name, 1, i++); columnmap.put("name", txt_name); // the rest of the fields. Everything is pretty much the same as above. // // the address field AddToFrame(new Label("Address:"), 0, i); txt_address = new TextField(35); txt_address.setEditable(false); AddToFrame(txt_address, 1, i++); columnmap.put("address", txt_address); // the city field // don't postincrement i, so state will show up // on the same row. AddToFrame(new Label("City:"), 0, i); txt_city = new TextField(20); txt_city.setEditable(false); AddToFrame(txt_city, 1, i); columnmap.put("city", txt_city); // the state field AddToFrame(new Label("State:"), 2, i); txt_state = new TextField(2); txt_state.setEditable(false); AddToFrame(txt_state, 3, i++); columnmap.put("state", txt_state); // the zip field AddToFrame(new Label("Zip:"), 0, i); txt_zip = new TextField(11); txt_zip.setEditable(false); AddToFrame(txt_zip, 1, i++); columnmap.put("zip", txt_zip); // the country field AddToFrame(new Label("Country:"), 0, i); txt_country = new TextField(25); txt_country.setEditable(false); AddToFrame(txt_country, 1, i++); columnmap.put("country", txt_country); // the phone field AddToFrame(new Label("Phone:"), 0, i); txt_phone = new TextField(20); txt_phone.setEditable(false); AddToFrame(txt_phone, 1, i++); columnmap.put("phone", txt_phone); // the id field. AddToFrame(new Label("Id:"), 0, i); txt_id = new TextField(4); txt_id.setEditable(false); AddToFrame(txt_id, 1, i++); columnmap.put("id", txt_id); // create the button panel and give it a new FlowLayout actionbuttons = new Panel(); actionbuttons.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); // The constraints for the button panel are a little // more complex than the constraints for the labels // and textfields, so we'll set it up here, and add // the actionbuttons panel GridBagConstraints c = new GridBagConstraints(); c.gridwidth = 3; c.gridheight = 1; c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.CENTER; c.weightx = 0.0; c.weighty = 0.0; c.gridx = 0; c.gridy = i; ((GridBagLayout)this.getLayout()).setConstraints(actionbuttons, c); this.add(actionbuttons); // instantiate and add each of the buttons previous = new Button("Previous"); actionbuttons.add(previous); next = new Button("Next"); actionbuttons.add(next); quit = new Button("Quit"); actionbuttons.add(quit); newrow = new Button("New"); actionbuttons.add(newrow); edit = new Button("Edit"); actionbuttons.add(edit); delete = new Button("Delete"); actionbuttons.add(delete); // save and cancel are disabled until the user // is adding or editing. save = new Button("Save"); actionbuttons.add(save); save.setEnabled(false); cancel = new Button("Cancel"); actionbuttons.add(cancel); cancel.setEnabled(false); // connect to the localhost and use the database // named 'sample' try { msql.Connect("localhost"); msql.SelectDB("sample"); } catch(MsqlException e ) { e.printStackTrace(); } // call getKeys() to populate cardfileKeys with unique identifiers // for all the keys in the table getKeys(); // call getRow() to display the first row in the table. // this should be the element in cardfileKeys at index // currentRow, which starts out at zero. getRow(); // pack the Frame and show it. pack(); show(); System.out.println("CardFile was started successfully."); } /** * getKeys() * * This populates the cardfileKeys Vector with unique identifiers * for all of the rows in the cardfile table. This lets us buffer * all of the rows, without storing the values for each column. * As a result, we only have to worry about dirty data if someone * changes a key, which of course, you would *never* do... * * now, if someone else deletes or inserts a row, that's a * different problem. Handling that is an excercise left to * the reader... */ protected void getKeys() { try { // delete all the elements in cardfileKeys cardfileKeys.removeAllElements(); // execute a query to get the id column for each of the // rows. Then, process each row and add the id column // to cardfileKeys MsqlResult result = msql.Query("select id from cardfile"); String row[]; while(( row = result.FetchRow()) != null){ cardfileKeys.addElement(row[0]); } } catch (MsqlException e) { e.printStackTrace(); } } /** * clearForm() * * Clear all of the input fields * */ protected void clearForm () { // blank the name field txt_name.setText(""); // blank the address field txt_address.setText(""); // blank the city field txt_city.setText(""); // blank the state field txt_state.setText(""); // blank the zip field txt_zip.setText(""); // blank the country field txt_country.setText(""); // blank the phone field txt_phone.setText(""); // blank the id field txt_id.setText(""); } /** * AddToFrame() * * A convenience method to wrap the living hell * that is GridBagConstraints() * */ protected void AddToFrame (Component item, int x, int y) { // some sane layout defaults. GridBagConstraints c = new GridBagConstraints(); c.gridwidth = 1; c.gridheight = 1; c.fill = GridBagConstraints.NONE; c.anchor = GridBagConstraints.NORTHWEST; c.weightx = 0.0; c.weighty = 0.0; // set the grid coordinates c.gridx = x; c.gridy = y; // set the constraints, and add the item to the layout ((GridBagLayout)this.getLayout()).setConstraints(item, c); this.add(item); } /** * * save() * * Save the record we are editing to the table * */ protected void save() { // construct an insert statement, with values for each // column, including id. Teaching this system to auto- // increment id in a multi-user environment is an excercise // left up to the reader. // // Assuming, of course, that bambi hasn't released a version // of mSQL which supports this natively :-) String sql = "insert into cardfile " + " (name, address, city, state, zip, country, phone, id)" + " values ( " + "'" + txt_name.getText() + "', " + "'" + txt_address.getText() + "', " + "'" + txt_city.getText() + "', " + "'" + txt_state.getText() + "', " + "'" + txt_zip.getText() + "', " + "'" + txt_country.getText() + "', " + "'" + txt_phone.getText() + "', " + txt_id.getText() + ")"; try { // if the query doesn't throw an exception, we can add the newly // created id to the cardfileKeys Vector, and set the currentRow // to the element which points to the new record MsqlResult result = msql.Query(sql); cardfileKeys.addElement(txt_id.getText()); currentRow = cardfileKeys.indexOf(txt_id.getText()); // call getRow() to refresh the form. This really shouldn't // be necessary, but it lets us know that the record was // saved correctly. Or not, as the case may be :-) getRow(); } catch(MsqlException e ) { e.printStackTrace(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -