0
votes

Trying to run the db2 command using jdbc. Below is java code and exception

db2 load from file of del modified by coldel0x09 insert into item Code:

           Connection conn = getConnectionFromDB(); // Connection object
           Statement stmt = conn.createStatement(); //Statement object
           sql = "db2 load from filepath of del modified by coldel0x09 insert into tablename";//command
           System.out.println("Executing command: "+ sql);
           stmt.execute(sql);
       } catch (SQLException e) {
           throw new RuntimeException("Error loading into table '" + tableName + "' with SQL: " + sql, e);
       }
   }

getting Exception after run the command:

Caused by: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=load;db2 ;JOIN , DRIVER=4.9.78 at com.ibm.db2.jcc.am.fd.a(fd.java:676) at com.ibm.db2.jcc.am.fd.a(fd.java:60) at com.ibm.db2.jcc.am.fd.a(fd.java:127) at com.ibm.db2.jcc.am.en.c(en.java:2553) at com.ibm.db2.jcc.am.en.d(en.java:2541) at com.ibm.db2.jcc.am.en.b(en.java:1957) at com.ibm.db2.jcc.t4.cb.h(cb.java:221) at com.ibm.db2.jcc.t4.cb.b(cb.java:47) at com.ibm.db2.jcc.t4.q.b(q.java:38) at com.ibm.db2.jcc.t4.rb.h(rb.java:114) at com.ibm.db2.jcc.am.en.hb(en.java:1952) at com.ibm.db2.jcc.am.en.a(en.java:3040) at com.ibm.db2.jcc.am.en.e(en.java:1028) at com.ibm.db2.jcc.am.en.execute(en.java:1012)

2
Incorrect code. LOAD is a command understood by the Db2-CLP, and so it is not SQL and therefore cannot be executed as SQL. Lookup the stored procedure ADMIN_CMD , which lets you do a load for Db2-on-Linux/Unix/Windows only. See ibm.com/support/knowledgecenter/SSEPGG_11.5.0/…mao
Also consider using an EXTERNAL TABLE to load your data if you are using Db2 11.5. This is an SQL statement and not a command like LOAD ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/… it can also load remote files, again unlike LOADPaul Vernon

2 Answers

2
votes

Error -104 is a syntax error, "unexpected token". What you are trying to do is to execute a command line command within a JDBC environment. That does not work by design.

You may want to look into executing LOAD by calling the ADMIN_CMD procedure. ADMIN_CMD allows to run administrative commands without having the command line interface available.

0
votes

As per data_henkik comment your code should look like:

  String storedProcedure = "CALL SYSPROC.ADMIN_CMD('load from [insert full load command here]')";
  CallableStatement cstm = conn.prepareCall(storedProcedure);
  cstm.execute();