This is a migrated thread and some comments may be shown as answers.

Database Operation with Datasource

1 Answer 287 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Adebayo
Top achievements
Rank 1
Adebayo asked on 30 Jul 2012, 12:00 PM
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.




1 Answer, 1 is accepted

Sort by
0
Adebayo
Top achievements
Rank 1
answered on 07 Aug 2012, 06:51 AM
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
Tags
Data Source
Asked by
Adebayo
Top achievements
Rank 1
Answers by
Adebayo
Top achievements
Rank 1
Share this question
or