Skip to Content
Creating DataSource

Lets start with creating the Datasource in the VisualAdministrator .For creating datasource i would suggest you to go through the thread where Lohita explained in detail on how to install the JDBC driver and creating the Datasource in VisualAdministrator .

Before stating your coding in WebDynpro ,we can test the Datasource in VisualAdmin itself.Select JDBC Connector->Select Your DataSource->Goto DB Initialization Tab and select “Add Statement”.

Enter a valid select statement and click on “execute” button .You can see the sample screen shots below

Writing Image to a Table

First we will create a table with one column of type IMAGE in SQLServer

Create a FileUpload UIElement on he WebDynpro view so that user can select a particular Image file and after clicking on a button it should add a new record to the above created table.The resource property of FileUpload UIElement should be bound to a value attribute of type “com.sap.ide.webdynpro.uielementdefinitions.Resource”

The following code will read the image file from the FileUpload UIElement and write it into the table

Reading from the Table


Find the code below for reading the Image from a table and writing to a context .Here you need to create a valueNode “ImageTable” and valueattributes “Name” of type String and “Image” of type Binary



Once you have context ready you can display images in different ways !!! Following is example for showing Images from Database in a WebDynpro table .

image

Screenshots of creating Datasource


image

image

We have one weblog Uploading and Downloading Images in MaxDB Database written by Balakrishnan.He explaind writing and reading images from MaxDB very well. On the top my weblog explains configuring the Datasource for SQLServer and directly displaying the images through context instead of writing to a file using FileOutputStream

To report this post you need to login first.

4 Comments

You must be Logged on to comment or reply to a post.

  1. Anilkumar Vippagunta Post author
    This code works on NW04S release.
    We should apply Binary cache service to achieve this on NW04 .

    Thanks to Bertram for helping &correcting me on this.

    Regards,Anilkumar

    (0) 
  2. Gita Chhatri
    Hi Anil,
    Thanks for such a helpful blog.
    The upload image is working in my case but im getting the following error while i try retrieving the image in web dynpro.

    first of all, the code:
    imageEle.setImage(res.getUrl(WDFileDownloadBehaviour.OPEN_INPLACE.ordinal()));
    is not working in my case, it gives an error at designtime saying that the type of Image is byte[] and im trying to assign a string to it.

    Then i changed my code to :
    imageEle.setImage(res.getUrl(WDFileDownloadBehaviour.OPEN_INPLACE.ordinal()).getBytes());

    Now it gives the following runtime exception:
    java.lang.IllegalArgumentException
         at com.sap.dictionary.runtime.DdTypeBinary.format(DdTypeBinary.java:60)
         at com.sap.tc.webdynpro.clientserver.data.DataContainer.doFormat(DataContainer.java:1405)
         at com.sap.tc.webdynpro.clientserver.data.DataContainer.getAndFormat(DataContainer.java:1098)
         at com.sap.tc.webdynpro.clientserver.data.DataContainer.getAndFormat(DataContainer.java:1070)
         at com.sap.tc.webdynpro.clientserver.uielib.standard.impl.TextView.getText(TextView.java:907)
         at com.sap.tc.webdynpro.clientserver.uielib.standard.uradapter.TextViewAdapter$TextViewProxy.getText(TextViewAdapter.java:765)
         at com.sap.tc.ur.renderer.ie6.TextViewRenderer.render(TextViewRenderer.java:48)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:434)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:133)
         at com.sap.tc.ur.renderer.ie6.SapTableCellRenderer.renderSapTableGenericCellFragment(SapTableCellRenderer.java:1188)
         at com.sap.tc.ur.renderer.ie6.SapTableCellRenderer.renderSapTableCellFragment(SapTableCellRenderer.java:205)
         at com.sap.tc.ur.renderer.ie6.SapTableCellRenderer.render(SapTableCellRenderer.java:84)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:434)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:133)
         at com.sap.tc.ur.renderer.ie6.SapTableRowRenderer.renderSapTableRowFragment(SapTableRowRenderer.java:105)
         at com.sap.tc.ur.renderer.ie6.SapTableRowRenderer.render(SapTableRowRenderer.java:49)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:434)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:133)
         at com.sap.tc.ur.renderer.ie6.SapTableDefaultBodyRenderer.renderSapTableDefaultBodyFragment(SapTableDefaultBodyRenderer.java:214)
         at com.sap.tc.ur.renderer.ie6.SapTableDefaultBodyRenderer.render(SapTableDefaultBodyRenderer.java:44)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:434)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:133)
         at com.sap.tc.ur.renderer.ie6.SapTableRenderer.renderSapTableFragment(SapTableRenderer.java:769)
         at com.sap.tc.ur.renderer.ie6.SapTableRenderer.render(SapTableRenderer.java:84)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:434)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:133)
         at com.sap.tc.ur.renderer.ie6.MatrixLayoutRenderer.renderMatrixLayoutCellFragment(MatrixLayoutRenderer.java:790)
         at com.sap.tc.ur.renderer.ie6.MatrixLayoutRenderer.renderMatrixLayoutRowFragment(MatrixLayoutRenderer.java:376)
         at com.sap.tc.ur.renderer.ie6.MatrixLayoutRenderer.renderMatrixLayoutFragment(MatrixLayoutRenderer.java:326)
         at com.sap.tc.ur.renderer.ie6.MatrixLayoutRenderer.render(MatrixLayoutRenderer.java:79)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:434)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:133)
         at com.sap.tc.ur.renderer.ie6.ScrollContainerRenderer.renderScrollContainerFragment(ScrollContainerRenderer.java:619)
         at com.sap.tc.ur.renderer.ie6.ScrollContainerRenderer.render(ScrollContainerRenderer.java:74)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:434)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:133)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.UiWindowRenderer.render(UiWindowRenderer.java:52)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:434)
         at com.sap.tc.webdynpro.clientimpl.html.renderer.uielements.base.RenderManager.render(RenderManager.java:133)
         at com.sap.tc.webdynpro.clientimpl.html.client.HtmlClient.sendHtml(HtmlClient.java:1052)
         at com.sap.tc.webdynpro.clientimpl.html.client.HtmlClient.fillDynamicTemplateContext(HtmlClient.java:455)
         at com.sap.tc.webdynpro.clientimpl.html.client.HtmlClient.sendResponse(HtmlClient.java:1236)
         at com.sap.tc.webdynpro.clientimpl.html.client.HtmlClient.retrieveData(HtmlClient.java:252)
         at com.sap.tc.webdynpro.clientserver.window.WindowPhaseModel.doRetrieveData(WindowPhaseModel.java:595)
         at com.sap.tc.webdynpro.clientserver.window.WindowPhaseModel.processRequest(WindowPhaseModel.java:156)
         at com.sap.tc.webdynpro.clientserver.window.WebDynproWindow.processRequest(WebDynproWindow.java:335)
         at com.sap.tc.webdynpro.clientserver.cal.AbstractClient.executeTasks(AbstractClient.java:143)
         at com.sap.tc.webdynpro.clientserver.session.ApplicationSession.doProcessing(ApplicationSession.java:299)
         at com.sap.tc.webdynpro.clientserver.session.ClientSession.doApplicationProcessingStandalone(ClientSession.java:759)
         at com.sap.tc.webdynpro.clientserver.session.ClientSession.doApplicationProcessing(ClientSession.java:712)
         at com.sap.tc.webdynpro.clientserver.session.ClientSession.doProcessing(ClientSession.java:261)
         at com.sap.tc.webdynpro.clientserver.session.RequestManager.doProcessing(RequestManager.java:149)
         at com.sap.tc.webdynpro.serverimpl.defaultimpl.DispatcherServlet.doContent(DispatcherServlet.java:62)
         at com.sap.tc.webdynpro.serverimpl.defaultimpl.DispatcherServlet.doPost(DispatcherServlet.java:53)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
         at com.sap.engine.services.servlets_jsp.server.HttpHandlerImpl.runServlet(HttpHandlerImpl.java:401)
         at com.sap.engine.services.servlets_jsp.server.HttpHandlerImpl.handleRequest(HttpHandlerImpl.java:266)
         at com.sap.engine.services.httpserver.server.RequestAnalizer.startServlet(RequestAnalizer.java:387)
         at com.sap.engine.services.httpserver.server.RequestAnalizer.startServlet(RequestAnalizer.java:365)
         at com.sap.engine.services.httpserver.server.RequestAnalizer.invokeWebContainer(RequestAnalizer.java:944)
         at com.sap.engine.services.httpserver.server.RequestAnalizer.handle(RequestAnalizer.java:266)
         at com.sap.engine.services.httpserver.server.Client.handle(Client.java:95)
         at com.sap.engine.services.httpserver.server.Processor.request(Processor.java:160)
         at com.sap.engine.core.service630.context.cluster.session.ApplicationSessionMessageListener.process(ApplicationSessionMessageListener.java:33)
         at com.sap.engine.core.cluster.impl6.session.MessageRunner.run(MessageRunner.java:41)
         at com.sap.engine.core.thread.impl3.ActionObject.run(ActionObject.java:37)
         at java.security.AccessController.doPrivileged(Native Method)
         at com.sap.engine.core.thread.impl3.SingleThread.execute(SingleThread.java:100)
         at com.sap.engine.core.thread.impl3.SingleThread.run(SingleThread.java:170)

    PLEASE HELP!!! ITS VERY URGENT!!!

    THANK YOU.

    (0) 
  3. Balmer S
    I get “java.lang.NullPointerException”, code is clear. I had to change connection method since the one mensioned in your source were not work for me. Code looks like that:

    try {
              {
                   String userName = “user”;
                   String password
    = “pswd”;
                   String url = “jdbc:mysql://DBadress”;
                   Class.forName (“com.mysql.jdbc.Driver”).newInstance();
                   Connection con = DriverManager.getConnection (url, userName, password);
                   System.out.println (“Database connection established”);       
                   
                      //Context ctx = new InitialContext();
                   //DataSource ds = (DataSource) ctx.lookup(“jdbc/dataSourceName”);                                    
                    //Connection con=ds.getConnection();                    
              
                 Statement  st=con.createStatement();
                 PreparedStatement pSt=con.prepareStatement( “insert into photo values(?)”);
                      
                   IWDResource res=wdContext.currentContextElement().getResource() ;
                   InputStream in = res.read(false);
                   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
                   int length;
                   byte[] part = new byte[10 * 1024];
                   while ((length = in.read(part)) != -1) {
                         bOut.write(part, 0, length);
                   }
                   in.close();
                 pSt.setBytes( 1,bOut.toByteArray());
                 pSt.execute() ;
                 bOut.close();
                wdComponentAPI .getMessageManager().reportSuccess(“sukces”);
              }
                   catch(Exception e)
                   {
                        wdComponentAPI.getMessageManager() .reportWarning( e.toString() );
                   }

    Could you please advice, it is very important for me. Nothing was written into the database. Only Nullpointer error appeared. Code looks clean in NW. Regards, Balmer.

    (0) 
  4. Sudheer Bheemakumar
    Hi Anil,
    I have a requirement of uploading and downloading documents of all file types(doc, pdf, xls, jpg etc) to SQL Server from my webdynpro application. I saw your blog for uploading/downloading image files. It is really good. Is there generic code that can be written to handle all file types ? Can you please send me the same?
    thanks
    Sudheer
    (0) 

Leave a Reply