The database data reading of Flex and Java interaction

Source: Internet
Author: User
Tags mysql

A few days ago a netizen asked me to write an example about flex and database interaction, has been due to the time is not able to write, specially spent some time this evening to write an example, hoping to give you a little help, in fact, Flex and database interaction is very simple, I mainly through Java and database interactive reading data, Then Flex and Java interaction, the Java read data in the Flex front display, well, said a bunch of nonsense, the following code explains the problem.

I don't say much about the steps to build a flex Web application.

First of all, we ask you to set up a database in the database, in my example database named UserDB, set up a table UserInfo, which set two fields, one for ID, one for 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.javaPackage 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 rs=st.executequery ("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;
}

}

Related Article

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.