0

On Update button press, following code snippet needs to be executed: Note that: t1, t2,. . .,t8 are JTextfields. Also note that CUST_PHONE and ADV receive datatype number, whereas rest all are varchar.
CUST_NAME is the Primary Key assumed;

theQuery("update gkkdb set CUST_ID='"+t1.getText()+"', CUST_PHONE="+t3.getText()+",CUST_CAT='"+t4.getText()+"',ST_DATE='"+t5.getText()+"',ADV="+t6.getText()+",END_DATE='"+t7.getText()+"',ADDR='"+t8.getText()+"' where CUST_NAME="+t2.getText());

theQuery(String s) function is as follows:-

public void theQuery(String query)
{
Connection con = null;
Statement st= null;
try{
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    con = DriverManager.getConnection("jdbc:oracle:thin:@Localhost:1521:xe","system","qwerty");
    System.out.println("Database connected");
    st= con.createStatement();
    st.executeUpdate(query);
    JOptionPane.showMessageDialog(null,"Customer Updated!");
    } catch(Exception e)
    {
    JOptionPane.showMessageDialog(null,e.getMessage());
    }
    }

It is displaying error as: ORA-00904: "xxxx" :invalid identifier, where xxxx is any CUST_NAME which i am using to update data.

1 Answer 1

2

Did you display the value of all your variables to make sure they contain data?

Is customer id a String or an Integer?

In any case use a PreparedStatement for the SQL to prevent errors.

A simple example to get your started:

String sql = "UPDATE Page SET Title = ? WHERE Name = ?";

PreparedStatement stmt = connection.prepareStatement(sql);

stmt.setString( 1, textField1.getText() );
stmt.setString( 2, textField2.getText() );
stmt.executeUpdate();
stmt.close();

I hope you get the idea. Each "?" gets replaced and you don't have to worry about the syntax. Much easier to read and spot mistakes.

Sign up to request clarification or add additional context in comments.

1 Comment

and I figured out while i was using Statement, the date entry was an issue. Some format issues were there. however it is resolved now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.