Skip to Content

How to execute SQL Script file through JDBC connection

Hi,

I am able to save the SQL Commands in a file and call them using following command in hana client.

\i "pth/filename.txt" in command line.

Is there a way to execute the same through java code using a jdbc connection.  Kindly share the code snippet.

I created statement object and callablestatement object.

I am getting the following exception

com.sap.db.jdbc.exceptions.JDBCDriverException: SAP DBTech JDBC: [257]: sql syntax error: incorrect syntax near "\": line 1 col 2 (at pos 2)

  at com.sap.db.jdbc.exceptions.SQLExceptionSapDB.createException(SQLExceptionSapDB.java:345)

  at com.sap.db.jdbc.exceptions.SQLExceptionSapDB.generateDatabaseException(SQLExceptionSapDB.java:185)

  at com.sap.db.jdbc.packet.ReplyPacket.buildExceptionChain(ReplyPacket.java:102)

  at com.sap.db.jdbc.ConnectionSapDB.execute(ConnectionSapDB.java:1033)

  at com.sap.db.jdbc.CallableStatementSapDB.sendCommand(CallableStatementSapDB.java:1790)

  at com.sap.db.jdbc.StatementSapDB.sendSQL(StatementSapDB.java:945)

  at com.sap.db.jdbc.CallableStatementSapDB.doParse(CallableStatementSapDB.java:185)

  at com.sap.db.jdbc.CallableStatementSapDB.constructor(CallableStatementSapDB.java:144)

  at com.sap.db.jdbc.CallableStatementSapDB.<init>(CallableStatementSapDB.java:89)

  at com.sap.db.jdbc.CallableStatementSapDBFinalize.<init>(CallableStatementSapDBFinalize.java:31)

  at com.sap.db.jdbc.ConnectionSapDB.prepareCall(ConnectionSapDB.java:1238)

  at com.sap.db.jdbc.trace.Connection.prepareCall(Connection.java:328)

  at mypackage.mypgm.main(Import.java:24)

Regards,

Subramanian S.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Oct 15, 2015 at 08:53 PM

    As you didn't show us what you were doing I cannot tell you what exactly you did wrong. However, the hdbsql functionality to read, parse a text file and execute the SQL commands in it and to finally deal with whatever gets send back by SAP HANA server is of course possible to program also in JAVA using JDBC.

    But it's not present as a feature in the JDBC driver.

    So, you will have to program that yourself. And that to that end, it's really straight forward JAVA programming.

    Read the file, parse it, run the SQL command, deal with the result set.

    Add comment
    10|10000 characters needed characters exceeded