Flex3 connects to the database through Servlet

Source: Internet
Author: User
This article briefly introduces how Flex connects to the database through servlet (Access ). The mx: HttpService is used to transmit data to the background. The software used in this paper: the installation and use of each software in the Flex3-eclipsepluginTomcat6.0Microsoftaccess2003Eclipse3.3 is not introduced here. First, use Access

This article briefly introduces how Flex connects to the database through servlet (Access ). The mx: HttpService is used to transmit data to the background. This article developed and used software: Flex3-eclipse plugin Tomcat 6.0 Microsoft access 2003 Eclipse 3.3 each software installation and use here will not be introduced. First, use Access

This article briefly introduces how to use Flex PassServlet Connection Database(Access ). Yes The method used to transmit data to the backend.
The software used in this article:
Flex3-eclipse plugin
Tomcat 6.0
Microsoft access 2003
Eclipse 3.3
The installation and use of each software is not described here.
First, use Access to create a table named songs. It contains four fields: name, singer, lrc, and addURL. For details, refer to the access file attached to the attachment. For details about Access deployment, refer to Java ConnectionAccess Database.
Create a new mxml file: Hello. mxml. The Code is as follows:


CreationComplete = "feedRequest. send ()">
Url = "http: // localhost: 8080/flexweb/HelloWorld"
UseProxy = "false"/>
Title = "{dgPosts. selectedItem. name}" color = "#1C06F6" fontSize = "16">













Save the file and compile it. At this moment, because no local server is deployed to execute http: // localhost: 8080/flexweb/HelloWorld

So an error message will appear. It doesn't matter. We started to build the servlet.

The specific servlet code is as follows: HelloWorld. java

Package test;
Import java. io .*;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;

Import javax. servlet .*;
Import javax. servlet. http .*;

Public class HelloWorld extends HttpServlet {
Public static String dbDriver = "sun. jdbc. odbc. JdbcOdbcDriver ";
Public String connStr = "jdbc: odbc: songs ";
Public ResultSet rs = null;
Public Connection con = null;
Public Statement st = null;
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {

Response. setContentType ("text/xml; charset = UTF-8 ");
Response. setHeader ("Cache-Control", "no-cache ");
String content = "";
String name = "";
String singer = "";
String lrc = "";
String addrURL = "";
Try {
Connection condb = getConnection ();
St = condb. createStatement ();
Rs1_st.exe cuteQuery ("select * from songs ");
While (rs. next ()){
Name = rs. getString ("name ");
Singer = rs. getString ("singer ");
Lrc = rs. getString ("lrc ");
AddrURL = rs. getString ("url ");
Content + =" "+ Name +" "+ Singer +" "+
Lrc +" "+ AddrURL +" ";
}
} Catch (ClassNotFoundException e ){
E. printStackTrace ();
} Catch (SQLException e1 ){
E1.printStackTrace ();
}
Content =" "+ Content;
Content + =" ";

System. out. println (content );
Response. getWriter (). write (content );
}
Public Connection getConnection () throws ClassNotFoundException {
Try {
Class. forName (dbDriver );
System. out. println ("Connect to db successfuly! ");
Con = DriverManager. getConnection (connStr );

} Catch (SQLException e ){
Con = null;
System. err. println (e. getMessage ());
}
Return con;
}
}


For details about how to use Tomcat to build executable servlet programs, refer to: Servlet platform construction. The servlet program above ConnectionAccess DatabaseAfter the data is retrieved, an XML file is generated and then transmitted to the Flex application, which is displayed on the GUI.
:

If there is a problem with the deployment program, you can leave a message.

Attachment download: Flex3PassServletConnectionDatabase

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.