1.11M Members

Adding Json data as a source for dojogrid

 
0
 

0 down vote favorite
share [g+] share [fb] share [tw]


I have a class in which i have a method name method() returning a String in the form of JSON string. The output is ::

[{"ID":1,"Names":"Shantanu"},{"ID":2,"Names":"Mayur"},{"ID":3,"Names":"Rohit"},{"ID":4,"Names":"Jasdeep"},{"ID":5,"Names":"Rakesh"}]

Now this in the form of data that i want to Populate for Datagrid using Dojo. And in My jsp page i am using it as follows ::

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="MyPackage.PopulateTextbox" %>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
@import "http://localhost:8080/2_8_2012/js/dojo/resources/dojo.css";

@import "http://localhost:8080/2_8_2012/js/dijit/themes/nihilo/nihilo.css";
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/dojo/1.4/dojo/dojo.xd.js" djConfig="isDebug: false, parseOnLoad: true"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">

dojo.require("dojox.grid.DataGrid");
dojo.require("dojo.data.ItemFileReadStore");
</script>

<script type="text/javascript">
var temp1;
$(document).ready(function() {

PopulateTextbox obj = new PopulateTextbox();
temp1 = obj.method();
});
out.println(temp1);
var storedata={
        identifier:"table1",
        label:"name",
        items: temp1
}
var gridStructure =[{
cells:[
            [
                  { field: "ID",
                        name: "ID_Emp",
                        width: "40%", styles: 'text-align: right;'
                  },
                  {
                      field: "Names",
                      name: "Name",
                      width: "40%", styles: 'text-align: right;'
                  }

            ]
      ]
}];

</script>

<title>Dojo Data</title>
</head>
<body class=nihilo>
<div style="width: 400px; height: 300px;">
  <div data-dojo-type="dojo.data.ItemFileReadStore" data-dojo-id="countryStoreForGrid" data-dojo-props="data:storeData"></div>
  <div id="grid"
    data-dojo-type="dojox.grid.DataGrid"
    data-dojo-props="store:countryStoreForGrid,
    structure:'gridStructure',
    queryOptions:{deep:true},
    query:{},
    rowsPerPage:40">
  </div>
</div>

</body>
</html>

What am i doing is as the page loads i am making an object of my PopulateTextbox class and calling the method which is returning a JSON string. And i am passing that string under ITEMS: as my data for Grid. Is it the right way of doin this ? can i call a method like this in javascript? If not, please suggest me the right way . When i run run this page i am getting an error ::

Webpage error details

Message: Expected ';'
Line: 25
Char: 17
Code: 0
URI: http://localhost:8080/2_8_2012/DisplayData.jsp

Its the same line where i am writing

PopulateTextbox obj = new PopulateTextbox();

Code for PopulateTextbox.java

package MyPackage;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.google.gson.Gson;

public class PopulateTextbox {

    Gson gson = new Gson();
    JSONObject obj = new JSONObject();
    JSONArray arr = new JSONArray();
    String temp1;

    String temp;
    List <String>rowValues = new ArrayList<String>();
    List <Integer>rowValues1 = new ArrayList<Integer>();
    String[] contactListNames;
    Connection con=null;
    Statement st=null;
    ResultSet rs=null;


    public String method(){


        try{


        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        Class.forName(driver);

        String db = "jdbc:odbc:Practice_Database";
        con = DriverManager.getConnection(db,"","");

        st = con.createStatement();
        String sql = "SELECT Emp_Name,ID FROM EmployeeSearch";
        rs = st.executeQuery(sql);

        while(rs.next()){
            obj.put("ID", rs.getInt("ID"));
            obj.put("Names",rs.getString("Emp_Name"));
            arr.add(obj);
            //obj.add("Name", rs.getString("Emp_Name"));
            //rowValues1.add(rs.getInt("ID"));
            //rowValues.add(rs.getString("Emp_Name"));
            //obj.accumulate("Names",rs.getString("Emp_Name"));
        }
        //obj.accumulate("ID",rowValues1);

        //obj.accumulate("Names",rowValues);
        temp1 = arr.toString();
        System.out.println(temp1);
        contactListNames = (String[]) rowValues.toArray(new String[rowValues.size()]);
        temp = gson.toJson(contactListNames);




    }catch(Exception e){System.out.println(e);}
    /*finally{
        try {
                if(con!=null)con.close();
            } catch (SQLException e) {

                e.printStackTrace();
            }
        try {
            if(rs!=null)rs.close();
        } catch (SQLException e) {

            e.printStackTrace();
        }try {
            if(st!=null)st.close();

        } catch (SQLException e) {

            e.printStackTrace();
        }


    }*/
        return temp1;

    }
    public static void main(String args[])
    {
        PopulateTextbox obj = new PopulateTextbox();
        String temp1= obj.method();


    }
}

Please help. thanks in advance..

You
Post:
Start New Discussion
View similar articles that have also been tagged: