简体   繁体   中英

java.sql.SQLSyntaxErrorException: ORA-01747

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

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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