Database Operation with Datasource

2 posts, 0 answers
  1. Adebayo
    Adebayo avatar
    3 posts
    Member since:
    Mar 2012

    Posted 30 Jul 2012 Link to this post

    Hello  All ,

    Im new    to Kendoui  web   and  i have been  getting my hands  dirty  with it .  but    i need  help  with  this  issue. im  using kendo with java or jsp  and  i could   read  records  from  an oracle database  unto the   grid.  and  i could  do an insert or  create operation to the  same oracle  database. but  i could not  do an update or delete operation  on the grid.  i keep  getting  an error . i  need  somebody to  tell  me what im doing  wrongly.  here is a sample of my code :   


      <Script>  
          
             
                          
        $("#grid").kendoGrid({
            
        dataSource: {
            transport: {
                read: "listdepart.jsp",
                create: {
                    url: "insertdepart.jsp",
                    type: "POST"
                } 
               
            },
            update:{
                url: "updatedepart.jsp",
                type:"POST"
            },
            destroy:{
                url:"deleletdepart.jsp",   
                type:"POST"
            },
            
              parameterMap: function (options, operation) {
                            if (operation !== "read" && options.models) {
                                return { models: kendo.stringify(options.models) };
                            }
                        
                    },
                batch: true,
                pageSize:5 , 


            schema: {
                data: function(result) {     
                  return result.data || result;
                },
                total: function(result) { 
                    var data = this.data(result);
                    return data ? data.length : 0;
                
            },
                
                model: {
                    id: "deptid",
                    fields: {
                       // id: { editable: false },
                       id: { validation: { required: true} ,type: "number" } ,
                       name: { validation: { required: true} }
                    }
                }
                
            }
          
        },
        
      
        
        //detailTemplate: kendo.template($("#template").html()),
       //// detailInit: detailInit,
        editable: true,
        navigable: true,  // enables keyboard navigation in the grid
        pageable:true,
        serverPaging: true,
        groupable: true,
        sortable:true ,
        filterable:true,
       // toolbar: [ "save", "cancel" ,"create" ],  // adds save and cancel buttons,
        toolbar: [ { name :"create" , text: "Add New Department" }],  // adds save and cancel buttons,
        editable:"popup" ,
        columns: [{title: "Department ID", field: "deptid" }, { title: "Department Name",field: "name" } ,{ title: "Action", command: ["edit", "destroy"] }]
       
       
        
    });
              
     </script>

    here  are  my  jsp 

    for insert
    ===========
    <%
     
     response.setContentType("application/json"); 
    try  {
    String dept_id = request.getParameter("deptid");
    String dept_name = request.getParameter("name");




    String jdbcname = "jdbc/orcl";
        
        
        Context ctx = new InitialContext();
        Context envCtx = (Context)ctx.lookup("java:/comp/env");
        DataSource ds = (DataSource)envCtx.lookup(jdbcname);
        Connection conn = ds.getConnection();
        Statement stmt = conn.createStatement();
         ResultSet rs  = null ;




    // if (bsave.equals("savechanges")) {
    String cmd2 = "insert into   department  (deptid , name)  values ('"+dept_id+"' , '"+dept_name+"')";
      rs = stmt.executeQuery(cmd2);
                if (rs.next()) {
            StringBuffer data = new StringBuffer("{\"data\":["); 
                   data.append("{\"Insert was done sucessfully"  + "\"},");
                 data.setCharAt( data.length()-1,']');  // replace last character with ]
                 data.append("}");
              out.println(data.toString());
                } else {
                  StringBuffer data = new StringBuffer("{\"data\":["); 
                   data.append("{\"Insert was not done sucessfully"  + "\"},");
                 data.setCharAt( data.length()-1,']');  // replace last character with ]
                 data.append("}");
              out.println(data.toString());
    conn.close();
                }
         //}





     } catch(Exception e) {
        out.println("Error: " + e);
        e.printStackTrace();
      }
    %> 

    ===============================================================================
    for delete or update ( Almost  the   same just operations  )
    ==============================================================


     response.setContentType("application/json"); 
    try  {
    String dept_id = request.getParameter("deptid");
    String dept_name = request.getParameter("name");




    String jdbcname = "jdbc/orcl";
        
        
        Context ctx = new InitialContext();
        Context envCtx = (Context)ctx.lookup("java:/comp/env");
        DataSource ds = (DataSource)envCtx.lookup(jdbcname);
        Connection conn = ds.getConnection();
        Statement stmt = conn.createStatement();
         ResultSet rs  = null ;






    String cmd2 = "delete  from   department  where  deptid = '"+dept_id+"'";
      rs = stmt.executeQuery(cmd2);
                if (rs.next()) {
            StringBuffer data = new StringBuffer("{\"data\":["); 
                   data.append("{\"Delete Operation  was sucessfully"  + "\"},");
                 data.setCharAt( data.length()-1,']');  // replace last character with ]
                 data.append("}");
              out.print(data.toString());
                } else {
                  StringBuffer data = new StringBuffer("{\"data\":["); 
                   data.append("{\"Delete Operation  was not  sucessfully"  + "\"},");
                 data.setCharAt( data.length()-1,']');  // replace last character with ]
                 data.append("}");
              out.print(data.toString());
    conn.close();
                }
     } catch(Exception e) {
        out.println("Error: " + e);
        e.printStackTrace();
      }
    %> 
            
    Any  help  to get this working   will  really  be appreciated.




  2. Adebayo
    Adebayo avatar
    3 posts
    Member since:
    Mar 2012

    Posted 07 Aug 2012 Link to this post

    I got  this   resolved .  The issue  is  an extra curly brace ( } ) on the  datasource   after  the create operation.  with this  extra } the  other operations (update and delete)   were  not visible.  so thats  why  if  i call  a delete or update operation nothing  happens.


    Thanks
Back to Top