Home > Database > Mysql Tutorial > Flex3通过Servlet连接数据库

Flex3通过Servlet连接数据库

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 15:11:08
Original
1491 people have browsed it

本文简单介绍了Flex如何 通过 servlet 连接 数据库 (Access)。使用的是mx:HttpService给后台传递数据的方法。 本文开发使用的软件: Flex3-eclipse plugin Tomcat 6.0 Microsoft access 2003 Eclipse 3.3 各个软件的安装和使用这里就不介绍了。 首先用Access

本文简单介绍了Flex如何通过servlet连接数据库(Access)。使用的是给后台传递数据的方法。
本文开发使用的软件:
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。具体代码如下:


creationComplete="feedRequest.send()" >
url="http://localhost:8080/flexweb/HelloWorld"
useProxy="false" />
title="{dgPosts.selectedItem.name}" color="#1C06F6" fontSize="16">













保存文件,编译执行。此刻由于没有部署本地服务器来执行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+=""+name+""+singer+""+
lrc+"
"+addrURL+"
";
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch( SQLException e1){
e1.printStackTrace();
}
content=""+content;
content+="
";

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连接数据库 


Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template