01
Flex3通过Servlet连接数据库
本文简单介绍了Flex如何通过servlet连接数据库(Access)。使用的是<mx:HttpService>给后台传递数据的方法。
本文开发使用的软件:
Flex3-eclipse plugin
Tomcat 6.0
Microsoft access 2003
Eclipse 3.3
各个软件的安装和使用这里就不介绍了。
首先用Access建立一个名字叫做songs的table.包含了name,singer,lrc,addURL四个field.具体的可以参考附件中附带的access文件。关于Access部署参考:Java连接access数据库 .
其次新建一个mxml文件:Hello.mxml 。具体代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
- creationComplete="feedRequest.send()" >
- <mx:HTTPService id="feedRequest"
- url="http://localhost:8080/flexweb/HelloWorld"
- useProxy="false" />
- <mx:Panel x="10" y="10" width="475" height="400" layout="absolute"
- title="{dgPosts.selectedItem.name}" color="#1C06F6" fontSize="16">
- <mx:DataGrid x="20" y="20" id="dgPosts" width="400" dataProvider="{feedRequest.lastResult.songs.song}" fontFamily="Times New Roman" fontSize="16">
- <mx:columns>
- <mx:DataGridColumn headerText="Name" dataField="name"/>
- <mx:DataGridColumn headerText="Singer" dataField="singer"/>
- <mx:DataGridColumn headerText="URL" dataField="addrURL"/>
- </mx:columns>
- </mx:DataGrid>
- <mx:LinkButton x="20" y="225" label="下载音乐" click="navigateToURL(new URLRequest(dgPosts.selectedItem.addrURL));" fontFamily="Times New Roman" fontSize="20" color="#0B3C0B"/>
- <mx:TextArea x="20" y="175" width="400" text="{dgPosts.selectedItem.lrc}" fontFamily="Times New Roman" fontSize="16"/>
- </mx:Panel>
- </mx:Application>
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="feedRequest.send()" > <mx:HTTPService id="feedRequest" url="http://localhost:8080/flexweb/HelloWorld" useProxy="false" /> <mx:Panel x="10" y="10" width="475" height="400" layout="absolute" title="{dgPosts.selectedItem.name}" color="#1C06F6" fontSize="16"> <mx:DataGrid x="20" y="20" id="dgPosts" width="400" dataProvider="{feedRequest.lastResult.songs.song}" fontFamily="Times New Roman" fontSize="16"> <mx:columns> <mx:DataGridColumn headerText="Name" dataField="name"/> <mx:DataGridColumn headerText="Singer" dataField="singer"/> <mx:DataGridColumn headerText="URL" dataField="addrURL"/> </mx:columns> </mx:DataGrid> <mx:LinkButton x="20" y="225" label="下载音乐" click="navigateToURL(new URLRequest(dgPosts.selectedItem.addrURL));" fontFamily="Times New Roman" fontSize="20" color="#0B3C0B"/> <mx:TextArea x="20" y="175" width="400" text="{dgPosts.selectedItem.lrc}" fontFamily="Times New Roman" fontSize="16"/> </mx:Panel> </mx:Application>
保存文件,编译执行。此刻由于没有部署本地服务器来执行http://localhost:8080/flexweb/HelloWorld
所以会出现错误信息,不要紧,我们开始搭建servlet.
servlet的具体代码如下: 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();
- rs=st.executeQuery("select * from songs");
- while(rs.next()){
- name = rs.getString("name");
- singer = rs.getString("singer");
- lrc = rs.getString("lrc");
- addrURL = rs.getString("url");
- content+="<song><name>"+name+"</name><singer>"+singer+"</singer><lrc>"+
- lrc+"</lrc><addrURL>"+addrURL+"</addrURL></song>";
- }
- }catch(ClassNotFoundException e){
- e.printStackTrace();
- }catch( SQLException e1){
- e1.printStackTrace();
- }
- content="<?xml version=\"1.0\" encoding=\"utf-8\"?><songs>"+content;
- content+="</songs>";
- 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;
- }
- }
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(); rs=st.executeQuery("select * from songs"); while(rs.next()){ name = rs.getString("name"); singer = rs.getString("singer"); lrc = rs.getString("lrc"); addrURL = rs.getString("url"); content+="<song><name>"+name+"</name><singer>"+singer+"</singer><lrc>"+ lrc+"</lrc><addrURL>"+addrURL+"</addrURL></song>"; } }catch(ClassNotFoundException e){ e.printStackTrace(); }catch( SQLException e1){ e1.printStackTrace(); } content="<?xml version=\"1.0\" encoding=\"utf-8\"?><songs>"+content; content+="</songs>"; 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; } }关于如何利用Tomcat搭建可执行的servlet程序参考:Servlet平台搭建 这里就不介绍了。上面的servlet程序连接access数据库,取出数据后然后生成XML文件,然后在传给Flex应用程序,显示在GUI界面中。截图:
如果部署程序有问题的话,可以留言交流。
附件下载:Flex3通过Servlet连接数据库