Go Back   CodingForums.com > :: Client side development > Flash & ActionScript > Adobe Flex

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-22-2010, 06:53 PM   PM User | #1
Tamanna
New to the CF scene

 
Join Date: Aug 2010
Location: India
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Tamanna is an unknown quantity at this point
accessing mysql database using a jsp in adobe flex

hey guys,im new to flex.im nt being able 2 access data frm mysql database using jsp page in flex.the jsp page is workin fine as it is fetching data widout flex but wid flex..data is not getting displayed. Here's my mxml page.

l version="1.0" encoding="utf-8"?>
<mx:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" minWidth="955" minHeight="600" xmlns:srv="services.srv.*">

<fxeclarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<mx:HTTPService id="srv" url="http://localhost:8080/examples/second3.jsp" resultFormat="object"/>


</fxeclarations>

<mx:Button label="Get Data" click="srv.send()"/>
<mxataGrid dataProvider="{srv.lastResult}" x="79" y="57" id="dataGrid"
<mx:columns>

<mxataGridColumn headerText="name" dataField="name"/>
<mxataGridColumn headerText="age" dataField="age"/>


</mx:columns>
</mxataGrid>
</mx:Application>

In the dataprovider property i've given "httpserviceid.lastResult"..i dont know what lastResult is ..i jst googled and found this written evrywhere so i used it..but the data dint get fetched.So i removed the datagrid part frm the code and switched to the design mode.Then
I dragged a datagrid component,right clikd it,clikd bindData,then configure return type ,then autodetect return type,then cliked the radio button stating "enter url with parameters to get it" and typed the url.."http://localhost:8080/examples/second3.jsp" but then the following showed up..

The response is not a valid XML or a JSON string..

Now i donno where im going wrong.i searched google but evrywhere there r articles accessing database thru xml in flex..here's my jsp page.(its in examples folder of tomcat)..

<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%! Connection con=null; %>
<%! Statement st= null; %>
<%! ResultSet rs= null; %>


<html>
<head><title>This is a Jdbc Example</title></head>
<body>

<%
out.println("Arun K Chanumalla");


try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(ClassNotFoundException ce){out.println(ce);}

try{
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/proj","root", "tamgam");
st = con.createStatement();
rs = st.executeQuery("SELECT name, age FROM first");
while(rs.next()) {

String name = rs.getString(1);
int age = rs.getInt(2);
out.println(name +"<br>");
out.println(age);

} // end of whilee
rs.close();
st.close();
con.close();

}catch(SQLException exception){
out.println("<!--");
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
out.print(sw);
sw.close();
pw.close();
out.println("-->");

}



%>
</body>
</html>

pls if anybody could tell me the right way to proceed..i'd b thankful...
Tamanna is offline   Reply With Quote
Old 08-23-2010, 07:13 PM   PM User | #2
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
This is not tested, but to give you the general idea:
Code:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/29/dynamically-loading-xml-files-using-the-httpservice-tag/ -->
<mx:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx" layout="absolute" minWidth="955" minHeight="600" xmlns:srv="services.srv.*">

<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
    <mx:XMLListCollection id="xmlListColl" />
    <mx:HTTPService id="srv" url="http://localhost:8080/examples/second3.jsp"
            resultFormat="e4x"
            fault="httpService_fault(event);"
            result="httpService_result(event)" />
</fx:Declarations>

<mx:Script>
    <![CDATA[
        import mx.rpc.events.ResultEvent;
        import mx.rpc.events.FaultEvent;
        import mx.controls.Alert;

        private function httpService_fault(evt:FaultEvent):void {
            var title:String = evt.type + " (" + evt.fault.faultCode + ")";
            var text:String = evt.fault.faultString;
            Alert.show(text, title);
            xmlListColl.removeAll();
        }

        private function httpService_result(evt:ResultEvent):void {
            var xmlList:XMLList = XML(evt.result).results;
            xmlListColl = new XMLListCollection(xmlList);
        }
    ]]>
</mx:Script>

<mx:Button label="Get Data" click="srv.send()"/>
<mx:DataGrid dataProvider="{xmlListColl}" x="79" y="57" id="dataGrid"
    <mx:columns>
        <mx:DataGridColumn headerText="name" dataField="name"/>
        <mx:DataGridColumn headerText="age" dataField="age"/>
    </mx:columns>
</mx:DataGrid>
</mx:Application>
You assign your dataprovider to an object of some sort(XMLListCollection in this case) and just update the value to the object using the result event handler function. Link where the example code mostly came from is in the comment at the top.
Inigoesdr is offline   Reply With Quote
Old 08-25-2010, 05:04 AM   PM User | #3
Tamanna
New to the CF scene

 
Join Date: Aug 2010
Location: India
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Tamanna is an unknown quantity at this point
hey thnx for d reply.....bt i wanted 2 know dat isnt dere any method to access data frm a database widout involving xml ...i surfed thru d net n evrywhere they've used xml.....i tried out nd even my code got executed finally by just inserting 2 tags "<first><name><%=out.println(name);%></name><age><%=out.println(age);%></age></first>"...i built no xml file n all jst wrote these tags ..earlier d application was sayin that the return type is nt xml n after inserting dese tags its running properly...but y one has 2 insert dese tags..just to make the return type look lyk xml..dat means flex cnt retrieve normal data...if its so...den im disappointed..
Tamanna is offline   Reply With Quote
Old 08-25-2010, 06:39 AM   PM User | #4
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
You can return data in any format you please -- just set the resultFormat attribute properly on the HTTPService. For example, if you just want the raw data use "text":
Code:
<mx:HTTPService id="srv" url="http://localhost:8080/examples/second3.jsp"
            resultFormat="text"
            fault="httpService_fault(event);"
            result="httpService_result(event)" />
Inigoesdr is offline   Reply With Quote
Users who have thanked Inigoesdr for this post:
Tamanna (09-05-2010)
Old 09-05-2010, 11:12 AM   PM User | #5
Tamanna
New to the CF scene

 
Join Date: Aug 2010
Location: India
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Tamanna is an unknown quantity at this point
hey i tried to use the text resultformat and tried to get the result in a string but nothing got returned..on the jsp page i returned..."out.println("smthing");".......and on the flex page i wrote...."textarea.text=myhttpservice.lastResult;"...but nothing was returned in the textarea...
Meanwhile,i was working on a user authentication application in flex and jsp..I designed the interface in actionscript file in flex showing username and password textboxes and a button to login..when the user presses the button ..a jsp page is called which checks whether the user is authentic or not by searching a database...so if the user is genuine..it shud return sme text back to flex which i cn read and then proceed..but as i told above...no text is being returned from the jsp to flex..so i surfed thru the net and found this token thing that can b used in flex to capture the result..i know the syntax for the token for the flex file..but i dnt know how to return that token from jsp(i mean what code shud i write to return a token in jsp)...using out.println or smthing else...if anybody has any idea about that ...or apart from token.any other aproach..kindly share...
thanx.
Tamanna is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:06 PM.


Advertisement
Log in to turn off these ads.