For information about creating server projects in which you can create database applications, see Working with Projects.
Generating a PHP database application
You can generate an application that uses PHP and a MySQL database. To generate this type of application, you must have a local PHP server and access to a target MySQL database table.
Generate PHP application code
If you have not already done so, create a Flex project that specifies PHP as its application server type.
Select Data > Create Application from Database.
From the Project list, select the PHP project to use.
From the Connection list, select a database connection profile. To create and use a new connection profile, click the New button and complete the text boxes. For more information, see Directory structure and deployment considerations for PHP.
From the Table list, select the database table to use.
From the Primary Key list, select the primary key of the database table if it is not already selected.
Click Next.
Specify a Main Source Folder and a Main Application File, or accept the default values. You can generate the PHP files in the bin-debug folder, which is the default location, or in the src folder. If you want to export the project after using the data wizard, you must generate the PHP files in the src folder; otherwise, the project is exported without the PHP files and does not function.
Click Next.
Deselect any database columns that you do not want to appear in the DataGrid that the wizard generates.
Choose a database filter column, or accept the default value.
Click Finish.
Select Run > External Tools > Run As > Run on Server.
Select Run > Run to run the application on the server.
Directory structure and deployment considerations for PHP
When you use the data wizard to generate a PHP application, the default location of the generated files is the [web_server_root]\[project_name]_debug directory. The directory includes a PEAR subdirectory that contains the PEAR XML parsing utility, and a history subdirectory that contains files for Flex history management.
The main deployment directory contains tablename.php, databasenameconn.php, functionsinc.php, and XmlSerializer.class.php PHP files. It also contains a SWF file named tablename.swf and an HTML wrapper named tablename.html.
The project src directory contains the following files: tablename.mxml, tablenameconfig.as, and tablenamescript.as.
Deploy an application to a remote server
Set up the host:
Create a new database. Typically, the host has a naming convention like sitename_dbname.
Create a web user with a password (for example, name: bigryans_webuser, pw: webuser). Give the web user the following privileges: insert, update, delete, and select.
Create the database table or tables (you can export the SQL and run it in your MySQL administrator application, or you can create it manually); for example: CREATE TABLE users (
userid int(10) unsigned NOT NULL auto_increment,
username varchar(255) collate latin1_general_ci NOT NULL,
emailaddress varchar(255) collate latin1_general_ci NOT NULL,
PRIMARY KEY (userid)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
Edit the Flexphpconn.php file. This file creates the connection to the database for your Flex application users:$hostname_conn = "localhost"; // If your database and Flex app are on the same server $database_conn = "bigryans_flexphp"; // name you gave it in step 1 $username_conn = "bigryans_webuser"; // name you created in step 1 $password_conn = "webuser"; // password you gave it in step 1
Upload the files. You need the following subset of the files that the wizard outputs:
/history directory (if you want to support history management/deep linking)
/PEAR directory
AC_OETags.js
Flexphpconn.php
functions.inc.php
playerProdInstall.swf (if you want to support express installation)
Flex application files:
users.html (HTML wrapper)
users.swf (Flex application)
users.php (PHP functions for the Flex application)
XMLSerializer.class.php
You do not need to upload the xxx-profile.swf files, the projectname.swf file, or the projectname.html file.
Generating a J2EE database application
You can generate an application that uses Java and a SQL database. To generate this type of application, you must have a local Java application server and access to a target SQL database table. You can generate an application that uses a Java servlet to access a database and return data to the Flex application; or if you have LiveCycle Data Services ES, you can choose to generate an application that uses the data management service to access a database and dynamically distribute data to and from clients.
Generate J2EE application code
If you have not already done so, create a new Flex project that specifies Java 2 Enterprise Edition (J2EE) as the application server type.
Select Data > Create Application from Database.
From the Project list, select the J2EE project to use.
From the Connection list, select a database connection profile. To create and use a new connection profile, click the New button and complete the text boxes.
From the Table list, select the database table to use.
From the Primary Key list, select the primary key of the database table.
Click Next.
To use a specific Java package for the Java classes that will be generated, enter the package name in the Java Package text box.
By default, the names of the generated Java classes are prefixed with the name of the selected database table. To use a different prefix, enter it in the Class Name Prefix text box.
If your project uses LiveCycle Data Services ES, select whether to use LiveCycle Data Services ES or XML over HTTP for communication between the client and the server. If you select LiveCycle Data Services ES, the Data Management Service is used. If you select XML over HTTP, a servlet passes XML between the client and the server.
Click Next.
(Optional) Deselect any of the listed table fields to exclude them from the generated Flex DataGrid control.
Click Finish.
Select Run > External Tools > Run As > Run on Server.
Select Run > Run to run the application on the server.
Directory structure and deployment considerations for J2EE
The Flex Builder project's WebContent directory contains directories to be deployed into the root directory of the web application. Depending on the type of Java application server, the web application is deployed as a WAR file or, as is the case with Tomcat, as a linked web application directory. The Flex Builder project's src directory contains Java source files for classes that are deployed to the WEB-INF/classes directory of the web application. For projects that are compiled with Flex Builder, the contents of the Flex Builder project's bin and flex_src directories are included in the list of files that are deployed to the web application; however, the compiled SWF files are deployed rather than the corresponding MXML files that are in the flex_src directory. All of the files in those folders are deployed to the root of the web application.
Generating an ASP .NET database application
You can generate an application that uses ASP .NET and a Microsoft SQL Server database. To generate this type of application, you must have a local .NET installation and access to a target Microsoft SQL Server database table. You can generate an application that uses an ASP .NET page to access a database and return data to the Flex application.
Generate ASP .NET application code
If you have not already done so, create a new Flex project that specifies ASP .NET as the application server type. You can deploy the application to the Visual Studio Development Server or the Internet Information Server (IIS) web server that comes with Windows. If you use IIS, the deployment directory specified in the New Flex Project wizard must be defined in the IIS administration console as an ASP Application.
Select Data > Create Application from Database.
From the Project list, select the ASP .NET project to use.
From the Connection list, select a database connection profile. To create and use a new connection profile, click the New button and complete the text boxes.
From the Table list, select the database table to use.
From the Primary Key list, select the primary key of the database table.
Click Next.
Specify a .NET class name, or accept the default value.
Choose either C# with Web Services or VB with Web Services as the server language.
Click Next.
(Optional) Deselect any of the listed table fields to exclude them from the generated Flex DataGrid control.
Click Finish.
Select Run > External Tools > Run As > Run on Server.
Select Run > Run to run the application on the server.
Directory structure and deployment considerations for ASP .NET
When you run Project > Export Release Build, files are saved in the project bin_release directory and the optimized application runs from that location.
Creating a database connection profile
To generate a database application, you must have a database connection profile that provides information about the database that you want to use.
Create a connection profile
If the Create Application from Database dialog box is not already open, select Data > Create Application from Database.
Click the New button to the right of the Connection text box.
In the Create Connection Profile dialog box, enter a connection name in the Name text box. You can also complete the optional Description text box.
(Optional) When the wizard is finished or when the Data Source Explorer is opened, select the Autoconnect option to connect to the database.
Click New.
Complete the database connection text boxes. The fields vary depending on the type of database connection you choose. For PHP projects, you use MySQL. For ASP.NET projects, you use Microsoft SQL Server. For J2EE projects, you can select from a list of choices, but MySQL and Microsoft SQL Server are supported.
Click Test Connection to make sure that the connection is valid.
Click Next.
Click Finish.
This is a place for learning new things. "The greatest pleasure in life is doing what people say you cannot do"
Wednesday, August 26, 2009
Building database-driven Flex applications without writing (Client- or server-side) code
One question that developers often ask when they see Flex for the first time is: “how do I write a database application?”
The answer to this question is typically: You use an HTTPService, WebService, or RemoteObject to connect to a server-side component that provides an API to manipulate your data. This approach provides infinite possibilities, but it makes the assumption that you already have that entire backend infrastructure in place. If you don’t, and all you want to do is build a rapid prototype or a simple application, this might look like a lot of work.
In addition to the RPC services mentioned above, the Flex Data Management Services (FDMS) provide a virtually code-free approach to synchronize data between the client application and the middle-tier. That takes care of 50% of our challenge: no data synchronization code to write at the client-side.
FDMS also provides a pluggable adapter and assembler architecture to handle changes at the server-side. As an example of writing a generic assembler, I built a simple JDBC assembler that allows you to develop rapid prototypes and basic database-driven applications without writing a single line of server-side code. All you need to do is specify the database connection information and the table you want to work with in data-management-config.xml.
This assembler is by no means a full featured solution, but it shows the power and flexibility of the FDMS architecture: you could easily extend its capabilities, or build other generic assemblers for existing persistence solutions (a Hibernate assembler is already shipping with the product).
The point is that if FDMS provides a solid architecture to handle the most complex persistence scenarios (with virtually no data synchronization code at the client side), with the addition of specialized assemblers, it can also provide simple solutions to simple use cases, significantly improve your productivity, and deliver end-to-end code-free persistence.
Installing the SimpleJDBCAssembler:
To use the SimpleJDBCAssembler, you need to install the Flex Data Services. If you haven’t already done so, you can download the Flex Data Services here, and follow the installation instructions.
Download jdbcassembler.zip here and expand it in your root directory (the files will be unzipped in a directory called jdbcassembler)
Copy SimpleJDBCAssembler.class in {context-root}\WEB-INF\classes\flex\samples\assemblers. For example, if you installed the integrated server, copy SimpleJDBCAssembler.class in \fds\jrun4\servers\default\samples\WEB-INF\classes\flex\samples\assemblers.
Running the sample application:
Copy the \jdbcdemo directory in the context root of your web application. For example, if you installed the integrated server, copy the \jdbcdemo directory in \fds\jrun4\servers\default\samples.
Open {context-root}\WEB-INF\flex\data-management-config.xml and add the following destination: flex.samples.assemblers.SimpleJDBCAssembler
application
org.hsqldb.jdbcDriver
jdbc:hsqldb:/jdbcassembler/db/flexdemo
product
true
This example uses an embedded HSQLDB database. Make sure hsqldb.jar is available in the classpath of your web application. For example in {context-root}\WEB-INF\lib. If you are using another database, make sure the JDBC driver for that database is in the classpath of your web application.
Start the server
Access http://localhost:8700/samples/jdbcdemo/simpleclient.mxml. (Change the port number and the context root as appropriate). This is a basic client built with just a few lines of code. You can edit the data inside the datagrid.
Access http://localhost:8700/samples/jdbcdemo/app.mxml. (Change the port number and the context root as appropriate). This is a slightly more sophisticated client. You edit data in a separate form.
Also notice that changes are pushed in real time to all the clients connected to a destination. For example, open the application in two browser sessions, change some data in one session, and notice that the changes are automatically pushed to the second session.
How to access database from flex 2
We often hear people asking about how to config datasource in flex to connect to a database. It is a miss conception to think that you need to config a datasource in flex and can use that datasource to connect to a database. Then how does flex app access data from a database? Flex does not connect to DB directly, but thru Java or other means. The doc has all the details regarding this topic, and has a sample of using PHP. But it may not be clear for people who are not familiar with J2EE to connect using java. Here is the steps to create access to database using Java.
Basically, there are three steps you need to accomplish in order to communicating with database from flex.1. Write java code to communicate to the database. We usually call this an Assembler. In here, you provide the information about what database and jdbc driver you are connecting to, and create a connection to it.
2. Then config a destination which point to the Assembler in data-management.xml. Any flex app uses this destination will use this Assembler, and connecting to the same database.
3. In your flex code, you reference the data service like this:dsEmployee = new DataService("crm.company"); here crm.company is a destination defined in step 2.Now, Let us look at the sample code included in the samples.war to see how it is done. Let us look at the crm sample in dataservice.
1. Open dataservice\crm\companyapp.mxml, In there you can see the following code:
dsCompany = new DataService("crm.company");// if hibernate is used, change the destination of the data service// dsCompany = new DataService("crm.company.hibernate");dsCompany.addEventListener(ResultEvent.RESULT, companyResultHandler);dsCompany.addEventListener(FaultEvent.FAULT, faultHandler);dsCompany.addEventListener(DataConflictEvent.CONFLICT, conflictHandler);dsCompany.autoCommit = false;
Here we are using a dataService named "crm.company" for our app.
2. Open WEF-INF\flex\data-management.xml, we can see crm.company is defined as:
This destination point to java class amples.crm.CompanyAssembler. This is our Assembler.
3. Let us see how the Assembler is constructed. --- go to WEB-INF\src\samples\crm\CompanyAssembler.java, we can see :CompanyDAO dao = new CompanyDAO(); //using CompanyDAO class--- go to CompanyDAO.java, we can see c = ConnectionHelper.getConnection(); //using ConnectionHelper class--- go to ConnectionHelper.java, we can see the code to connect to database:
private ConnectionHelper(){try{Class.forName("org.hsqldb.jdbcDriver");// Obtain a path to WEB-INF/classes/samples/crmString str = URLDecoder.decode(getClass().getClassLoader().getResource("samples/crm").toString(),"UTF-8");// Create HSQLDB JDBC URL pointing to WEB-INF/db/crm/crm (where the last crm is the datanase name)url = "jdbc:hsqldb:" + str.substring(0, str.indexOf("classes/samples/crm")) + "db/crm/crm"; }catch (Exception e){e.printStackTrace();}}This is the core of the Assembler. This is where you tell flex which database to connect.
The most important information you need to provide are the driver name and the URL. Each database has different driver name and URL, you have to make sure you are using the correct name and format. Here is a list of driver name and example of URL:
#MySqldriver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/test
#Oracle#driver=oracle.jdbc.OracleDriver#url=jdbc:oracle:thin:@test:1521
#Sybase Enterprise#driver=com.sybase.jdbc3.jdbc.SybDriver#url=jdbc:sybase:Tds:localhost:2048/test
#Sybase Anywhere#driver=com.sybase.jdbc3.jdbc.SybDriver#url=jdbc:sybase:Tds:localhost:2638/test
#Informix#driver=com.informix.jdbc.IfxDriver #url=jdbc:informix-qli://localhost:50000/test:informixserver=testserver
#MS SQL#driver=com.microsoft.jdbc.sqlserver.SQLServerDriver#url please see http://msdn2.microsoft.com/en-us/library/ms378428.aspx
Subscribe to:
Posts (Atom)