Java与LCDS
俺在这里使用Adobe LiveCycle Data Services 3.1作为Flex与Java沟通的桥梁。Adobe LiveCycle Data Services 3.1 RemoteObject主要配置文件remoting-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service"
class="flex.messaging.services.RemotingService">
<adapters>
<adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
</adapters>
<default-channels>
<channel ref="my-amf"/>
</default-channels>
<destination id="echo2005T">
<properties>
<source>com.les.java3006.DocXML3WService</source>
</properties>
</destination>
</service>
注意destination id="echo2005T" 这个与Flex2001.mxml定义一致,com.les.java3006.DocXML3WService是一个Java类,其中定义方法getAlbumElementToXml_V2
DocXML3WService.java全文
package com.les.java3006;
import java.io.File;
import java.io.InputStream;
import java.sql.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class DocXML3WService {
Connection con = null;
Statement cstmt = null;
ResultSet rs = null;
public org.w3c.dom.Document getAlbumElementToXml_V2() throws SQLException{
SQLXML xmlSource=null;
org.w3c.dom.Document _w3cDocument=null;
Integer _id=null;
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName=XTJC;integratedSecurity=false;username=Flexuser;password=12345678";
String connectstring="select dbo.SelectAlbumElementsToXml() as XMLData";
try{
// 使用数据源建立SQL2008数据库的连接
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
cstmt=con.createStatement();
rs=cstmt.executeQuery(connectstring);
while (rs.next()) {
xmlSource = rs.getSQLXML("XMLData");
InputStream binaryStream = xmlSource.getBinaryStream();
DocumentBuilderFactory builderfactory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=builderfactory.newDocumentBuilder();
_w3cDocument=builder.parse(binaryStream);
}
}
catch (Exception e) {
System.out.println("代号:1001,getAlbumElementToXml_V2错误"+e);
}
finally{
Connectorclose();
}
return _w3cDocument;
}
//关闭连接和清空连接对象
private void Connectorclose(){
if (rs != null) try { rs.close(); } catch(Exception e) {}
if (cstmt != null) try { cstmt.close(); } catch(Exception e) {}
if (con != null) try { con.close(); } catch(Exception e) {}
}
}
说明一下:
1. 使用java.sql.SQLXML类,这个只在JAVA 6 受到支持,因此你的数据库驱动类库同时必须支持JDBC 4,现在MSSQL Oracle 等数据库引入一种新类型XML,其通过JDBC4,可转化为java.sql.SQLXML类;
2. 如果连接成功,得到java.sql.SQLXML类,应该转化为受Flex支持的XML数据绑定类org.w3c.dom.Document;
3. 原来使用Hibernate代码删除,改为直接使用JDBC语句连接数据库;
4. LCDS与Java代码部署在Tomcat 7,原来使用Tomcat 6 不支持Java 6的SQLjdbc4(MS SQL Server JDBC 4 库)编译代码。