简体   繁体   English

使用Java中的JTextfields将数据更新到oracle数据库中的问题

[英]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,. 在按下“更新”按钮时,需要执行以下代码段:注意:t1,t2 、。 . .,t8 are JTextfields. 。,t8是JTextfields。 Also note that CUST_PHONE and ADV receive datatype number, whereas rest all are varchar. 还要注意,CUST_PHONE和ADV接收数据类型号,而其余的都是varchar。
CUST_NAME is the Primary Key assumed; CUST_NAME是假定的主键;

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:- theQuery(String s)函数如下:-

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. 它显示错误为:ORA-00904:“ xxxx”:无效的标识符,其中xxxx是我用来更新数据的任何CUST_NAME。

Did you display the value of all your variables to make sure they contain data? 您是否显示了所有变量的值以确保它们包含数据?

Is customer id a String or an Integer? 客户ID是字符串还是整数?

In any case use a PreparedStatement for the SQL to prevent errors. 在任何情况下,请对SQL使用PreparedStatement以避免错误。

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. 更容易阅读和发现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM