Flex and background database connection (remoteobject)

Source: Internet
Author: User

After several days of hard work, we can finally connect flex to the back-end database. Using the remoteobject method, the configuration is indeed troublesome. To sum up:

Installation Tool: myeclipse

Flex

Blazeds. War

Mysql-connector-java-5.1.7-bin.jar

Configuration process:

1. Install the flex plug-in myeclipse (reference: http://www.blogjava.net/rainwindboys/archiv/2009/06/24/232914.html)

2. Create a web project in myeclipse

3. Introduce database-driven mysql-connector-java-5.1.7-bin.jar of Java and MySQL in web engineering

In the Java build path libraries in properties, select Add external jars to introduce the package.

4. Right-click the project in the flex view and use add web project capablity to cancel the Create web. xml check box default.

Right-click the project and add Web Support to the flex Project (role: to allow tomcat to load this project, because Tomcat can only load web projects)

5. Change the flex server in properties to the root URL of http: // localhost: 8080.

Context root is/login

Import blazeds. War (Lite version of LCDs) in deploy flex war)

6. Configure webroot-> flex-> Add to remoting-config.xml

<Destination ID = "remotelogin">
<Properties>
<Source> com. Test. login </source>
</Properties>
</Destination>

Com. Test. login is a Java file referenced in the src directory.

In the. mxml file

<Mx: remoteobject id = "firstro" Destination = "remotelogin" result = "getroresult (event)"/>

Then we can call the Java method in the trigger event (firstro) of the button through the above remoteobject component ID (firstro) instance as follows:

Private function firstro (str1: String, str2: string): void {
Firstro. checklogin (str1, str2); // use this method to pass two parameters to the Method

}

7. Summary

Remoteobject background connection method: There are two methods to handle returned events: event listening, directly return

(1) first, we will introduce direct return, which is shown in the following example:

<Mx: remoteobject id = "firstro" Destination = "remotelogin" result = "getroresult (event)"/>

Private function getroresult (E: resultevent): void {
VaR Infos: string;
Infos = E. Result. tostring (); // This is a parameter returned from the method and determined based on the parameter
If (Infos = "login "){

Alert. Show ("login"); // login Processing

} Else {

Alert. Show ("not logged on"); // login process

}

(2) Introduction to the event listening method

<Mx: remoteobject Destination = "remotelogin" id = "remotehello"> </MX: remoteobject>
<Mx: button x = "192" Y = "255" label = "button" Click = "testlogin ()"/>

Public Function testlogin (): void {
VaR lognam: String = username. text;
VaR logpassword: String = userpassword. text;
Remotehello. checklogin (lognam, logpassword );
Remotehello. addeventlistener (resultevent. Result, getrohellores); // In the button event, listen to whether the Java program on the server is completed and return the result.
}

Private function getrohellores (E: resultevent): void {// processing when the returned result is returned
// Alert. Show (E. Result. tostring ());
Show. Visible = "true ";
Show. Text = E. Result. tostring ();

}

8. Problems:

Cache: it is not a big problem. It can be run every time it is modified correctly.

Data Display problem: Alert. Show ("output string") (show cannot be less)

Or use the component output:

<Mx: Label x = "25" Y = "16" text = "" id = "show" fontsize = "15" width = "66"/>

Show. Visible = "true ";
Show. Text = E. Result. tostring ();

9. Example

// Login. mxml

<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute">
<Mx: SCRIPT>
<! [CDATA [
Import MX. rpc. Events. resultevent;
Import MX. Controls. Alert;
Import MX. Collections. arraycollection;
Private function firstro (str1: String, str2: string): void {
Firstro. checklogin (str1, str2); // use this method to pass two parameters to the Method

}
Private function getroresult (E: resultevent): void {
VaR Infos: string;
Infos = E. Result. tostring (); // This is a parameter returned from the method and determined based on the parameter
// Show. Visible = "true ";
// Show. Text = E. Result. tostring ();
// Alert. Show (Infos );
// Show. Text = Infos;
If (Infos = "login "){

Alert. Show ("login"); // login Processing

} Else {

Alert. Show ("not logged on"); // login process

}
}
]>
</MX: SCRIPT>
 
<Mx: remoteobject id = "firstro" Destination = "remotelogin" result = "getroresult (event)"/>
<Mx: canvas x = "148" Y = "157" width = "200" Height = "200" fontsize = "12">
<Mx: Label x = "25" Y = "48" text = "user"/>
<Mx: Label x = "25" Y = "86" text = "passwd"/>
<Mx: textinput x = "81" Y = "46" width = "109" id = "name1"/>
<Mx: textinput x = "81" Y = "84" width = "109" id = "passwd" displayaspassword = "true"/>
<Mx: button x = "25" Y = "142" label = "login" Click = "firstro (name1.text. tostring

(), Passwd. Text. tostring () "/>
<Mx: button x = "125" Y = "142" label = "button"/>
<Mx: Label x = "25" Y = "16" text = "" id = "show" fontsize = "15" width = "66"/>

</MX: canvas>
</MX: Application>

// Login. Java

Package com. test;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. util. arraylist;
Import java. util. List;

Public class login {

Testdao test = new testdao ();
Public String checklogin (string name, string password ){
// System. Out. println (name );
String temp = "not logged on ";
String SQL = "select * From t_user where username = '" + name + "' and

Userpassword = '"+ password + "'";
Resultset rs = test. selectsql (SQL );
Try {
If (Rs. Next ()){
Temp = "login ";

}
} Catch (sqlexception e ){
E. printstacktrace ();
}
// System. Out. println (temp );
Return temp;
}
}

// Testdao. Java

Package com. test;
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. resultset;
Import java. SQL. sqlexception;
Import java. SQL. statement;
Public class testdao {
Private Static string username = "root ";
Private Static string Password = "123456 ";
Private Static string database = "Web ";
Private statement stmt = NULL;
Private resultset rs = NULL;
Private Static connection con = NULL;
Private Static string url = "JDBC: mysql: // localhost: 3306/" + database + "?

Useunicode = true & characterencoding = utf8 ";

Static {
Try {
Class. forname ("com. MySQL. JDBC. Driver"). newinstance ();
Con = drivermanager. getconnection (URL, username, password );
} Catch (exception e ){
E. printstacktrace ();
}
}

Public resultset selectsql (string SQL ){

Try {
Stmt = con. createstatement ();
Rs1_stmt.exe cutequery (SQL );
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
Return Rs;
}
// Insert Delete update
Public void updatesql (string SQL ){

Try {
Stmt = con. createstatement ();
Stmt.exe cuteupdate (SQL );
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
Public void getclose (){
Try {
If (con! = NULL ){
Con. Close ();
}
If (RS! = NULL ){
Rs. Close ();
}
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}
}
}

// Remoteing-config.xml

<Destination ID = "remotelogin">
<Properties>
<Source> com. Test. login </source>
</Properties>
</Destination>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.