




已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
FLex调用servlet连接数据库前言Flex 最重要的部分之一就是和服务器以及数据库的通讯。Flex 提供了三个类来与服务器通讯: HTTPService,RemoteObject 以及WebService。HTTPService 类提供了使用超文本传输协议(HTTP)与服务器通讯的方式。一个Flex 应用程序可以使用GET 或者POST 请求来发送数据到一个服务器并且处理这个请求返回的 XML 或者字符串。使用HTTPService 类,你可以与PHP 页面,ColdFusion 页面,JavaServe页面( jsp),Java servlet, Ruby onRails, 以及ASP 动态网页通讯。与Java Servlet通讯由于本人是Java出身,所以这里就来讨论一下与Servlet的通讯方式。建立数据库这里选用MySql数据库,首先建立如下的数据库表写服务器端Java代码Servletview plaincopy to clipboardprint?package servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import PODO.Product; import db.ProductDao; public class GetProductServlet extends HttpServlet List result; /* * Constructor of the object. */ public GetProductServlet() super(); /* * Destruction of the servlet. */ public void destroy() super.destroy(); / Just puts destroy string in log / Put your code here /* * The doGet method of the servlet. * * This method is called when a form has its tag value method equals to get. * * param request * the request send by the client to the server * param response * the response send by the server to the client * throws ServletException * if an error occurred * throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException result = new ProductDao().getProduct(); String xmlContent = ; response.setContentType(text/xml;charset=utf-8); PrintWriter out = response.getWriter(); if (result != null) for (int i = 0; i result.size(); i+) Product p = (Product) result.get(i); xmlContent += + p.getName() + + p.getType() + + p.getPrice() + + p.getNum() + ; xmlContent += ; out.print(xmlContent); out.flush(); out.close(); /* * The doPost method of the servlet. * * This method is called when a form has its tag value method equals to * post. * * param request * the request send by the client to the server * param response * the response send by the server to the client * throws ServletException * if an error occurred * throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException this.doGet(request, response); /* * Initialization of the servlet. * * throws ServletException * if an error occurs */ public void init() throws ServletException / Put your code here package servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import PODO.Product;import db.ProductDao;public class GetProductServlet extends HttpServlet List result;/* * Constructor of the object. */public GetProductServlet() super();/* * Destruction of the servlet. */public void destroy() super.destroy(); / Just puts destroy string in log/ Put your code here/* * The doGet method of the servlet. * * This method is called when a form has its tag value method equals to get. * * param request * the request send by the client to the server * param response * the response send by the server to the client * throws ServletException * if an error occurred * throws IOException * if an error occurred */public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException result = new ProductDao().getProduct();String xmlContent = ;response.setContentType(text/xml;charset=utf-8);PrintWriter out = response.getWriter();if (result != null) for (int i = 0; i result.size(); i+) Product p = (Product) result.get(i);xmlContent += + p.getName() + + p.getType() + + p.getPrice()+ + p.getNum() + ;xmlContent += ;out.print(xmlContent);out.flush();out.close();/* * The doPost method of the servlet. * * This method is called when a form has its tag value method equals to * post. * * param request * the request send by the client to the server * param response * the response send by the server to the client * throws ServletException * if an error occurred * throws IOException * if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException this.doGet(request, response);/* * Initialization of the servlet. * * throws ServletException * if an error occurs */public void init() throws ServletException / Put your code here 数据库连接view plaincopy to clipboardprint?public class MyConnection public Connection conn = null; public MyConnection() try / 注册数据库驱动程序为MYSQL驱动 Class.forName(com.mysql.jdbc.Driver); catch (java.lang.ClassNotFoundException e) System.err.println(mydb(): + e.getMessage(); try conn = DriverManager.getConnection( jdbc:mysql:/:3306/flex, root, root); catch (SQLException ex) System.err.println(conn: + ex.getMessage(); public Connection getDbConnection() return conn; public class MyConnection public Connection conn = null;public MyConnection() try / 注册数据库驱动程序为MYSQL驱动Class.forName(com.mysql.jdbc.Driver); catch (java.lang.ClassNotFoundException e) System.err.println(mydb(): + e.getMessage();try conn = DriverManager.getConnection(jdbc:mysql:/:3306/flex,root, root); catch (SQLException ex) System.err.println(conn: + ex.getMessage();public Connection getDbConnection() return conn; DAOview plaincopy to clipboardprint?public class ProductDao Connection conn; ResultSet rs; Statement stmt; public ProductDao() conn = new MyConnection().getDbConnection(); try stmt = conn.createStatement(); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); public List getProduct() List list = new ArrayList(); try String sql = select * from product; rs = stmt.executeQuery(sql); while (rs.next() String name=rs.getString(name); String type=rs.getString(type); double price=Double.parseDouble(rs.getString(price); int num=Integer.parseInt(rs.getString(num); Product p=new Product(name,type,price,num); list.add(p); rs.close(); stmt.close(); conn.close(); catch (SQLException e) / TODO Auto-generated catch block e.printStackTrace(); finally return list; public class ProductDao Connection conn;ResultSet rs;Statement stmt;public ProductDao() conn = new MyConnection().getDbConnection();try stmt = conn.createStatement(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();public List getProduct() List list = new ArrayList();try String sql = select * from product;rs = stmt.executeQuery(sql);while (rs.next() String name=rs.getString(name);String type=rs.getString(type);double price=Double.parseDouble(rs.getString(price);int num=Integer.parseInt(rs.getString(num);Product p=new Product(name,type,price,num);list.add(p); rs.close(); stmt.close(); conn.close(); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace(); finally return list; PODOview plaincopy to clipboardprint?public class Product private String name; private String type; private double price; private int num; public Product(String name, String type, double price, int num) = name; this.type = type; this.price = price; this.num = num; public String getName() return name; public void setName(String name) = name; public String getType() return type; public void setType(String type) this.type = type; public double getPrice() return price; public void setPrice(double price) this.price = price; public int getNum() return num; public void setNum(int num) this.num = num; public class Product private String name;private String type;private double price;private int num;public Product(String name, String type, double price, int num) = name;this.type = type;this.price = price;this.num = num;public String getName() return name;public void setName(String name) = name;public String getType() return type;public void setType(String type) this.type = type;public double getPrice() return price;public void setPrice(double price) this.price = price;public int getNum() return num;public void setNum(int num) this.num = num; 部署TOMCAT这一部分略过,如果觉得手动部署比较麻烦,我们可以使用MyEclipse插件。值得注意的是web.xml文件的配置,一定
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年航空公司飞机维护员新员工岗位专业知识笔试题目及答案
- 生药学试题试卷及答案
- 高校采购合同模板(3篇)
- 高粱种子买卖合同书模板(3篇)
- 高空施工合同范本售后(3篇)
- 地坪施工与设备租赁综合合同
- 农用土地租赁与农业绿色生产模式合作框架协议
- 汽车制造企业生产线员工招聘与安全生产协议
- 民航气象专业面试题及答案
- 幼师专业考试题及答案
- 海水鱼类增殖放流记录表格、人工标志、增殖放流验收报告
- 室内高尔夫行业分析
- 微商培训的课件目录
- 《农业保险承保理赔电子化作业规范》
- 常见呼吸道传染病课件
- 《影视艺术鉴赏》课件
- 老年心脏病护理课件
- 德国国家概况
- 服装立体裁剪课件
- 整本书读写《一颗遗失的扣子》(课件)三年级下册语文统编版
- 检测室安全操作规程
评论
0/150
提交评论