简体   繁体   中英

java.sql.sqlexpcetion:ORA-00917:missing comma

I am sorry that i ask a very stupid question but i cant find the place where i miss the comma in the code..

sqlStr.append("INSERT INTO DS_GOAL ");
sqlStr.append("(DS_SITE_CODE, DS_FINANCIAL_YEAR, DS_DEPARTMENT_CODE, DS_PLAN_ID, DS_GOAL_ID, ");
sqlStr.append("DS_DESC, TO_CHAR(DS_PLAN_END_DATE, \"dd/MM/YY\"),");
sqlStr.append("DS_CORP_OBJECTIVE, DS_CORP_OBJECTIVE_OTHER, DS_FOCUS, DS_FOCUS_OTHER, ");
sqlStr.append("DS_TOTAL, DS_EQUIPMENT, DS_RECRUIT, DS_FTE, ");
sqlStr.append("DS_CREATED_USER, DS_MODIFIED_USER, DS_GOAL_ORDER ) ");
sqlStr.append("VALUES ");
sqlStr.append("(?, ?, ?, ?, ?,");
sqlStr.append("?, ?,");
sqlStr.append("?, ?, ?, ?,");
sqlStr.append("?, ?, ?, ?,");
sqlStr.append("?, ?, ?)");
sqlStr_insertGoal = sqlStr.toString();

After the sqlStr.toString()

the console shows

 INSERT INTO DS_GOAL (DS_SITE_CODE, DS_FINANCIAL_YEAR, DS_DEPARTMENT_CODE, DS_PLAN_ID,
 DS_GOAL_ID, 
 DS_DESC, TO_CHAR(DS_PLAN_END_DATE, 'dd/MM/YYYY'), 
 DS_CORP_OBJECTIVE, DS_CORP_OBJECTIVE_OTHER, DS_FOCUS, DS_FOCUS_OTHER, 
 DS_TOTAL, DS_EQUIPMENT, DS_RECRUIT,
 DS_FTE, DS_CREATED_USER, DS_MODIFIED_USER, DS_GOAL_ORDER)
 VALUES (?, ?, ?, ?, ?,?, ?,?, ?, ?, ?,?, ?, ?, ?,?, ?, ?)

After Edited the code the console shows

INSERT INTO DS_GOAL (DS_SITE_CODE, DS_FINANCIAL_YEAR, DS_DEPARTMENT_CODE, DS_PLAN_ID,
 DS_GOAL_ID, 
 DS_DESC, DS_PLAN_END_DATE, 
 DS_CORP_OBJECTIVE, DS_CORP_OBJECTIVE_OTHER, DS_FOCUS, DS_FOCUS_OTHER, 
 DS_TOTAL, DS_EQUIPMENT, DS_RECRUIT,
 DS_FTE, DS_CREATED_USER, DS_MODIFIED_USER, DS_GOAL_ORDER)
 VALUES (?, ?, ?, ?, ?,?, TO_CHAR(DS_PLAN_END_DATE, 'dd/MM/YYYY'),?, ?, ?, ?,?, ?, ?,
 ?,?, ?, ?)

But the consoles shows invalid column index error Thanks for help

I suspect your problem isn't actually a case of a missing comma (in my experience ORA errors are notorious for telling you the wrong thing). My suspicion is that your real issue is the use of " around the format string in your TO_CHAR call. To demonstrate, try this:

SELECT TO_CHAR(SYSDATE, "dd/MM/YY")
  FROM DUAL;

If I run the above I get an ORA-00904: "dd/MM/YY": invalid identifier error. If I change the quotes to apostrophes instead:

SELECT TO_CHAR(SYSDATE, 'dd/MM/YY')
  FROM DUAL;

I get 16/04/14 . Double quotes are for identifiers, not strings:

SELECT TO_CHAR(SYSDATE, 'dd/MM/YY') AS "The Date"
  FROM DUAL;                        // ^ This is an identifier

prints:

The Date
--------
16/04/14

EDIT:

Sorry, I should have spotted this one sooner! You're using TO_CHAR in your columns list, which you can't do. The below example nicely produces an ORA-00917: missing comma error:

CREATE TABLE JON_TEST (COL1 VARCHAR2(20));

COMMIT;

INSERT INTO JON_TEST (TO_CHAR(COL1, 'DD/MM/YYYY'))
VALUES (SYSDATE);

Whereas this works:

INSERT INTO JON_TEST (COL1)
VALUES (TO_CHAR(SYSDATE, 'dd/MM/YYYY'));

So you need to correct three things:

  • You need to change TO_CHAR to TO_DATE , and
  • You need to move the call to TO_DATE to your VALUES clause, and
  • You need to ensure that you use ' instead of " with the format string.

This is how Oracle define the syntax for INSERT statements :

这就是Oracle如何定义INSERT语句的语法

Notice that in the middle section that it only says column_name and not sql_expression .

Try changing your query to the following:

sqlStr.append("INSERT INTO DS_GOAL ")
    .append("(DS_SITE_CODE, DS_FINANCIAL_YEAR, DS_DEPARTMENT_CODE, DS_PLAN_ID, DS_GOAL_ID, ")
    .append("DS_DESC, DS_PLAN_END_DATE, ")
    .append("DS_CORP_OBJECTIVE, DS_CORP_OBJECTIVE_OTHER, DS_FOCUS, DS_FOCUS_OTHER, ")
    .append("DS_TOTAL, DS_EQUIPMENT, DS_RECRUIT, DS_FTE, ")
    .append("DS_CREATED_USER, DS_MODIFIED_USER, DS_GOAL_ORDER ) ")
    .append("VALUES ")
    .append("(?, ?, ?, ?, ?,")
    .append("?, TO_DATE(?, 'dd/MM/YY'),")
    .append("?, ?, ?, ?,")
    .append("?, ?, ?, ?,")
    .append("?, ?, ?)");
    sqlStr_insertGoal = sqlStr.toString();

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