The Artima Developer Community
Sponsored Link

Java Answers Forum
Please somebody help me with this

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
mak

Posts: 1
Nickname: trem
Registered: Mar, 2003

Please somebody help me with this Posted: Mar 9, 2003 6:27 PM
Reply to this message Reply
Advertisement
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.








import java.io.*;
import java.sql.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import oracle.jdbc.driver.*;

/**
*
* class JdbcCW
*
* demonstration of Java client connecting to an Oracle DBMS
*
* @author put your name here
* @date December 2002
*/


public class JdbcCW extends JFrame{

static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
static String initialquery = "select table_name from user_tables";
static String nullstring = "null";

private String user;
private String pass;
private Connection conn;
private Statement stmt;
private ResultSet results;


private JComboBox tableChooser;

private JButton editButton;
private JButton exitButton;
private JButton detailsButton;

private JPanel panel1;
private JPanel panel2;
private String selectedTable;

private Container cp;

public static void main(String[] args) throws SQLException {
JdbcCW demo = new JdbcCW();
demo.show();
}

/**
* JdbcCW constructor method
*/

public JdbcCW() throws SQLException {

super("Java/Oracle coursework");

addWindowListener (new WindowAdapter() {
public void windowClosing (WindowEvent evt) {
System.exit(0);
}
}
);


try {

user =
JOptionPane.showInputDialog("enter oracle username eg ops$c9999999");

pass = JOptionPane.showInputDialog("enter oracle password eg 29feb80");

if ( user.length() == 0 || pass.length() == 0)
throw new Exception("no user name and/or password");

DriverManager.registerDriver (new OracleDriver());
conn = DriverManager.getConnection(url, user, pass);
if (conn != null)
System.out.println("Connected");
}
catch (Exception e ) {
e.printStackTrace();
System.exit(0); // problems - abandon ship
}

// now we can access tables in the database

stmt = conn.createStatement();
results = stmt.executeQuery(initialquery);

boolean anyRecords = results.next();

if ( ! anyRecords ) {
JOptionPane.showMessageDialog (this, "No Tables in the database");
System.exit(0);
}

tableChooser = new JComboBox();
do
tableChooser.addItem(results.getString(1));
while (results.next() ) ;

selectedTable = (String) tableChooser.getSelectedItem();

tableChooser.addActionListener (new ActionListener () {
public void actionPerformed (ActionEvent evt) {
changeTable();
}
}
);
editButton = new JButton("Edit");
editButton.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent evt) {
runEdit();
}
}
);
exitButton = new JButton("Exit");
exitButton.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
}
);

panel1 = new JPanel(); // panels have flow layout
JLabel label1 = new JLabel("Choose Table");
panel1.add(label1);
panel1.add(tableChooser);

panel2 = new JPanel();

panel2.add(editButton);
panel2.add(exitButton);


cp = getContentPane();
cp.add(panel1,BorderLayout.NORTH);
cp.add(panel2,BorderLayout.SOUTH);

setSize(300,200);
setLocation(100,100);

}

private void changeTable() {

selectedTable = (String) tableChooser.getSelectedItem();
}


/**
* method runEdit runs a query to determine the structure of the
* selected table in order to customise the table editor window
*
*/

private void runEdit() {
System.out.println("Selected Table " + selectedTable);
String query = "select column_name,data_type from user_tab_columns " +
"where table_name = '" + selectedTable + "'";

try {
results = stmt.executeQuery(query);
} catch (java.sql.SQLException e) {
System.out.println("SQL Exception on query " + query);
return;
}

JdbcEdit tableEditor = new JdbcEdit(this,selectedTable,results);
tableEditor.show();
}

public ResultSet makeQuery(String query) {
ResultSet results;
try {
results = stmt.executeQuery(query);
} catch (SQLException e) {
System.out.println("Query Failed " + query);
return null;
}
return results;
}

} // end class JdbcGen


/**
* class JdbcEdit
*
* oracle table editing dialog window
*
* @author put your name here as well
* @date December 2002
*/


class JdbcEdit extends JDialog {

private JdbcCW parent;
private Container cp;

private Vector columnNames;
private Vector dataTypes;
private Vector editFields;
private Vector rows;

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) {

super(parent,"table editor " + tableName, true);
this.parent = parent;

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

columnNames.add(columnName);
dataTypes.add(dataType);
editFields.add(dataField);

}while (results.next() ) ;
} catch (java.sql.SQLException e) {
System.out.println("SQL Exception on query ");
return;
}

// get the data from the Oracle table and put the first
// row of data in the text fields in the dialog window

populateData(tableName);

// find out which column(s) are part of the primary key and make these
// textfields non-editable so the values can't be changed

findPKConstraints(tableName);

// this would be a good place to discover any foreign key constraints


JPanel buttonsPanel = new JPanel();
JButton exitButton = new JButton("Exit");
exitButton.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent evt) {
closeWindow();
}
}
);
JButton nextButton = new JButton("Next");
nextButton.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent evt) {
showRow(true);
}
}
);
JButton prevButton = new JButton("Prev");
prevButton.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent evt) {
showRow(false);
}
}
);

buttonsPanel.add(exitButton);
buttonsPanel.add(nextButton);
buttonsPanel.add(prevButton);


cp = getContentPane();
cp.add(mainPanel,BorderLayout.CENTER);
cp.add(buttonsPanel,BorderLayout.SOUTH);

pack();
}

private void closeWindow() {
dispose();
}



private void populateData(String tableName) {
int noOfColumns;

// have to access the Statement object in the parent frame

ResultSet tableData = parent.makeQuery("select * from " + tableName);

try {
boolean anyRecords = tableData.next();
if ( ! anyRecords ) {
JOptionPane.showMessageDialog (this, "No data in " +
tableName);
return;
}

rows = new Vector();

noOfColumns = columnNames.size();
do {
String [] row = new String[noOfColumns];
for (int i = 0; i < noOfColumns; i++) {
row = tableData.getString(i+1);
}
rows.add(row);

} while (tableData.next() ) ;
} catch (java.sql.SQLException e) {
System.out.println("SQL Exception on query ");
return;
}

// Put the first row of data from the table into the test fields;

String [] rowData = (String[]) rows.get(0);

for (int i = 0; i < noOfColumns; i++) {

// get the reference to a text field in the dialog window

JTextField textField = (JTextField) editFields.get(i);

// 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;
}

if (next && currentRow < rows.size() - 1) {
currentRow++;
} else if (!next && currentRow > 0) {
currentRow--;
} else {
System.out.println("No Next/Prev row " + currentRow);
getToolkit().beep();
return;
}

String [] rowData = (String[]) rows.get(currentRow);
int noOfAttributes = dataTypes.size();

for (int i = 0; i < noOfAttributes; i++) {
JTextField textField = (JTextField) editFields.get(i);
textField.setText(rowData);
}
}

private void findPKConstraints(String tableName){

String PK_ConstraintName = "none";
String pkquery =
"select owner,constraint_name from user_constraints " +
"where table_name = '" + tableName + "' and constraint_type = 'P'";

// have to access the Statement object in the parent frame

ResultSet results = parent.makeQuery(pkquery);

try {
boolean anyRecords = results.next();
if ( ! anyRecords ) {
// if none just return - but print a debug message
System.out.println("No primary key constraint found");
return;
} else {

// There should only be one

System.out.println("Owner = " + results.getString(1) +
" name = " + results.getString(2));
PK_ConstraintName = results.getString(2);

// Now find out which columns

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());
}

}
} catch (java.sql.SQLException e) {
System.out.println("SQL Exception on query " + pkquery);
return;
}
}

}

Topic: Problem deploying WAR file Previous Topic   Next Topic Topic: RTF Editor / Writer and LoadRtf object.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use