PDA

View Full Version : [Java] Returning JSON Array from DB to Grid Problem



talha06
28 Feb 2010, 11:31 AM
Hello everyone,

I was using XML Reader and writing the results to XML to communicate with DB. I'm new to JSON Objects. Now I'm trying to send data(JSON Array) to back(grid).

Here's my JSON Store:


var myStore = new Ext.data.JsonStore({
autoLoad : true,
//url : 'tabloDoldur.ajax',// eklenecek..
url : 'GetKisiler',// Servlet : DAO.KisiHelper araciligiyla veri cekiyor..
fields : myRecords,
idProperty : 'kisiId',
totalProperty : 'totalCount',
root : 'data',
sortInfo : {
field : 'kisiId',
direction : 'DESC'
}
});



Here's my servlet code for this :


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Kisi2> kisiList = null;
JSONArray arr = null;
Kisi2 k = null;
try {
Class.forName("oracle.jdbc.OracleDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","talha", "00");
Statement st = con.createStatement();
String sql = "select * from kisi";
kisiList = new ArrayList<Kisi2>();
arr = new JSONArray();
ResultSet rs = st.executeQuery(sql);
while(rs.next()) {
k = new Kisi2(); // 'Kisi2' is a basic class that just includes variables like id, kisiId, kisiAd, etc.
k.id = rs.getInt("ID");
k.kisiId = rs.getInt("KISI_ID");
k.kisiAd = rs.getString("KISI_AD");
k.kisiSoyad = rs.getString("KISI_SOYAD");
k.kisiYas = rs.getInt("KISI_YAS");
k.kisiCinsiyet = rs.getString("KISI_CINSIYET");
k.kisiMeslek = rs.getString("KISI_MESLEK");

boolean sonuc = kisiList.add(k);
if(sonuc) {
System.out.println(k.kisiAd + " kisisi listeye eklendi");
} else {
System.out.println("Kisi EKLENEMEDI!!");
}
arr.put(kisiList);
}
//arr = JSONArray.fromObject(kisiList);
response.getWriter().write(arr.toString());
rs.close();
st.close();
con.close();
} catch(Exception e) {
e.printStackTrace();
}
}

realjax
1 Mar 2010, 1:01 AM
And your question would be ...?

talha06
1 Mar 2010, 1:59 AM
And your question would be ...?
I'm asking my mistake in server side code. (Java) And I want to do this in the best way; maybe using AJAX requests.
Thanx for reply.