Flex 3通过Servlet连接数据库

系统 2175 0
<!-- Feedsky FEED发布代码开始 --> <!-- FEED自动发现标记开始 --> 点击这里使用RSS订阅本Blog:<link title="RSS 2.0" type="application/rss+xml" href="http://feed.feedsky.com/softwave" rel="alternate"> <!-- FEED自动发现标记结束 --><script language="javascript"><!-- main_sub="c1s67"; more_subs=""; --> </script><script language="javascript" src="http://www.feedsky.com/jsout/publishlist_v2.js?burl=softwave&amp;out_html=true"></script><!-- Feedsky FEED发布代码结束 -->


本文转自【onlyzhangqin的CSDN Blog http://blog.csdn.net/onlyzhangqin/archive/2008/06/03/2502512.aspx】

本文简单介绍了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代码
  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < mx:Application xmlns:mx = "http://www.adobe.com/2006/mxml" layout = "absolute"
  3. creationComplete = "feedRequest.send()" >
  4. < mx:HTTPService id = "feedRequest"
  5. url = "http://localhost:8080/flexweb/HelloWorld"
  6. useProxy = "false" />
  7. < mx:Panel x = "10" y = "10" width = "475" height = "400" layout = "absolute"
  8. title = "{dgPosts.selectedItem.name}" color = "#1C06F6" fontSize = "16" >
  9. < mx:DataGrid x = "20" y = "20" id = "dgPosts" width = "400" dataProvider = "{feedRequest.lastResult.songs.song}" fontFamily = "TimesNewRoman" fontSize = "16" >
  10. < mx:columns >
  11. < mx:DataGridColumn headerText = "Name" dataField = "name" />
  12. < mx:DataGridColumn headerText = "Singer" dataField = "singer" />
  13. < mx:DataGridColumn headerText = "URL" dataField = "addrURL" />
  14. </ mx:columns >
  15. </ mx:DataGrid >
  16. < mx:LinkButton x = "20" y = "225" label = "下载音乐" click = "navigateToURL(newURLRequest(dgPosts.selectedItem.addrURL));" fontFamily = "TimesNewRoman" fontSize = "20" color = "#0B3C0B" />
  17. < mx:TextArea x = "20" y = "175" width = "400" text = "{dgPosts.selectedItem.lrc}" fontFamily = "TimesNewRoman" fontSize = "16" />
  18. </ mx:Panel >
  19. </ mx:Application >
  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < mx:Application xmlns:mx = "http://www.adobe.com/2006/mxml" layout = "absolute"
  3. creationComplete = "feedRequest.send()" >
  4. < mx:HTTPService id = "feedRequest"
  5. url = "http://localhost:8080/flexweb/HelloWorld"
  6. useProxy = "false" />
  7. < mx:Panel x = "10" y = "10" width = "475" height = "400" layout = "absolute"
  8. title = "{dgPosts.selectedItem.name}" color = "#1C06F6" fontSize = "16" >
  9. < mx:DataGrid x = "20" y = "20" id = "dgPosts" width = "400" dataProvider = "{feedRequest.lastResult.songs.song}" fontFamily = "TimesNewRoman" fontSize = "16" >
  10. < mx:columns >
  11. < mx:DataGridColumn headerText = "Name" dataField = "name" />
  12. < mx:DataGridColumn headerText = "Singer" dataField = "singer" />
  13. < mx:DataGridColumn headerText = "URL" dataField = "addrURL" />
  14. </ mx:columns >
  15. </ mx:DataGrid >
  16. < mx:LinkButton x = "20" y = "225" label = "下载音乐" click = "navigateToURL(newURLRequest(dgPosts.selectedItem.addrURL));" fontFamily = "TimesNewRoman" fontSize = "20" color = "#0B3C0B" />
  17. < mx:TextArea x = "20" y = "175" width = "400" text = "{dgPosts.selectedItem.lrc}" fontFamily = "TimesNewRoman" fontSize = "16" />
  18. </ mx:Panel >
  19. </ mx:Application >

保存文件,编译执行。此刻由于没有部署本地服务器来执行 http://localhost:8080/flexweb/HelloWorld

所以会出现错误信息,不要紧,我们开始搭建servlet.

servlet的具体代码如下: HelloWorld.java

Xml代码
  1. packagetest;
  2. importjava.io.*;
  3. importjava.sql.Connection;
  4. importjava.sql.DriverManager;
  5. importjava.sql.ResultSet;
  6. importjava.sql.SQLException;
  7. importjava.sql.Statement;
  8. importjavax.servlet.*;
  9. importjavax.servlet.http.*;
  10. publicclassHelloWorldextendsHttpServlet{
  11. publicstaticString dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver" ;
  12. publicString connStr = "jdbc:odbc:songs" ;
  13. publicResultSet rs = null ;
  14. publicConnection con = null ;
  15. publicStatement st = null ;
  16. publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
  17. throwsServletException,IOException{
  18. response.setContentType("text/xml; charset = utf -8");
  19. response.setHeader("Cache-Control","no-cache");
  20. String content = "" ;
  21. String name = "" ;
  22. String singer = "" ;
  23. String lrc = "" ;
  24. String addrURL = "" ;
  25. try{
  26. Connection condb = getConnection ();
  27. st = condb .createStatement();
  28. rs = st .executeQuery("select*fromsongs");
  29. while(rs.next()){
  30. name = rs .getString("name");
  31. singer = rs .getString("singer");
  32. lrc = rs .getString("lrc");
  33. addrURL = rs .getString("url");
  34. content+=" < song > < name > "+name+" </ name > < singer > "+singer+" </ singer > < lrc > "+
  35. lrc+" </ lrc > < addrURL > "+addrURL+" </ addrURL > </ song > ";
  36. }
  37. }catch(ClassNotFoundExceptione){
  38. e.printStackTrace();
  39. }catch(SQLExceptione1){
  40. e1.printStackTrace();
  41. }
  42. content =" <? xml version=/" 1 .0/" encoding =/"utf-8/" ?> < songs > "+content;
  43. content+=" </ songs > ";
  44. System.out.println(content);
  45. response.getWriter().write(content);
  46. }
  47. publicConnectiongetConnection()throwsClassNotFoundException{
  48. try{
  49. Class.forName(dbDriver);
  50. System.out.println("Connecttodbsuccessfuly!");
  51. con = DriverManager .getConnection(connStr);
  52. }catch(SQLExceptione){
  53. con = null ;
  54. System.err.println(e.getMessage());
  55. }
  56. returncon;
  57. }
  58. }
  1. packagetest;
  2. importjava.io.*;
  3. importjava.sql.Connection;
  4. importjava.sql.DriverManager;
  5. importjava.sql.ResultSet;
  6. importjava.sql.SQLException;
  7. importjava.sql.Statement;
  8. importjavax.servlet.*;
  9. importjavax.servlet.http.*;
  10. publicclassHelloWorldextendsHttpServlet{
  11. publicstaticString dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver" ;
  12. publicString connStr = "jdbc:odbc:songs" ;
  13. publicResultSet rs = null ;
  14. publicConnection con = null ;
  15. publicStatement st = null ;
  16. publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
  17. throwsServletException,IOException{
  18. response.setContentType("text/xml; charset = utf -8");
  19. response.setHeader("Cache-Control","no-cache");
  20. String content = "" ;
  21. String name = "" ;
  22. String singer = "" ;
  23. String lrc = "" ;
  24. String addrURL = "" ;
  25. try{
  26. Connection condb = getConnection ();
  27. st = condb .createStatement();
  28. rs = st .executeQuery("select*fromsongs");
  29. while(rs.next()){
  30. name = rs .getString("name");
  31. singer = rs .getString("singer");
  32. lrc = rs .getString("lrc");
  33. addrURL = rs .getString("url");
  34. content+=" < song > < name > "+name+" </ name > < singer > "+singer+" </ singer > < lrc > "+
  35. lrc+" </ lrc > < addrURL > "+addrURL+" </ addrURL > </ song > ";
  36. }
  37. }catch(ClassNotFoundExceptione){
  38. e.printStackTrace();
  39. }catch(SQLExceptione1){
  40. e1.printStackTrace();
  41. }
  42. content =" <? xml version=/" 1 .0/" encoding =/"utf-8/" ?> < songs > "+content;
  43. content+=" </ songs > ";
  44. < A title = system href = "http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989" target = _blank > system </ A > .out.println(content);
  45. response.getWriter().write(content);
  46. }
  47. publicConnectiongetConnection()throwsClassNotFoundException{
  48. try{
  49. Class.forName(dbDriver);
  50. < A title = system href = "http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989" target = _blank > system </ A > .out.println("Connecttodbsuccessfuly!");
  51. con = DriverManager .getConnection(connStr);
  52. }catch(SQLExceptione){
  53. con = null ;
  54. < A title = system href = "http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989" target = _blank > system </ A > .err.println(e.getMessage());
  55. }
  56. returncon;
  57. }
  58. }
关于如何利用Tomcat搭建可执行的servlet程序参考: Servlet平台搭建 这里就不介绍了。上面的servlet程序连接access数据库,取出数据后然后生成XML文件,然后在传给Flex应用程序,显示在GUI界面中。截图:

如果部署程序有问题的话,可以留言交流。

附件下载: Flex3通过Servlet连接数据库


<!-- Google Reader shared发布代码开始 --> <script type="text/javascript" src="http://www.google.com/reader/ui/publisher.js"></script><script type="text/javascript" src="http://www.google.com/reader/public/javascript/user/00697638153916680411/state/com.google/broadcast?n=5&amp;callback=GRC_p%28%7Bc%3A%22green%22%2Ct%3A%22%5Cu8FD9%5Cu4E9B%5Cu6587%5Cu7AE0%5Cu4E5F%5Cu503C%5Cu5F97%5Cu4E00%5Cu770B%22%2Cs%3A%22false%22%7D%29%3Bnew%20GRC"></script><!-- Google Reader shared发布代码结束 -->

Flex 3通过Servlet连接数据库


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论