Browse DevX
Sign up for e-mail newsletters from DevX


Connect Flash to a Database Using Sockets : Page 3

Making a database connection in Flash is not the straightforward process enjoyed by Java and .NET developers. And while it may be a black art, it needn't be avoided, for the XMLSocket class is there to help.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The most significant challenge of this model is the need for the client application to know the metadata (the structure) of the information returned from the database by the server application. However, the typical scenario would involve a Flash application that only submits a limited number of queries to the databases and gets a limited number of responses, therefore keeping the ParseReturn function of a manageable size and complexity. In fact, some of the other database connectivity methods mentioned in the beginning of this article do require the programmer to define the XML schema of the messages to be exchanged, so there is no way to avoid describing the data to be received.

The communication volume is another limiting factor: a robust server application that needs to answer a lot of client calls may need to use multithreading, connection pooling, and other advanced techniques.

The security of the communications between the Flash application and the Java application is very important: my example sends unencrypted data that can be 'sniffed'. A production-level application will need to encrypt data before sending it via sockets.

Setting up the Environment
You can install the Java application to run on the same machine as the database server or on a different one. To start the Java application, you will have to execute this command in DOS (see the launch.bat file in the source code download that accompanies this article):

set CLASSPATH=.;%CLASSPATH%;C:/Program Files/Microsoft SQL Server 2000 Driver for
JDBC/lib/mssqlserver.jar;C:/Program Files/Microsoft SQL Server 2000 Driver for JDBC
/lib/msutil.jar;C:/Program Files/Microsoft SQL Server 2000 Driver for JDBC/lib/msbase.jar;

java oSckSvr

The underlined portion of this command is the path to the JDBC driver for MS SQL Server.

For those developers with less Java expertise, setting up the development environment can be a bit of a hassle. I used the directory structure shown in Figure 2. I set up my Eclipse environment to copy the Java binaries to the \bin subdirectory, as shown in Figure 3. Figures 4 and 5 demonstrate the application making a connection to the database and receiving a reply.

Figure 2. Java Application Directory Structure: The screen shot shows the author's directory structure for the source.
Figure 3. Java Application Directory Structure: The screen shot shows the author's directory structure for the binaries and launcher in Eclipse.

Author's Note: You can download the JDBC driver for SQL Server from Microsoft's web site (see "Related Resources," left column).

Figure 4. Connecting to the Database: Here a connection is being made to SQL Server.
Figure 5. Query and Response: SQL Server receives the query from Figure 4 and sends its response.

In order to allow the Flash client to download the response from the Java application if the two do not reside in the same domain, a cross-domain XML policy file needs to exist on the web server. For more details, see Flash help (Actionscript reference guide > Working with external data > Flash player security features > About allowing cross-data loading).

In conclusion, although typical communication with a database is not as straightforward in Flash as it is in other development environments, it can be implemented—with the help of a mediating application written in an environment that is capable of accessing databases using a protocol such as JDBC or ADO.

Razvan Petrescu has eight years experience in technology as a systems analyst, architect, developer, and database administrator. He is currently working for a large healthcare company in the Midwest.
Thanks for your registration, follow us on our social networks to keep up-to-date