📄 cardfile.java
字号:
/** * * update() * * Send an update to the mSQL server. * */ protected void update() { // construct an update string for each of the columns // except for the id. This is used as the criteria // for the update, so we probably don't want to // update it... String sql = "update cardfile " + " set name = '" + txt_name.getText() + "', " + " address = '" + txt_address.getText() + "', " + " city = '" + txt_city.getText() + "', " + " state = '" + txt_state.getText() + "', " + " zip = '" + txt_zip.getText() + "', " + " country = '" + txt_country.getText() + "', " + " phone = '" + txt_phone.getText() + "' " + " where id = " + txt_id.getText(); try { // send the query MsqlResult result = msql.Query(sql); // call getRow() to refresh the form. This really shouldn't // be necessary, but it lets us know what the update did. getRow(); } catch(MsqlException e ) { e.printStackTrace(); } } /** * handleEvent * * Deal with things the user did... */ public boolean handleEvent(Event event) { switch(event.id) { // deal with action buttons case Event.ACTION_EVENT: // if the next button was pushed, then I want to // increment currentRow. But, if that would push // it out past cardfileKeys.size(), I will just // wrap around to the beginning (zero). if (event.target == next) { if (currentRow + 1 == cardfileKeys.size()) { currentRow = 0; } else { currentRow++; } // call getRow() to update the form getRow(); } // if the user pushed the previous button, then // I want to decrement currentRow. If currentRow // already zero, then decrementing further would // probably throw some evil exception, so I'll // set it to cardfileKeys.size() - 1, which is the // index of the last element. if (event.target == previous) { if (currentRow == 0) { currentRow = cardfileKeys.size() - 1; } else { currentRow--; } // call getRow() to update the form getRow(); } // if the user pressed delete, then call the // delRow() method. if (event.target == delete) { delRow(); } // if the user pressed quit, then let's get out of here! if (event.target == quit) { System.exit(0); } // if the user wants to edit the current row, // then set the formstate as appropriate, and // call the setEdit() method. if (event.target == edit) { formstate = "edit"; setEdit(); } // in case the user presses the new button, I // want to 1) clear the form, 2) set the formstate // to "new", 3) let them modify the id field, and // 4) call setEdit() if (event.target == newrow) { clearForm(); formstate = "new"; txt_id.setEditable(true); setEdit(); } // in case the user hits the save button, I need // to distinguish between "new" and "edit" mode, // so I know whether to call save() or update(). // Also, I need if (event.target == save) { if (formstate.equals("new")) { save(); txt_id.setEditable(false); // make the id field uneditable } if (formstate.equals("edit")) { update(); } // set the formstate to "browse", and call setBrowse() formstate = "browse"; setBrowse(); } // if the user pressed cancel, return the formstate to browse if (event.target == cancel) { // if it was new, make sure that they can't edit the // id field... if (formstate.equals("new")) { txt_id.setEditable(false); } // return the formstate to browse, call getRow() // to retrieve the row they were looking at // before editing or adding, and call setBrowse() formstate = "browse"; getRow(); setBrowse(); } break; // in case they closed the window, then take // it as a sign that they want to quit case Event.WINDOW_DESTROY: System.exit(0); break; // hmmm... // just pass these on case Event.MOUSE_DOWN: case Event.MOUSE_UP: case Event.MOUSE_DRAG: case Event.KEY_PRESS: case Event.KEY_ACTION: case Event.KEY_RELEASE: case Event.KEY_ACTION_RELEASE: case Event.GOT_FOCUS: case Event.LOST_FOCUS: case Event.MOUSE_ENTER: case Event.MOUSE_EXIT: case Event.MOUSE_MOVE: return false; } return true; } /** * setEdit() * * prepare the form for editing/adding * */ protected void setEdit () { // disable all these buttons next.setEnabled(false); previous.setEnabled(false); newrow.setEnabled(false); edit.setEnabled(false); delete.setEnabled(false); // set everything except the id to be editable txt_name.setEditable(true); txt_address.setEditable(true); txt_city.setEditable(true); txt_state.setEditable(true); txt_zip.setEditable(true); txt_country.setEditable(true); txt_phone.setEditable(true); // enable these two buttons save.setEnabled(true); cancel.setEnabled(true); } /** * setBrowse() * * prepare the form for viewing * */ protected void setBrowse() { // enable all these buttons next.setEnabled(true); previous.setEnabled(true); newrow.setEnabled(true); edit.setEnabled(true); delete.setEnabled(true); // disable the fields txt_name.setEditable(false); txt_address.setEditable(false); txt_city.setEditable(false); txt_state.setEditable(false); txt_zip.setEditable(false); txt_country.setEditable(false); txt_phone.setEditable(false); txt_id.setEditable(false); // disable these buttons save.setEnabled(false); cancel.setEnabled(false); } /** * getRow() * * retrieve a row from the table, using the one indicated by * cardfileKeys.elementAt(currentRow) * */ protected void getRow() { // if there are no rows to process, just clear // the form and return... if (cardfileKeys.isEmpty()) { clearForm(); return; } try { // issue a select statement to get the row which is // pointed to by currentRow. Unless we have an // integrity violation, this should only be one // row. MsqlResult result = msql.Query("select * from cardfile where id = " + cardfileKeys.elementAt(currentRow)); // ahhh... catalog data. Since each textfield is // represented in the columnmap hash, keyed by // the column name to which it corresponds, I // can use the array of column names to map each // column in the result set to the fields on the // form. MsqlFieldDesc field[] = result.ListFields(); // get the number of columns int cols = result.NumFields(); // retrieve the row String row[]; row = result.FetchRow(); // loop over each column, up until the number indicated // by the call the NumFields() for(int i=0; i < cols; i++) { // get the name of the column from field[i].FieldName() String col_name = field[i].FieldName(); // this gets the object from columnmap (a TextField) which // is keyed by the name of the column in col_name // here, we simply call the setText() method of that TextField // object to the value of the column. ((TextField) columnmap.get(col_name)).setText(row[i]); } } catch (MsqlException e) { e.printStackTrace(); } catch (ArrayIndexOutOfBoundsException e) { // ahhh, just ignore it! } } /** * delRow() * * deletes the current row. * */ protected void delRow() { try { // issue the query to delete the row MsqlResult result = msql.Query("delete from cardfile where id = " + cardfileKeys.elementAt(currentRow)); // Oh yeah, don't forget to remove the element from // cardfileKeys. cardfileKeys.removeElement(cardfileKeys.elementAt(currentRow)); // let's just be lazy and return to row 0... currentRow = 0; // call getRow() to refresh the form with the current record. getRow(); } catch (MsqlException e) { e.printStackTrace(); } } // our little friend main, who makes it all happen public static void main(String[] args) { // make a new CardFile, pack() it and show() it. CardFile cardfile = new CardFile(); cardfile.pack(); cardfile.show(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -