---
title: "采用数据库为Flex Tree组件的提供数据-Java与LCDS"
author: "bndis"
date: "2012-07-01T09:44:07Z"
tags: []
categories: ["非技术"]
slug: "flex-tree-java-lcds"
draft: false
lastmod: "2012-07-01T09:44:07Z"
id: 1572740
url: "https://bndis.iteye.com/blog/1572740"
---

Java与LCDS 俺在这里使用Adobe LiveCycle Data Services 3.1作为Flex与Java沟通的桥梁。Adobe LiveCycle Data Services 3.1 RemoteObject主要配置文件remoting-config.xml 
　　 
　　 
　　 
　　 
　　 
　　 
　　 
　　 
　　 
　　 
　　com.les.java3006.DocXML3WService 
　　 
　　 
　　 
　　注意destination id="echo2005T" 这个与Flex2001.mxml定义一致，com.les.java3006.DocXML3WService是一个Java类，其中定义方法getAlbumElementToXml_V2 
　　DocXML3WService.java全文 
　　package com.les.java3006; 
　　importjava.io.File; 
　　import java.io.InputStream; 
　　import java.sql.*; 
　　import javax.xml.parsers.DocumentBuilder; 
　　import javax.xml.parsers.DocumentBuilderFactory; 
　　publicclass 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;userna me=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;     }     //关闭连接和清空连接对象 
　　privatevoid 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 库）编译代码。