java.sql.SQLSyntaxErrorException:ORA-01747

发布于 2024-12-09 01:46:37 字数 2826 浏览 2 评论 0原文

搜索了整个网站,但没有任何帮助,所以我决定开辟一个新主题。这是我的问题:我正在用 java 开发一个简单的 GUI,它在后面使用 JDBC。以下是一些标题: - 我建立了成功的数据库连接, - 我可以运行 SELECT 语句并在我的代码中获取数据。 但问题是,当我使用 INSERT 语句时,它给出了下面的错误,其描述如下:“java.sql.SQLSyntaxErrorException:ORA-01747无效的用户名.表名.列名或表名.列名等...” 我在网上挖掘了两天,但无法找到满足我需求的解决方案。上面是我的代码,显示了我如何实现插入:

String query = "INSERT INTO DQMP.DQ_USER("
                                + " USER_ID,"
                                + " USER_SHORTNAME,"
                                + " USER_NAME,"
                                + " GSM1,"
                                + " E_MAIL,"
                                + " DEPARTMENT_ID,"
                                + ") VALUES(?, ?, ?, ?, ?, ?)";                             

                PreparedStatement st = conn.prepareStatement(query);

                      st.setString(1, "user_id_seq.nextval");
                      st.setString(2, str1);
                      st.setString(3, str2);
                      st.setLong(4, lng);
                      st.setString(5, str4);
                      st.setInt(6, 1);

                      st.executeUpdate();
                      System.out.println("Insertion successful");
                      st.close();           

这是我的 DESC DQ_USER:

TABLE DQ_USER
 Name                                      Null?    Type                        
 ----------------------------------------- -------- ----------------------------
 USER_ID                                   NOT NULL NUMBER                      
 USER_SHORTNAME                                     VARCHAR2(30)                
 USER_NAME                                          VARCHAR2(128)               
 GSM1                                               VARCHAR2(30)                
 E_MAIL                                             VARCHAR2(512)               
 DEPARTMENT_ID                             NOT NULL NUMBER

任何帮助将不胜感激。

这是我的最新更改的代码:

Statement st = conn.createStatement();
                    String query = "SELECT USER_ID_SEQ.NEXTVAL FROM DUAL;";
                    ResultSet rs = st.executeQuery(query);
                    int seq = 0;
                    while(rs.next()){
                         seq = rs.getInt("USER_ID_SEQ");
                         System.out.println(seq);
                    }

                    CallableStatement stmt = conn.prepareCall("{call PKDQ_CONTROL_MNG.ADD_USER (?, ?, ?, ?, ?, ?)}");

                    stmt.setInt(1, seq);
                    stmt.setString(2, str1);
                    stmt.setString(3, str2);
                    stmt.setInt(4, int1);
                    stmt.setString(5, str4);
                    stmt.setString(6, "1");

                    stmt.executeUpdate();
                    stmt.close();

Searched through the entire site but nothing helped at all so I decided to open up a new topic. Here's my problem: I'm developing a simple GUI in java which uses JDBC behind the back. Here are some headlines:
- I established a successful database connection,
- I can run SELECT statements and fetch data within my code.
But the problem is that when I use an INSERT statement, It gives the error below which has a description like "java.sql.SQLSyntaxErrorException: ORA-01747 invalid username.tablename.columnname or tablename.columnname etc..."
I'm digging the web for 2 days but I couldn't come up with a solution to my need. Above is my code showing how I implemented the INSERT:

String query = "INSERT INTO DQMP.DQ_USER("
                                + " USER_ID,"
                                + " USER_SHORTNAME,"
                                + " USER_NAME,"
                                + " GSM1,"
                                + " E_MAIL,"
                                + " DEPARTMENT_ID,"
                                + ") VALUES(?, ?, ?, ?, ?, ?)";                             

                PreparedStatement st = conn.prepareStatement(query);

                      st.setString(1, "user_id_seq.nextval");
                      st.setString(2, str1);
                      st.setString(3, str2);
                      st.setLong(4, lng);
                      st.setString(5, str4);
                      st.setInt(6, 1);

                      st.executeUpdate();
                      System.out.println("Insertion successful");
                      st.close();           

Here's my DESC DQ_USER:

TABLE DQ_USER
 Name                                      Null?    Type                        
 ----------------------------------------- -------- ----------------------------
 USER_ID                                   NOT NULL NUMBER                      
 USER_SHORTNAME                                     VARCHAR2(30)                
 USER_NAME                                          VARCHAR2(128)               
 GSM1                                               VARCHAR2(30)                
 E_MAIL                                             VARCHAR2(512)               
 DEPARTMENT_ID                             NOT NULL NUMBER

Any help would be appreciated.

Here's my code with latest changes:

Statement st = conn.createStatement();
                    String query = "SELECT USER_ID_SEQ.NEXTVAL FROM DUAL;";
                    ResultSet rs = st.executeQuery(query);
                    int seq = 0;
                    while(rs.next()){
                         seq = rs.getInt("USER_ID_SEQ");
                         System.out.println(seq);
                    }

                    CallableStatement stmt = conn.prepareCall("{call PKDQ_CONTROL_MNG.ADD_USER (?, ?, ?, ?, ?, ?)}");

                    stmt.setInt(1, seq);
                    stmt.setString(2, str1);
                    stmt.setString(3, str2);
                    stmt.setInt(4, int1);
                    stmt.setString(5, str4);
                    stmt.setString(6, "1");

                    stmt.executeUpdate();
                    stmt.close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不交电费瞎发啥光 2024-12-16 01:46:37

您的插入语句中有一个尾随逗号。将其删除。

+ " DEPARTMENT_ID," // <-- Here is the trailing comma
+ ") VALUES(?, ?, ?, ?, ?, ?)";

另外,我猜测 USER_ID 的类型为 NUMBER,并且您尝试在此列中存储字符串“user_id_seq.nextval”。

如果要插入序列的下一个值,则必须首先发出一个 SQL 查询来选择序列的下一个值,提取此查询返回的数字,然后使用 setInteger 在插入语句中设置它或setLong

或者您可以只使用以下查询:

"INSERT INTO DQMP.DQ_USER(user_id_seq.nextval, 
+ " USER_SHORTNAME,"
+ ...
+ ") VALUES(?, ?, ?, ?, ?)"; // only 5 parameters

You have a trailing comma in your insert statement. Remove it.

+ " DEPARTMENT_ID," // <-- Here is the trailing comma
+ ") VALUES(?, ?, ?, ?, ?, ?)";

Also, I guess that the USER_ID is of type NUMBER, and you try to store the String "user_id_seq.nextval" in this column.

If you want to insert the next value of a sequence, you must first issue a SQL query which selects the sequence next value, extract the number returned by this query, ans set it in your insert statement using setInteger or setLong.

Or you can just use the following query :

"INSERT INTO DQMP.DQ_USER(user_id_seq.nextval, 
+ " USER_SHORTNAME,"
+ ...
+ ") VALUES(?, ?, ?, ?, ?)"; // only 5 parameters
梦里泪两行 2024-12-16 01:46:37

我不知道是不是这样,但是您的 INSERT 语句最终会看起来像 INSERT INTO ... DEPARTMENT_ID,) VALUES(...

您已经得到了DEPARTMENT_ID 之后的杂散 ,

I don't know if that's it, but your INSERT statement will end up looking like INSERT INTO ... DEPARTMENT_ID,) VALUES(....

You've got a stray , after DEPARTMENT_ID.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文