java.sql.SQLException:: ORA-00923: FROM keyword requested was not found error sql java code execution

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_41885819/article/details/100540762

Recently wrote a particularly large number of database synchronization interface, you need to splice in java sql large segment of the program, and then submitted to the Executive, there have been many times ORA-00923 errors, a little flattering.

For example, the following code, when executed db.execute (insertSql) will certainly report this error.

@Override
public int doInsert(DB db,InterfaceLogBean logBean) {
	StringBuilder insertBuilder = new StringBuilder();
	insertBuilder.append("INSERT INTO ");
	insertBuilder.append("MTL_ONHAND_QUANTITIES_DETAIL ");
	insertBuilder.append("(INVENTORY_ITEM_ID,ITEM_CODE,ORGANIZATION_ID,DATE_RECEIVED,LAST_UPDATE_DATE,LAST_UPDATED_BY,CREATION_DATE,CREATED_BY,");
	insertBuilder.append("PRIMARY_TRANSACTION_QUANTITY,SUBINVENTORY_CODE,LOT_NUMBER,ONHAND_QUANTITIES_ID,ORGANIZATION_TYPE,");
	insertBuilder.append(" OWNING_ORGANIZATION_ID,TRANSACTION_UOM_CODE,TRANSACTION_QUANTITY,IS_CONSIGNED)");
	insertBuilder.append("SELECT INVENTORY_ITEM_ID,ITEM_CODE,84,DATE_RECEIVED,LAST_UPDATE_DATE,LAST_UPDATED_BY,CREATION_DATE,CREATED_BY, ");	
	insertBuilder.append("PRIMARY_TRANSACTION_QUANTITY,SUBINVENTORY_CODE,LOT_NUMBER,ONHAND_QUANTITIES_ID,ORGANIZATION_TYPE,");
	insertBuilder.append("OWNING_ORGANIZATION_ID,TRANSACTION_UOM_CODE,TRANSACTION_QUANTITY,IS_CONSIGNED");
	insertBuilder.append("FROM MTL_ONHAND_QUANTITIES_TEMP MOQT ");
	insertBuilder.append("WHERE 1=1 AND NOT EXISTS(SELECT 1 FROM MTL_ONHAND_QUANTITIES_DETAIL MOQD WHERE 1=1 AND MOQD.ONHAND_QUANTITIES_ID = MOQT.ONHAND_QUANTITIES_ID)");
	String insertSql = insertBuilder.toString();
	insertBuilder.delete(0, insertBuilder.length());
	try {
			return db.execute(insertSql);
		} catch (JDBCException e) {
			e.printStackTrace();
			logBean.setExceptContent(e.getMessage());
			return -1;
		} catch (Exception e) {
			e.printStackTrace();
			logBean.setExceptContent(e.getMessage());
			return -1;
		}
	}

If you have time, you can look at DEBUG, this sql before insertSql execution is valid, then copied to the plsql perform what you know where the problem lies.
The reason is simple, the stitching is sql when the spaces or where there is no line breaks, which is the penultimate behind append a need to stay out of space, so it will not back IS_CONSIGNED and rear FROM pieces together, if not this space, insertSql will appear in IS_CONSIGNEDFROM, naturally can not find the keyword FROM.
Yet another method is to be spliced back together with each piece of string "\ n" string format such splicing will wrap each segment.

  • Examples of a
	public static void main(String[] args) {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("SELECT ID,NAME,AGE");
		stringBuilder.append("FROM STUDENT");
		System.out.println(stringBuilder.toString());
		//输出内容为:SELECT ID,NAME,AGEFROM STUDENT
		//会报错ORA-00923
	}
  • Example Two
	public static void main(String[] args) {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("SELECT ID,NAME,AGE\n");
		stringBuilder.append("FROM STUDENT\n");
		System.out.println(stringBuilder.toString());
		/**
		符合要求
		输出内容为:
		SELECT ID,NAME,AGE
		FROM STUDENT		
		*/
	}
  • Three examples
	public static void main(String[] args) {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("SELECT ID,NAME,AGE ");
		stringBuilder.append("FROM STUDENT");
		System.out.println(stringBuilder.toString());
		/**
		符合要求
		输出内容为:
		SELECT ID,NAME,AGE FROM STUDENT
		*/
	}

Guess you like

Origin blog.csdn.net/qq_41885819/article/details/100540762