Flex and Java Database Data Reading
Some netizens asked me to write an example about the interaction between flex and the database a few days ago. I have been unable to write this article due to the tight schedule. This evening I took some time to write an example, hoping to help you, in fact, the interaction between flex and the database is very simple. I mainly read data through the interaction between Java and the database, then the interaction between flex and Java, and display the data read by Java on the front end of flex, the following code illustrates the problem.
I will not talk much about creating a flex web application.
First, you need to create a database in the database. In my example, the database name is userdb, and a table userinfo is created. set one of the two fields to ID and the other to name.
Second, create the following Java code in the src directory
User. Java
Package org. rjb. Java; Public class user { Private string name; Public String getname (){ Return name; } Public void setname (string name ){ This. Name = Name; } } |
Userdao. Java
Package org. rjb. Java; 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; Public class userdao { Public static connection getconnection (){ Connection c = NULL; Try { String driver = "com. MySQL. JDBC. Driver "; String url = "JDBC: mysql: // localhost: 3306/userdb "; String username = "root "; String Password = "1235 "; Class. forname (driver ); C = drivermanager. getconnection (URL, username, password ); } Catch (exception e ){ System. Out. println (E. getmessage ()); } Return C; } Public list getalluser () throws sqlexception { Connection c = getconnection (); Statement ST = C. createstatement (); Resultset rs1_st.exe cutequery ("select * From userinfo "); Arraylist userlist = new arraylist <user> (); While (Rs. Next ()){ User u = new user (); U. setname (Rs. getstring ("name ")); Userlist. Add (U ); } Return userlist; } }
|
The above is all the Java code, and the following is the flex code.
Package org. rjb. Flex { [Remoteclass (alias = "org. rjb. java. User")] Public class user { Private VaR _ name: string; Public Function user () { } Public Function get name (): String { Return this. _ name; } Public Function Set Name (Name: string): void { This. _ name = Name; } } }
|
Third, create the following code under flex_src:
User.
Package org. rjb. Flex { [Remoteclass (alias = "org. rjb. java. User")] Public class user { Private VaR _ name: string; Public Function user () { } Public Function get name (): String { Return this. _ name; } Public Function Set Name (Name: string): void { This. _ name = Name; } } }
|
User.
Package org. rjb. Flex { [Remoteclass (alias = "org. rjb. java. User")] Public class user { Private VaR _ name: string; Public Function user () { } Public Function get name (): String { Return this. _ name; } Public Function Set Name (Name: string): void { This. _ name = Name; } } }
|
Next is the mxml code, that is, the interface on which we actually display our data.
Flexwithjava. mxml
<? XML version = "1.0" encoding = "UTF-8"?> <Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "vertical"> <Mx: remoteobject id = "UD" Destination = "userdao" result = "onresult (event)" fault = "onfault ()"/> <Mx: DataGrid dataprovider = "{users}"> <Mx: columns> <Mx: datagridcolumn datafield = "name" headertext = "name"/> </MX: columns> </MX: DataGrid> <Mx: Label id = "info"/> <Mx: button label = "click" Click = "clickfun ()"/> <Mx: SCRIPT> <! [CDATA [ Import MX. Collections. arraycollection; Import MX. rpc. Events. resultevent; [Bindable] Public var users: arraycollection = new arraycollection (); Public Function clickfun (): void { UD. getalluser (); } Public Function onresult (Event: resultevent): void { Users = arraycollection (event. Result ); Info. Text = "get data successfully "; } Public Function onfault (): void { Info. Text = "error "; } ]> </MX: SCRIPT> </MX: Application>
|
Fourth, configure the remoting-config.xml, add the following configuration file in it:
<destination id="UserDao">
<properties>
<source>org.rjb.java.UserDao</source>
</properties>
</destination>
OK, it's that simple. Haha, run it quickly and check the effect.