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;
}
}