Skip to Content

Hello Friends,

                      My ultimate aim in this blog is to explain how to use MaxDB in Webdynpro applications, so that your webdynpro application becomes persistent with the data entered. Before starting up I would like to confirm with the pre requisites.

1.)    MaxDB installed to the server where you deploy your Webdynpro application.

2.)    SAP NWDS installed.

3.)    Check for the availability of JDBC Driver in your Visual Administrator.

 

Step1:

Create a Dictionary Project.

Go to Dictionary Perspective-> New Project.

Enter the Project Name and click finish.

Now expand the sub tree and create a new table by right clicking on the Tables option in the Dictionary Project created. Create table rows with the data type.

  

Now rebuild the project, create archive. You would find the .sda archive created in the Navigator.

Now deploy your application to the J2EE Engine. Therefore the Table instance is created in the J2EE Engine.Snapshot

So the Database part is over and we shall create the Webdynpro application to access the table rows.

 Step2: Create a Webdynpro Application to Access the Database using Jdbc Connectivity

Create a Webdynpro application with views and component.

Create a context in the component controller. A Value Node with cardinality 1…n and value attributes as that of the columns which you created in the dictionary table. Bind this node to a form in the view.

Create another context in the component controller with a value node having Cardinality 0..n,  bound to a table displaying data from the database.

Use buttons which performs the action to add the data to the database remove data and retrieve data.

In the Event Handler of the button write the following code.

/* Event handler to Insert a new set of data to the Database*/

try
    {
      
      InitialContext ctx = new InitialContext();
      java.sql.DataSource ds = (java.sql.DataSource)ctx.lookup("jdbc/DataSourceName");
                  Connection con = ds.getConnection();
                  
                  PreparedStatement pstm = con.prepareStatement("insert into TMP_PHONEBOOK(FIRSTNAME,LASTNAME,LOCATION,MOBILENUMBER,LANDLINE,EMAIL) values(?,?,?,?,?,?)");
                  pstm.setString(1,wdContext.currentInputNodeElement().getF_name());
                  pstm.setString(2,wdContext.currentInputNodeElement().getL_name());
                  pstm.setString(3,wdContext.currentInputNodeElement().getLocation());
                  pstm.setLong(4,wdContext.currentInputNodeElement().getMob_number());
                  pstm.setInt(5,wdContext.currentInputNodeElement().getLandline());
                  pstm.setString(6,wdContext.currentInputNodeElement().getEmail());
                  

                  pstm.executeUpdate();

                  
                  con.close();
            wdContext.nodeInputNode().invalidate();
            
      
    }
    catch(Exception e)
    {
      wdComponentAPI.getMessageManager().reportWarning("Warning::"+e.getLocalizedMessage());
    }
 
/*Event handler to Retrieve Data From The Database*/
 
wdContext.nodeOutputNode().invalidate();
    try
    {
            InitialContext ctx = new InitialContext();
            java.sql.DataSource ds = (java.sql.DataSource)ctx.lookup("jdbc/DataSourceName");
            Connection con = ds.getConnection();
            
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from TMP_PHONEBOOK");
            
            while(rs.next())
            {
                  IPrivatePhoneBookView.IOutputNodeElement ele = wdContext.createOutputNodeElement();
                  ele.setF_name(rs.getString("FIRSTNAME"));
                  ele.setL_name(rs.getString("LASTNAME"));
                  ele.setLocation(rs.getString("LOCATION"));
                  ele.setMob_number(rs.getLong("MOBILENUMBER"));
                  ele.setLandline(rs.getInt("LANDLINE"));
                  ele.setEmail(rs.getString("EMAIL"));
                  
                  wdContext.nodeOutputNode().addElement(ele);
            }
            
            con.close();
      
      
    }
    catch(Exception e)
    {
      wdComponentAPI.getMessageManager().reportWarning("Warning::"+e.getLocalizedMessage());
    }
 
/*Event handler to Delete a particular data from the Database*/
try    {     
InitialContext ctx = new InitialContext(); 
DataSource ds = (DataSource)ctx.lookup("jdbc/DataSourceName"); 
Connection con = ds.getConnection();  
PreparedStatement stmt = con.prepareStatement("delete from TMP_PHONEBOOK where FIRSTNAME = ?"); 
stmt.setString(1,wdContext.currentContextElement().getDelName());
stmt.executeUpdate();  
 con.close();  
} catch(Exception e)    
   wdComponentAPI.getMessageManager().reportWarning("Warning::"+e.getLocalizedMessage());
  Now you can rebuild your Project and deploy the application. Similarly you can use other SQL statements for required Operations. 

 

Please note:

1.)The DataSourceName should be obtained from the Visual Administrator.Login to the Visual Administrator. 
Go to Cluster- Server - Services - JDBC Connectors - DataSources. 

You could see the DataSource Aliases in the table..

2.) The Highlighted text in this blog denotes name of the node,attributes, table etc., which are not generic.

Regards,

Karthikeyan.R

To report this post you need to login first.

9 Comments

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

  1. Jawed Macknojia
    The practice you just shown isn’t worth implementing, because MVC pattern (Web dynpro is based on) doesn’t encourage you to write your persistence code in the view or controller, thats the responsibility of the model I guess?

    If you could write same code in the model then it has worth.

    Regards,
    Jawed Ali

    (0) 
    1. Karthikeyan Rammohan Post author
      Hi Jawed,
                 Thank u for the comment. Since i was just implementing the scenario, i did not code it in the component controller, which also goes against the MVC paradigm. I ve now changed the part which u mentioned.

      Regards,
      Karthik

      (0) 
  2. Vladimir Pavlov
    Karthik,

    sorry to say that but I didn’t even read this, I stopped at the first sentence. Data handling should happen in the persistence layer of your app, not in the UI layer what Web Dynpro is (meant for).

    Don’t understand how it could appear on the SDN home page?!


    Vladimir

    (0) 
  3. Walter Kahn
    In all fairness to Karthik, I think that the main emphasis of the blog is the correct use of JDBC with MaxDB, and [b]not[/b] a tutorial about [i]correct[/i] WD design.

    Walter

    (0) 
    1. Vladimir Pavlov
      Really? Is it?! “My ultimate aim in this blog is to explain how to use MaxDB in Webdynpro applications”
      The correct use of JDBC with MaxDB is in the DAO layer – and it certainly does not suggest root lookup of the DataSource. (took a look at the sample code after your comment)


      Vladimir

      (0) 
  4. Kilian Kiko
    Hi

    For one who is used to Java and MVC this really hurts. However, I have seen a lot of ABAP code that just goes likes this.

    To all of you WDA or ABAP programmers, please stick to your knitting and don’t try this with WDJ. Thanks.

    Regards
    Kilian

    (0) 
  5. SAP Learner
    * Do we need to create DataSource in Visual Admin? Can we use default one?

        * After inserting data into DataBase, how can i check inserted data? Is it with SQL Studio’s? If yes, what is the default UID for MaxDB?

        * Is there any other approach, to communicate with MaxDB and EJB/ WD?

        * If you have any good documentation please pass it to me also.

    (0) 
    1. Karthikeyan Rammohan Post author
      Hi,
      1.) You can either use the existing datasource alias or couls create a new one in the visual admin.
      2.) You can view the data inserted using SQL studio. the default user authentication should be SUPERDBA and passwd: mymaster. If this password doesn’t work contact your admin.

      Regards,
      Karthikeyan R

      (0) 

Leave a Reply