Skip to Content
Author's profile photo KOWSHIK DUTTA

Sample Java code for loading files from local machine to HANA

Subject:

Need to upload a PDF, WORD or Image file from local desktop to HANA

Prerequisites:

1) You have installed HANA studio and client in your desktop

2) Create a table in HANA to store the files:

create column table “YOURSCHEMA”.”SOURCETABLE”( “FILENAME” VARCHAR (20) null,

  “FILECONTENT” BLOB null)

Development:

Step 1) Create a JAVA project in HANA studio. Put “ngdbc.jar” on your libraries in the Java build path as shown in the attachment.

Step 2) Write the below Java code in HANA Studio ( JAVA EE perspective ):

package uploadText;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;

public class uploadClass {

  public static void main(String[] args) throws IOException {

  // TODO Auto-generated method stub

  String inputFile = “d:\\ABCD.pdf”;

  byte[] buffer = new byte[50000000];

  Connection con;

  PreparedStatement stmt;

  int rs;

  try {

  Class.forName(“com.sap.db.jdbc.Driver”);

  con = java.sql.DriverManager.getConnection(“jdbc:sap://HANAIP:HANAPORT”, “HANA_ID”, “HANA_PASSWORD”);

  FileInputStream file = new FileInputStream(inputFile);

  file.read(buffer);

  stmt = con.prepareStatement(“INSERT INTO \”YOURSCHEMA\”.\”SOURCETABLE\” VALUES (?,?)”);

  stmt.setString(1,”ABCD.pdf”);

  stmt.setBytes(2,buffer);

  rs = stmt.executeUpdate();

  if(rs == 1){

  System.out.println(“File uploaded successfully”);

  }

  stmt.close();

  file.close();

  con.close();

  } catch (ClassNotFoundException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  } catch (SQLException e){

  e.printStackTrace();

  } catch (FileNotFoundException e){

  e.printStackTrace();

  } catch (IOException e){

  e.printStackTrace();

  }

  }

}

Step 3) In HANA Studio console you will see the message “File uploaded successfully”.

Step 4) Run the below SQL:

SELECT * FROM SOURCETABLE

You will see the table content.

I could load a text as well as image file using the same code. Just need to change the file name.

Assigned Tags

      2 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi KOWSHIK DUTTA

      I tried your document. I was able to connect to the JDBC but i'm getting the following error.

      com.sap.db.jdbc.exceptions.jdbc40.SQLInvalidAuthorizationSpecException: [10]: invalid username or password:

        at com.sap.db.jdbc.exceptions.jdbc40.SQLInvalidAuthorizationSpecException.createException(SQLInvalidAuthorizationSpecException.java:40)

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

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

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

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

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

        at com.sap.db.util.security.AbstractAuthenticationManager.connect(AbstractAuthenticationManager.java:43)

        at com.sap.db.jdbc.ConnectionSapDB.openSession(ConnectionSapDB.java:577)

        at com.sap.db.jdbc.ConnectionSapDB.doConnect(ConnectionSapDB.java:427)

        at com.sap.db.jdbc.ConnectionSapDB.<init>(ConnectionSapDB.java:175)

        at com.sap.db.jdbc.ConnectionSapDBFinalize.<init>(ConnectionSapDBFinalize.java:13)

        at com.sap.db.jdbc.Driver.connect(Driver.java:209)

        at java.sql.DriverManager.getConnection(Unknown Source)

        at java.sql.DriverManager.getConnection(Unknown Source)

        at uploadText.uploadClass.main(uploadClass.java:23)

      Author's profile photo KOWSHIK DUTTA
      KOWSHIK DUTTA
      Blog Post Author

      Hi,

            Your are getting an invalid ID / Password.

      Please provide your HANA ID  / Password that you use in your HANA studio in the below code.

      con = java.sql.DriverManager.getConnection("jdbc:sap://HANAIP:HANAPORT", "HANA_ID", "HANA_PASSWORD");


      It should work.


      Regards,

      Kowshik