Technology Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
0 Likes
3,061

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.

2 Comments