點擊這里使用RSS訂閱本Blog:

亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Flex 3通過Servlet連接數據庫

系統 2497 0
<!-- Feedsky FEED發布代碼開始 --> <!-- FEED自動發現標記開始 --> 點擊這里使用RSS訂閱本Blog:<link title="RSS 2.0" type="application/rss+xml" 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條評論
主站蜘蛛池模板: 中文不卡视频 | 91在线播放国产 | 免费看曰批女人爽的视频网址 | 欧美精品一区二区三区在线 | 久久久久久a亚洲欧洲aⅴ | 男人的天堂视频 | 天天碰夜夜操 | 亚洲aa在线| 国产成人综合亚洲欧洲色就色 | 奇米久久 | 久久精品国产波多野结衣 | 欧美在线色| 一级片影院 | 99在线免费观看视频 | 国产成人啪一区二区 | 日本成本人在线观看免费视频 | 亚洲成人看片 | 欧美日韩一区二区三在线 | 欧美成人三级视频 | 色在线视频 | 国产欧美一区二区精品仙草咪 | 夜夜夜爽爽爽久久久 | 国产成人丝袜网站在线看 | 欧美精品www | 91在线操| 久操视频在线免费观看 | 精品毛片 | 亚洲精品成人一区 | 国产福利视精品永久免费 | 五月婷婷狠狠 | 日本一级爰免费视频 | 久99re视频9在线观看 | 天然素人在线观看 | 国产三级久久久精品三级 | 欧美一级毛片国产一级毛片 | 中文国产成人久久精品小说 | 日本最猛黑人xxxx猛交 | 日韩毛片在线免费观看 | 国产二区精品视频 | 国产成人精品久久亚洲高清不卡 | 视色视频 |