Sri2110
Sri2110

Reputation: 335

Issue in updating data into oracle database using JTextfields in java

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.

Upvotes: 0

Views: 64

Answers (1)

camickr
camickr

Reputation: 324118

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.

Upvotes: 2

Related Questions