FLex调用servlet连接数据库.docx

上传人:b****2 文档编号:24516755 上传时间:2023-05-28 格式:DOCX 页数:22 大小:41.10KB
下载 相关 举报
FLex调用servlet连接数据库.docx_第1页
第1页 / 共22页
FLex调用servlet连接数据库.docx_第2页
第2页 / 共22页
FLex调用servlet连接数据库.docx_第3页
第3页 / 共22页
FLex调用servlet连接数据库.docx_第4页
第4页 / 共22页
FLex调用servlet连接数据库.docx_第5页
第5页 / 共22页
点击查看更多>>
下载资源
资源描述

FLex调用servlet连接数据库.docx

《FLex调用servlet连接数据库.docx》由会员分享,可在线阅读,更多相关《FLex调用servlet连接数据库.docx(22页珍藏版)》请在冰豆网上搜索。

FLex调用servlet连接数据库.docx

FLex调用servlet连接数据库

FLex调用servlet连接数据库

前言

Flex最重要的部分之一就是和服务器以及数据库的通讯。

Flex提供了三个类来与服务器通讯:

HTTPService,RemoteObject以及WebService。

HTTPService类提供了使用超文本传输协议(HTTP)与服务器通讯的方式。

一个Flex应用程序可以使用GET或者POST请求来发送数据到一个服务器并且处理这个请求返回的XML或者字符串。

使用HTTPService类,你可以与PHP页面,ColdFusion页面,JavaServe页面(jsp),Javaservlet,RubyonRails,以及ASP动态网页通讯。

与JavaServlet通讯

由于本人是Java出身,所以这里就来讨论一下与Servlet的通讯方式。

建立数据库

这里选用MySql数据库,首先建立如下的数据库表

写服务器端Java代码

Servlet

viewplaincopytoclipboardprint?

packageservlet;

importjava.io.IOException;

importjava.io.PrintWriter;

importjava.util.List;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importPODO.Product;

importdb.ProductDao;

publicclassGetProductServletextendsHttpServlet{

Listresult;

/**

*Constructoroftheobject.

*/

publicGetProductServlet(){

super();

}

/**

*Destructionoftheservlet.

*/

publicvoiddestroy(){

super.destroy();//Justputs"destroy"stringinlog

//Putyourcodehere

}

/**

*ThedoGetmethodoftheservlet.

*

*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.

*

*@paramrequest

*therequestsendbytheclienttotheserver

*@paramresponse

*theresponsesendbytheservertotheclient

*@throwsServletException

*ifanerroroccurred

*@throwsIOException

*ifanerroroccurred

*/

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

result=newProductDao().getProduct();

StringxmlContent="

xmlversion='1.0'encoding='utf-8'?

>";

response.setContentType("text/xml;charset=utf-8");

PrintWriterout=response.getWriter();

if(result!

=null){

for(inti=0;i

Productp=(Product)result.get(i);

xmlContent+=""+p.getName()+""

+p.getType()+""+p.getPrice()

+""+p.getNum()+"";

}

xmlContent+="";

out.print(xmlContent);

out.flush();

out.close();

}

}

/**

*ThedoPostmethodoftheservlet.

*

*Thismethodiscalledwhenaformhasitstagvaluemethodequalsto

*post.

*

*@paramrequest

*therequestsendbytheclienttotheserver

*@paramresponse

*theresponsesendbytheservertotheclient

*@throwsServletException

*ifanerroroccurred

*@throwsIOException

*ifanerroroccurred

*/

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

this.doGet(request,response);

}

/**

*Initializationoftheservlet.

*

*@throwsServletException

*ifanerroroccurs

*/

publicvoidinit()throwsServletException{

//Putyourcodehere

}

}

packageservlet;

importjava.io.IOException;

importjava.io.PrintWriter;

importjava.util.List;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importPODO.Product;

importdb.ProductDao;

publicclassGetProductServletextendsHttpServlet{

Listresult;

/**

*Constructoroftheobject.

*/

publicGetProductServlet(){

super();

}

/**

*Destructionoftheservlet.

*/

publicvoiddestroy(){

super.destroy();//Justputs"destroy"stringinlog

//Putyourcodehere

}

/**

*ThedoGetmethodoftheservlet.

*

*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.

*

*@paramrequest

*therequestsendbytheclienttotheserver

*@paramresponse

*theresponsesendbytheservertotheclient

*@throwsServletException

*ifanerroroccurred

*@throwsIOException

*ifanerroroccurred

*/

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

result=newProductDao().getProduct();

StringxmlContent="

xmlversion='1.0'encoding='utf-8'?

>";

response.setContentType("text/xml;charset=utf-8");

PrintWriterout=response.getWriter();

if(result!

=null){

for(inti=0;i

Productp=(Product)result.get(i);

xmlContent+=""+p.getName()+""

+p.getType()+""+p.getPrice()

+""+p.getNum()+"";

}

xmlContent+="";

out.print(xmlContent);

out.flush();

out.close();

}

}

/**

*ThedoPostmethodoftheservlet.

*

*Thismethodiscalledwhenaformhasitstagvaluemethodequalsto

*post.

*

*@paramrequest

*therequestsendbytheclienttotheserver

*@paramresponse

*theresponsesendbytheservertotheclient

*@throwsServletException

*ifanerroroccurred

*@throwsIOException

*ifanerroroccurred

*/

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)

throwsServletException,IOException{

this.doGet(request,response);

}

/**

*Initializationoftheservlet.

*

*@throwsServletException

*ifanerroroccurs

*/

publicvoidinit()throwsServletException{

//Putyourcodehere

}

}

数据库连接

viewplaincopytoclipboardprint?

publicclassMyConnection{

publicConnectionconn=null;

publicMyConnection(){

try{

//注册数据库驱动程序为MYSQL驱动

Class.forName("com.mysql.jdbc.Driver");

}catch(java.lang.ClassNotFoundExceptione){

System.err.println("mydb():

"+e.getMessage());

}

try{

conn=DriverManager.getConnection(

"jdbc:

mysql:

//127.0.0.1:

3306/flex",

"root","root");

}catch(SQLExceptionex){

System.err.println("conn:

"+ex.getMessage());

}

}

publicConnectiongetDbConnection(){

returnconn;

}

}

publicclassMyConnection{

publicConnectionconn=null;

publicMyConnection(){

try{

//注册数据库驱动程序为MYSQL驱动

Class.forName("com.mysql.jdbc.Driver");

}catch(java.lang.ClassNotFoundExceptione){

System.err.println("mydb():

"+e.getMessage());

}

try{

conn=DriverManager.getConnection(

"jdbc:

mysql:

//127.0.0.1:

3306/flex",

"root","root");

}catch(SQLExceptionex){

System.err.println("conn:

"+ex.getMessage());

}

}

publicConnectiongetDbConnection(){

returnconn;

}

}

DAO

viewplaincopytoclipboardprint?

publicclassProductDao{

Connectionconn;

ResultSetrs;

Statementstmt;

publicProductDao(){

conn=newMyConnection().getDbConnection();

try{

stmt=conn.createStatement();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

publicListgetProduct(){

Listlist=newArrayList();

try{

Stringsql="select*fromproduct";

rs=stmt.executeQuery(sql);

while(rs.next()){

Stringname=rs.getString("name");

Stringtype=rs.getString("type");

doubleprice=Double.parseDouble(rs.getString("price"));

intnum=Integer.parseInt(rs.getString("num"));

Productp=newProduct(name,type,price,num);

list.add(p);

}

rs.close();

stmt.close();

conn.close();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}finally{

returnlist;

}

}

}

publicclassProductDao{

Connectionconn;

ResultSetrs;

Statementstmt;

publicProductDao(){

conn=newMyConnection().getDbConnection();

try{

stmt=conn.createStatement();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}

}

publicListgetProduct(){

Listlist=newArrayList();

try{

Stringsql="select*fromproduct";

rs=stmt.executeQuery(sql);

while(rs.next()){

Stringname=rs.getString("name");

Stringtype=rs.getString("type");

doubleprice=Double.parseDouble(rs.getString("price"));

intnum=Integer.parseInt(rs.getString("num"));

Productp=newProduct(name,type,price,num);

list.add(p);

}

rs.close();

stmt.close();

conn.close();

}catch(SQLExceptione){

//TODOAuto-generatedcatchblock

e.printStackTrace();

}finally{

returnlist;

}

}

}

PODO

viewplaincopytoclipboardprint?

publicclassProduct{

privateStringname;

privateStringtype;

privatedoubleprice;

privateintnum;

publicProduct(Stringname,Stringtype,doubleprice,intnum){

this.name=name;

this.type=type;

this.price=price;

this.num=num;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicStringgetType(){

returntype;

}

publicvoidsetType(Stringtype){

this.type=type;

}

publicdoublegetPrice(){

returnprice;

}

publicvoidsetPrice(doubleprice){

this.price=price;

}

publicintgetNum(){

returnnum;

}

publicvoidsetNum(intnum){

this.num=num;

}

}

publicclassProduct{

privateStringname;

privateStringtype;

privatedoubleprice;

privateintnum;

publicProduct(Stringname,Stringtype,doubleprice,intnum){

this.name=name;

this.type=type;

this.price=price;

this.num=num;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicStringgetType(){

returntype;

}

publicvoidsetType(Stringtype){

this.type=type;

}

publicdoublegetPrice(){

returnprice;

}

publicvoidsetPrice(doubleprice){

this.price=price;

}

publicintge

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 图表模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1