im trying to do this for 3 weeks but nothing i domt know what to do please help me!!!thanks
1.1 modify the application so that: i) Information about any foreign key constraints for the selected table is displayed eg as additional labels in the table editor window, or in a separate dialog window. You can choose how you wish to display this information. ii) An Update button is added for each text field in the display, which will attempt to update the corresponding field in the database table, from the current data in the textfield. The following code will retrieve the current value from a JTextField object: String newValue = textfieldName.getText(); The following code will convert the string to a number: int newNumber = Integer.parseInt(newValue);
Remember, an attempt to update a row in a table may fail if eg it tries to alter a foreign key to a value not currently recorded for any corresponding primary key that the foreign key refers to. An attempt to set a foreign key field to 'null' should always be successful. If the update attempt fails, the value in the displayed text field should revert to its original state, to maintain consistency with the database.
private int noOfColumns; private int currentRow = 0;
/** * JdbcEdit constructor method * the parameter true makes the dialog window modal - * the parent frame is inactive as long as this window is displayed */
public JdbcEdit (JdbcCW parent, String tableName, ResultSet results) {
columnNames = new Vector(); dataTypes = new Vector(); editFields = new Vector();
JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
// boxLayout - components are added to mainPanel in a vertical stack
try { boolean anyRecords = results.next(); if ( ! anyRecords ) { JOptionPane.showMessageDialog (this, "No Detail for " + tableName+ " in the database"); return; } do { String columnName = results.getString(1); String dataType = results.getString(2);
System.out.println("Row " + columnName + " Type " + dataType);
JPanel editPanel = new JPanel(); JLabel colNameLabel = new JLabel(columnName); JTextField dataField = new JTextField(20); editPanel.add(colNameLabel); editPanel.add(dataField);
// this would be a good place to add an Update button
mainPanel.add(editPanel);
// now store the columnName, dataType and data text field // in vectors so other methods can access them // at this point in time the text field is empty
// vector editFields holds a reference to each JTextField // in the visible dialog window // hence the next line of code puts the data retrieved from // the table into the relevant text field in the GUI interface
textField.setText(rowData); }
}
// method showRow updates the textfields in the GUI interface // with the next row of data from the table ( if next is true) // or with the previous row of data from the table ( if next is false)
private void showRow(boolean next) { if ( rows == null ) { System.out.println("table is empty"); getToolkit().beep(); return; }
pkquery = "select Column_name, position from user_cons_columns" + " where constraint_name = '" + PK_ConstraintName + "'";
// have to access the Statement object in the parent frame
results = parent.makeQuery(pkquery);
anyRecords = results.next(); if ( ! anyRecords ) { // if none just return - but there must be at least one System.out.println("no columns found"); return; } else { do { System.out.println ("Name " + results.getString(1) + " position " + results.getString(2)) ; int position = Integer.parseInt(results.getString(2)); JTextField primarykey = (JTextField)editFields.get(position - 1); primarykey.setEditable(false); } while (results.next()); }