WebPrint.java
上传用户:mingda
上传日期:2017-06-20
资源大小:27691k
文件大小:23k
源码类别:

OA系统

开发平台:

Java

  1. /**
  2. 公司:北京方成科技发展有限公司
  3. 作者:梅勇
  4. 日期:2001.08.24
  5. */
  6. import java.io.*;
  7. import java.text.*;
  8. import java.util.*;
  9. import javax.servlet.*;
  10. import javax.servlet.http.*;
  11. import java.sql.*;
  12. import explato.database.*;
  13. import sun.io.ByteToCharConverter; 
  14. import org.xml.sax.* ;
  15. import org.apache.xerces.dom.TextImpl;
  16. import org.w3c.dom.Attr;
  17. import org.w3c.dom.Document;
  18. import org.w3c.dom.NamedNodeMap;
  19. import org.w3c.dom.Node;
  20. import org.w3c.dom.NodeList;
  21. import dom.DOMParserWrapper;
  22. import dom.wrappers.DOMParser;
  23. public class WebPrint extends HttpServlet {
  24. private String strXml="";
  25. private ResultSet rs = null;
  26. private ResultSet rs1 = null;
  27. private Connection con = null;
  28. private ConnectionManager cm=null;
  29. private Document document=null;
  30.     public void doPost(HttpServletRequest req,
  31.                       HttpServletResponse res)
  32.         throws IOException, ServletException
  33.     {
  34. //设置演示版,
  35.      long Time1=1026000000+10454400;
  36.      long curTime=0;
  37.      curTime=System.currentTimeMillis();
  38.      curTime=curTime/1000;
  39.     
  40.      //每天的时间为 86400
  41. if(curTime>Time1){ 
  42.      System.out.println("演示版已到期,系统将不正常!"); 
  43.      return ;
  44. }
  45. //------------------
  46.         res.setContentType("text/html;charset=UTF-8");
  47.         PrintWriter out = res.getWriter();
  48. try {
  49.        ServletContext application = getServletContext();
  50. //ConnectionManager cm= (ConnectionManager)application.getAttribute("fcConPoolManager");
  51. cm=(ConnectionManager)application.getAttribute ( "fcConPoolManager" );
  52. //demo是配置文件中逻辑数据库的名字,同时也是连接池的名字
  53. // String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; 
  54. // String sConnStr = "jdbc:odbc:ereport"; 
  55. // Class.forName(sDBDriver);
  56. // con = DriverManager.getConnection(sConnStr,"sa",""); 
  57. con = cm.getConnection( "demo" );     //或者  con = cm.getConnection( "demo", 30000 );   //30000是不能马上取到连接时的等待时间(以毫秒计)
  58. //-----------------------
  59. /*
  60. Statement stmt = con.createStatement();
  61. //rs = stmt.executeQuery("SELECT * FROM test");
  62. con.setAutoCommit(false);
  63. stmt.executeUpdate("insert into test values('aaa','adfsdfsd')");
  64. int tran1=stmt.getUpdateCount();
  65. if (tran1>0){
  66. con.commit();
  67. }else{
  68. con.rollback();
  69. }
  70. // out.println("tran1="+tran1);
  71. /*String tt;
  72. while (rs.next()) {
  73. tt = rs.getString("fstr1");
  74. out.print("<LI>" + rs.getString("fstr2") + "</LI>");
  75. out.print("<pre>" + tt + "</pre>");
  76. }
  77. rs.close();*/
  78. //----------------------
  79. } catch (Exception e){
  80. //out.println("读表发生错误");
  81. }
  82. finally {
  83. if( con != null ){
  84. cm.freeConnection( "demo", con );
  85. }
  86. }
  87. //-------------------------------------------------------
  88. //strKey为?后的值
  89. String strKey=new String(req.getQueryString());
  90. //计算前台SEND来的XML串 存入strXml中
  91. BufferedReader in = req.getReader();
  92. String line = null;
  93. strXml="";
  94. while ((line = in.readLine()) != null) {
  95.       strXml=strXml+line;
  96.     }        
  97. //parse XML串
  98.     try {
  99.         DOMParserWrapper parser =
  100.         (DOMParserWrapper)Class.forName("dom.wrappers.DOMParser").newInstance();
  101.         document = parser.parseXml(strXml);
  102. }
  103. catch (Exception e) {
  104.     }
  105. //--------------------------------------------------------
  106. //用一个CASE语句来处理各种方法
  107. if (strKey.equals("fc_insert")) out.print(fc_insert());
  108. if (strKey.equals("fc_insert1")) out.print(fc_insert1()); //新增主子表
  109. if (strKey.equals("fc_update1")) out.print(fc_update1()); //新增主子表
  110. if (strKey.equals("fc_select")) out.print(fc_select());
  111. if (strKey.equals("fc_FieldToTable")) out.print(fc_FieldToTable());
  112. if (strKey.equals("twors")) out.print(twors());//两个结果集的测试
  113. if (strKey.equals("sqltoxml")) out.print(sqltoxml());//两个结果集的测试
  114. if (strKey.equals("dsnSetSession")) 
  115. {
  116. Node root = document.getChildNodes().item(0);
  117.         String strName=root.getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
  118.         String strValue="";
  119.         try {
  120.          //当此节点值为空时
  121.          strValue=root.getChildNodes().item(1).getChildNodes().item(0).getNodeValue();
  122. }
  123. catch (Exception e) {   }
  124. HttpSession session = req.getSession(true);
  125. session.setAttribute(strName, strValue);
  126. out.print("");
  127. }
  128. if (strKey.equals("dsnGetSession")) 
  129. {
  130. Node root = document.getChildNodes().item(0);
  131.         String strName=root.getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
  132. HttpSession session = req.getSession(true);
  133. String strSR=session.getAttribute(strName).toString();;
  134. out.print(strSR);
  135. }
  136. if (strKey.equals("loadprint")) { 
  137. String strX="function%20detachPage%28%29%7B%20htmlToptmp%3DhtmlTop%3B%20htmlBottomtmp%3DhtmlBottom%3B%20var%20iStart%3D0%3B%20var%20iEnd%3D0%3B%20var%20lngEndLine%3Dt.rows.length-tabCauda%3B%20var%20colwidthXML%3DLoadUserData%28%22setcolwidth%22%2C%22ReportColWidth%22+ReportName%29%3B%20effectiveWidth%3DparseInt%28repWidth-tabLeft-tabRight%29%3B%20var%20oXML1%3Dnew%20ActiveXObject%28%22Microsoft.XMLDOM%22%29%3B%20oXML1.async%3D%22false%22%3B%20oXML1.loadXML%28%22%3Croot%3E%22%20+colwidthXML+%22%3C/root%3E%22%29%3B%20sumcol%3D0%3B%20var%20compressRate%3D1%3B%20if%28prnSet.compressWidth%3D%3D1%29%20%7B%20for%20%28var%20j%3D0%3Bj%3CMaxCols%3Bj++%29%7B%20if%20%28oXML1.documentElement.childNodes.item%28j%29.getAttribute%28%22colSelect%22%29%3D%3D%22block%22%29%7B%20sumcol%3Dsumcol+parseInt%28oXML1.documentElement.childNodes.item%28j%29.getAttribute%28%22colWidth%22%29%29%3B%20%7D%20%7D%20compressRate%3DeffectiveWidth/%28sumcol+MaxCols%29%3B%20if%28compressRate%3E%3D1%20%29%20%7B%20compressRate%3D1%3B%20%7D%20compressColWidth%28compressRate%29%3B%20%7D%20var%20hcompressRate%3D1%3B%20var%20t_outerHTMLbak%3Dt.outerHTML%20%3B%20if%28prnSet.compressHeight%3D%3D1%29%20%7B%20cur1%3D0%3B%20for%20%28var%20i%3DtabHead%3Bi%3ClngEndLine%3Bi++%29%20%7B%20cur1%3Dcur1+parseInt%28t.rows%28i%29.offsetHeight%29%3B%20%7D%20hcompressRate%3D%28repHeight-tabBottom-tabTop-PageHeadHeight-PageCaudaHeight-heightTop-heightBottom%29/%28cur1+lngEndLine-tabHead%29%3B%20if%28hcompressRate%3E%3D1%20%29%20%7B%20hcompressRate%3D1%3B%20%7D%20compressRowHeight%28hcompressRate%2CtabHead%2ClngEndLine%29%3B%20%7D%20if%20%28prnSet.compressFont%21%3D1%29%20%7B%20var%20compressFont%3D0%3B%20if%28hcompressRate%3CcompressRate%29%20%7B%20compressFont%3DhcompressRate%3B%20%7D%20else%20%7B%20compressFont%3DcompressRate%3B%20%7D%20if%28compressFont%3C1%29%7B%20t.style.fontSize%3DparseFloat%28t.style.fontSize%29*compressFont%3B%20%7D%20%7D%20setAutoturnRow%28%29%3B%20cur1%3D0%3B%20var%20sOtherPage%3D%22%22%3B%20for%20%28var%20i%3DtabHead%3Bi%3ClngEndLine%3Bi++%29%20%7B%20cur1%3Dcur1+parseInt%28t.rows%28i%29.offsetHeight%29%3B%20sOtherPage%3Dt.rows%28i%29.getAttribute%28%22otherpage%22%29%3B%20if%20%28sOtherPage%3D%3D%221%22%20%7C%7C%20cur1%3E%3DrepHeight-tabBottom-tabTop-PageHeadHeight-PageCaudaHeight-heightTop-heightBottom%7C%7C%20i%3D%3DlngEndLine-1%29%7B%20if%20%28sOtherPage%3D%3D%221%22%20%7C%7C%20cur1%3D%3DrepHeight-tabBottom-tabTop-PageHeadHeight-PageCaudaHeight-heightTop-heightBottom%29%20%7B%20cur1%3D0%3B%20%7D%20else%20%7B%20cur1%3DparseInt%28t.rows%28i%29.offsetHeight%29%3B%20%7D%20sumcol%3D0%3B%20var%20bFind%3Dfalse%3B%20iStart%3D0%3B%20iEnd%3D0%3B%20if%20%28sOtherPage%3D%3D%221%22%20%7C%7C%20i%3D%3DlngEndLine-1%29%7B%20perPageHTML%3DperPageHTML+t.rows%28i%29.outerHTML%20%3B%20%7D%20for%20%28var%20j%3D0%3Bj%3CMaxCols%3Bj++%29%7B%20if%20%28oXML1.documentElement.childNodes.item%28j%29.getAttribute%28%22colSelect%22%29%3D%3D%22block%22%29%7B%20sumcol%3Dsumcol+parseInt%28oXML1.documentElement.childNodes.item%28j%29.getAttribute%28%22colWidth%22%29%29*compressRate%3B%20%7D%20if%20%28sumcol%3E%3DeffectiveWidth%20%7C%7C%20j%3D%3DMaxCols-1%29%7B%20if%20%28sumcol%3C%3DeffectiveWidth%29%7B%20iEnd%3Dj%3B%20sumcol%3D0%3B%20%7D%20else%20%7B%20iEnd%3Dj-1%3B%20sumcol%3DparseInt%28oXML1.documentElement.childNodes.item%28j%29.getAttribute%28%22colWidth%22%29%29*compressRate%3B%20%7D%20var%20s1%3DHideCol%28strCol%2CiStart%2CiEnd%29%3B%20bFind%3Dtrue%3B%20PageDataAdd%28Pages%29%3B%20arrPage%5BPages%5D%3DhtmlTop+htmlBottom+tabstyle+s1%3B%20arrPage%5BPages%5D%3DarrPage%5BPages%5D+PageHeadHTML%3B%20arrPage%5BPages%5D%3DarrPage%5BPages%5D+perPageHTML+PageCaudaHTML+%22%3C/Table%3E%22%20+%22%3Cimg%20%3E%22%3B%20if%20%28i%3D%3DlngEndLine-1%29%7B%20if%28cur1%3ErepHeight-tabBottom-tabTop-PageHeadHeight-PageCaudaHeight-heightTop-heightBottom%29%7B%20PageDataAdd%28Pages+1%29%3B%20arrPage%5BPages+1%5D%3DhtmlTop+htmlBottom+tabstyle+s1%3B%20arrPage%5BPages+1%5D%3DarrPage%5BPages+1%5D+PageHeadHTML+PageCaudaHTML+%22%3C/Table%3E%22+%22%3Cimg%20%3E%22%3B%20biglabel.innerHTML%3Dbiglabel.innerHTML%20+%22%3Cdiv%20class%3DpreviewDiv%20%3E%u6B63%u5728%u8BA1%u7B97...%3C/DIV%3E%22%3B%20Pages%3DPages+1%3B%20%7D%20%7D%20biglabel.innerHTML%3Dbiglabel.innerHTML%20+%22%3Cdiv%20class%3DpreviewDiv%20%3E%u6B63%u5728%u8BA1%u7B97...%3C/DIV%3E%22%3B%20Pages%3DPages+1%3B%20if%20%28%28j%3D%3DMaxCols-1%20%29%20%26%26%20%28iEnd%3D%3Dj-1%29%20%29%7B%20var%20s1%3DHideCol%28strCol%2CMaxCols-1%2CMaxCols-1%29%3B%20bFind%3Dtrue%3B%20PageDataAdd%28Pages%29%3B%20arrPage%5BPages%5D%3DhtmlTop+htmlBottom+tabstyle+s1%3B%20arrPage%5BPages%5D%3DarrPage%5BPages%5D+PageHeadHTML%3B%20arrPage%5BPages%5D%3DarrPage%5BPages%5D+perPageHTML+PageCaudaHTML+%22%3C/Table%3E%22+%22%3Cimg%20%3E%22%3B%20if%20%28i%3D%3DlngEndLine-1%29%7B%20if%28cur1%3ErepHeight-tabBottom-tabTop-PageHeadHeight-PageCaudaHeight-heightTop-heightBottom%29%7B%20PageDataAdd%28Pages+1%29%3B%20arrPage%5BPages+1%5D%3DhtmlTop+htmlBottom+tabstyle+s1%20%3B%20arrPage%5BPages+1%5D%3DarrPage%5BPages+1%5D+PageHeadHTML+PageCaudaHTML+%22%3C/Table%3E%22+%22%3Cimg%20%3E%22%3B%20biglabel.innerHTML%3Dbiglabel.innerHTML%20+%22%3Cdiv%20class%3DpreviewDiv%20%3E%u6B63%u5728%u8BA1%u7B97...%3C/DIV%3E%22%3B%20Pages%3DPages+1%3B%20%7D%20%7D%20biglabel.innerHTML%3Dbiglabel.innerHTML%20+%22%3Cdiv%20class%3DpreviewDiv%20%3E%u6B63%u5728%u8BA1%u7B97...%3C/DIV%3E%22%3B%20Pages%3DPages+1%3B%20%7D%20iStart%3DiEnd+1%3B%20%7D%20%7D%20perPageHTML%3D%22%22%3B%20%7D%20if%28sOtherPage%21%3D%221%22%29%20perPageHTML%3DperPageHTML+t.rows%28i%29.outerHTML%20%3B%20%7D%20mdblRate%3D1%3B%20htmlTop%3D%22%22%3B%20htmlBottom%3D%22%22%3B%20t.outerHTML%3Dt_outerHTMLbak%3B%20t.style.display%3D%22none%22%3B%20for%28var%20i%3D0%3Bi%3CPages%3Bi++%29%7B%20arrPage%5Bi%5D%3Dfc_RepStr%28arrPage%5Bi%5D%2C%22%28P%29%22%2CPages+%22%22%29%3B%20%7D%20%7D%20%20";
  138. out.println(strX);
  139. }
  140. }
  141. //下面是自定义的方法
  142. //--------------------------------------
  143. //两个结果集的示例,第一个用连接池打开,第二个用一般方式打开
  144. private String twors() {
  145. // Node root = document.getChildNodes().item(0);
  146.  //   String sField=root.getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
  147.   //  String sTable=root.getChildNodes().item(1).getChildNodes().item(0).getNodeValue();
  148. String strSql="select flngVoucherID,fstrVoucherTypeName from Voucher" ;
  149. String sTmp="";
  150. try {
  151. Statement stmt = con.createStatement();
  152. rs = stmt.executeQuery(strSql);
  153. while (rs.next()) {
  154. String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; 
  155. String sConnStr = "jdbc:odbc:ereport"; 
  156. Class.forName(sDBDriver);
  157. Connection con1 = DriverManager.getConnection(sConnStr,"sa",""); 
  158. String sT=rs.getString(1);
  159. Statement stmt1 = con1.createStatement();
  160. rs1 = stmt1.executeQuery("select fstrAccountName from voucherdetail Where flngVoucherId="+sT);
  161. while (rs1.next()) {
  162. System.out.println("科目:"+rs1.getString(1));
  163. }
  164. }
  165. }
  166. catch (Exception e){
  167. sTmp=e.getMessage() ;
  168. }
  169. return sTmp;
  170. }
  171. //--------------------------------------
  172. private String fc_FieldToTable() {
  173. Node root = document.getChildNodes().item(0);
  174.     String sField=root.getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
  175.     String sTable=root.getChildNodes().item(1).getChildNodes().item(0).getNodeValue();
  176. String strSql="select "+sField+" From "+sTable ;
  177. String sTmp="";
  178. try {
  179. Statement stmt = con.createStatement();
  180. rs = stmt.executeQuery(strSql);
  181. }
  182. catch (Exception e){
  183. sTmp=e.getMessage() ;
  184. }
  185. return sTmp;
  186. }
  187. //--------------------------------------
  188. private String fc_select() {
  189. Node root = document.getChildNodes().item(0);
  190.     String strSql=root.getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
  191.     String strPageNo=root.getChildNodes().item(1).getChildNodes().item(0).getNodeValue();
  192. int iPageNo=Integer.parseInt(strPageNo,10);//页号
  193.     String strPageSize=root.getChildNodes().item(2).getChildNodes().item(0).getNodeValue();
  194. int iPageSize=Integer.parseInt(strPageSize,10);//页尺寸
  195. strSql=fcTrans(strSql);
  196. //System.out.println(strSql);
  197. String strX = "<root>";
  198. String sTmp = "";
  199. int iField=1;
  200. int iCount=1;
  201. int i=1;
  202. try {
  203. Statement stmt = con.createStatement();
  204. rs = stmt.executeQuery(strSql);
  205.         ResultSetMetaData rsmd=rs.getMetaData();
  206. while (rs.next()) {
  207. //每页30行
  208. if (i>(iPageNo-1)*iPageSize && i<=iPageNo*iPageSize) {
  209.         strX = strX + "<record>";
  210. iCount=rsmd.getColumnCount();
  211.         for (iField = 1 ; iField<=iCount ;iField++ ) 
  212. {
  213. //当有NULL字段名时,表示此则无需给值.用fcnull标记名区分.
  214. String colName=rsmd.getColumnName(iField);
  215. if (colName==null) {
  216. strX = strX + "<fcnull></fcnull>" ;
  217. }
  218. else {
  219. String sT=rs.getString(iField);
  220. if (sT==null) sT="";
  221.             strX = strX + "<fc>" + sT +"</fc>" ;
  222.             }
  223. }
  224.         strX = strX + "</record>";
  225.         }
  226.         i = i + 1;
  227. }
  228. i=i-1;
  229. if (i>0) {
  230. strX = strX + "<pages>"+i+"</pages>";
  231. }
  232. strX = strX + "</root>";
  233. rs.close();
  234. }
  235. catch (Exception e){
  236. sTmp=e.getMessage() ;
  237. System.out.println(sTmp);
  238. }
  239. return strX ;
  240. }
  241. //--------------------------------------
  242. private String fc_insert() {
  243. Node root = document.getChildNodes().item(0);
  244.     String strSql=root.getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
  245. strSql=fcTrans(strSql);
  246. String sTmp = "";
  247. // CallableStatement cstmt=null;
  248. try {
  249. Statement stmt = con.createStatement();
  250. // stmt.executeQuery(strSql);
  251. stmt.executeUpdate(strSql);
  252. /*
  253. cstmt=con.prepareCall("{call sp_getID(?)}");
  254. cstmt.registerOutParameter(1,java.sql.Types.INTEGER,1);
  255. cstmt.executeUpdate();
  256. sTmp="<dsnID>"+ cstmt.getInt(1)+"</dsnID>";
  257. */
  258. }
  259. catch (Exception e){
  260. sTmp=e.getMessage() ;
  261. }
  262. return sTmp ;
  263. }
  264. //--------------------------------------
  265. private String fc_insert1() {
  266. //凭证增加
  267. Node root = document.getChildNodes().item(0);
  268. String sTmp = "";
  269. CallableStatement cstmt=null;
  270. try {
  271. Statement stmt = con.createStatement();
  272. int i;
  273. //System.out.println(root.getChildNodes().getLength());
  274.     String strSql=root.getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
  275. strSql=fcTrans(strSql);
  276. stmt.executeUpdate(strSql);
  277. String sID="1"; //存主表新生成的ID
  278. try {
  279. //SQL Server 的存储过程,用于计算自动ID
  280. cstmt=con.prepareCall("{call sp_getID(?)}");
  281. cstmt.registerOutParameter(1,java.sql.Types.INTEGER,1);
  282. cstmt.executeUpdate();
  283. sID=""+cstmt.getInt(1);
  284. }
  285. catch (Exception e1){
  286. try {
  287. //计算MS Access数据库的主表ID
  288. String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; 
  289. String sConnStr = "jdbc:odbc:webprint"; 
  290. Class.forName(sDBDriver);
  291. Connection con1 = DriverManager.getConnection(sConnStr,"sa",""); 
  292. Statement stmt1 = con1.createStatement();
  293. rs1 = stmt1.executeQuery("select max(flngSendOutID) From SendOut");
  294. if (rs1.next()) {
  295. sID=rs1.getString(1);
  296. }
  297. }
  298. catch (Exception e2){
  299. }
  300. }
  301. //追加子表内容
  302. for(i=1;i<root.getChildNodes().getLength();i++) {
  303.     strSql=root.getChildNodes().item(i).getChildNodes().item(0).getNodeValue();
  304. strSql=fcTrans(strSql);
  305. strSql=strSql+sID+" )";
  306. stmt.executeUpdate(strSql);
  307. }
  308. }
  309. catch (Exception e){
  310. System.out.println(e.getMessage()) ;
  311. }
  312. return sTmp ;
  313. }
  314. //--------------------------------------
  315. private String fc_update1() {
  316. //凭证修改
  317. Node root = document.getChildNodes().item(0);
  318. String sTmp = "";
  319. String strSql="";
  320. try {
  321. Statement stmt = con.createStatement();
  322. int i;
  323. for(i=0;i<root.getChildNodes().getLength();i++) {
  324.     strSql=root.getChildNodes().item(i).getChildNodes().item(0).getNodeValue();
  325. strSql=fcTrans(strSql);
  326. stmt.executeUpdate(strSql);
  327. }
  328. }
  329. catch (Exception e){
  330. System.out.println(e.getMessage()) ;
  331. }
  332. return sTmp ;
  333. }
  334. //--------------------------------------
  335. private String SqlToField(String strSql) {
  336. //用一个SQL语句返回一个字段的第一个记录值
  337. String strR =  "";
  338. try {
  339. Statement stmt = con.createStatement();
  340. rs = stmt.executeQuery(strSql);
  341. if (rs.next()) {
  342.     strR = rs.getString(1) ;
  343. }
  344. rs.close();
  345. stmt.close();
  346. }
  347. catch (Exception e){
  348. //out.println("读表发生错误");
  349. }
  350. return strR;
  351. }
  352. //--------------------------------------
  353. //通用函数
  354. private String rTrim(String str){
  355.      String rstr = str;
  356.   if(str == null)
  357.          {
  358.     rstr = "";
  359.   }
  360.          else
  361.          {
  362.     if(str.length() == 0) rstr = "";
  363.     if(str.length() > 0)
  364.     {
  365.       for(int i = str.length() ; i  >  0 ; i--)
  366.              {
  367.      if(str.substring(i-1,i).equals(" "))  
  368.        rstr = rstr.substring(0,i-1);
  369.      else break;
  370.    }
  371.      } 
  372.        
  373.   }
  374.   return rstr;
  375. //--------------------------------------------
  376. private String  XmlOut(ResultSet oRs , String sRowEntityName,long EntFormat ) 
  377. {
  378. String sSet="";
  379. try {
  380. //oRs.first();
  381. while (oRs.next())
  382. {
  383.     sSet = sSet + RecordToXMLEntity(oRs, sRowEntityName, EntFormat) ;
  384. }
  385. }
  386. catch (Exception e)
  387. {
  388. return e.getMessage();
  389. }
  390. return sSet ;
  391. }
  392. private String  RecordToXMLEntity(ResultSet oRs , String EntityName,long EntFormat)
  393. {
  394. String sEty ="";
  395. int iField=1;
  396. int iCount=1;
  397.     try {
  398.     ResultSetMetaData rsmd=oRs.getMetaData();
  399. iCount=rsmd.getColumnCount();
  400. if (EntFormat==1) 
  401. {
  402. //属性
  403.         sEty = "<" + EntityName ;
  404.         for (iField = 1 ; iField<=iCount ;iField++ ) 
  405. {
  406.             sEty = sEty + " " + rsmd.getColumnName(iField) +"='" + oRs.getString(iField) +"'" ;
  407. }
  408.         sEty = sEty + "/>" + "n";
  409. }
  410. else
  411. {
  412. //节点
  413.     sEty = "<" + EntityName +">" +"n" ;
  414.         for (iField = 1 ; iField<=iCount ; iField++) 
  415.         {
  416.             sEty = sEty +"<" +  rsmd.getColumnName(iField) +">" + oRs.getString(iField) +"</" + rsmd.getColumnName(iField) +">";
  417.         }
  418.         sEty = sEty +"n" +"</" + EntityName +">" +"n";
  419. }
  420. }
  421. catch (Exception e)
  422. {
  423. }
  424. return sEty;
  425. }
  426. //-----------------------------------------------------
  427. private Attr[] sortAttributes(NamedNodeMap attrs) {
  428.     int len = (attrs != null) ? attrs.getLength() : 0;
  429.     Attr array[] = new Attr[len];
  430.     for ( int i = 0; i < len; i++ ) {
  431.         array[i] = (Attr)attrs.item(i);
  432.     }
  433.     for ( int i = 0; i < len - 1; i++ ) {
  434.         String name  = array[i].getNodeName();
  435.         int    index = i;
  436.         for ( int j = i + 1; j < len; j++ ) {
  437.             String curName = array[j].getNodeName();
  438.             if ( curName.compareTo(name) < 0 ) {
  439.                 name  = curName;
  440.                 index = j;
  441.             }
  442.         }
  443.         if ( index != i ) {
  444.             Attr temp    = array[i];
  445.             array[i]     = array[index];
  446.             array[index] = temp;
  447.         }
  448.     }
  449.     return(array);
  450. } // sortAttributes(NamedNodeMap):Attr[]
  451. private  String fcTrans(String sSource) 
  452. {
  453. //
  454. //XML节点中的汉字经8859_1==>UTF-8转换后用System.out.println输出正常
  455. String unicodesR ="";
  456. try {
  457. byte [] by = sSource.getBytes("8859_1"); //GB2312
  458. unicodesR = new String(by,"UTF-8");
  459. }
  460. catch (Exception e)
  461. {
  462. }
  463. //System.out.println(unicodesR);
  464. return unicodesR;
  465. ///
  466. }
  467. //--------------------------------------------
  468. //WebCode中用
  469. private  String sqltoxml() 
  470. {
  471. Node root=document.getChildNodes().item(0);
  472. String strpage=root.getChildNodes().item(0).getChildNodes().item(0).getNodeValue();
  473. String strsql=root.getChildNodes().item(1).getChildNodes().item(0).getNodeValue();
  474. strsql=fcTrans(strsql);
  475. String strsearch=root.getChildNodes().item(2).getChildNodes().item(0).getNodeValue();
  476. String strrownum=root.getChildNodes().item(3).getChildNodes().item(0).getNodeValue();
  477. String showcheckbox=root.getChildNodes().item(4).getChildNodes().item(0).getNodeValue();
  478. String blnempty=root.getChildNodes().item(5).getChildNodes().item(0).getNodeValue();
  479. //加空行
  480. showcheckbox=showcheckbox.toUpperCase();
  481. int intpage=Integer.parseInt(strpage);//开始页
  482. int intrownum=Integer.parseInt(strrownum);//页尺寸
  483. String strX="";
  484. try {
  485. //建立数据库连接
  486. Statement stmt = con.createStatement();
  487. //执行查询建立结果集
  488. //-----------------
  489. //System.out.print(strsearch);
  490. int i=1;
  491. if (strsearch.compareTo("###")!=0){
  492. rs = stmt.executeQuery(strsql);
  493. boolean bfind=false;
  494. while(rs.next()){
  495. String searchone=rs.getString(1);
  496. //out.print(searchone);
  497. if (searchone.equals(strsearch)){
  498. bfind=true;
  499. break;
  500. }
  501. i=i+1;
  502. }
  503. rs.close();
  504. if (bfind==true){
  505. if (i<=intrownum){
  506. intpage=1;
  507. }
  508. else {
  509. if (i%intrownum>0){
  510. intpage=i/intrownum+1;
  511. }
  512. else {
  513. intpage=i/intrownum;
  514. }
  515. }
  516. }
  517. //自动定位到当前选择所在值的页
  518. }
  519. //out.print("intpage="+intpage);
  520. //-----------------
  521. // System.out.print(intpage);
  522. rs = stmt.executeQuery(strsql);
  523. ResultSetMetaData rsmd=rs.getMetaData();
  524. strX="<root>";
  525. String sLabel="";
  526. int k=0;
  527. int thisrow=0;
  528. int pagenumber=0;//总页数
  529. int iFind=0; //控制在开始处插入一个空白行
  530. while (rs.next()) {
  531. if (blnempty.equals("true") && iFind==0) {
  532. //插入一个空白行
  533. strX=strX+"<tr height='16'>";
  534. for (i=1;i<=rsmd.getColumnCount();i++) {
  535.         strX = strX + "<td></td>";
  536. }
  537. strX=strX+"</tr>";
  538. iFind=1;
  539. }
  540. if (k>=(intpage-1)*intrownum && k<(intpage)*intrownum){
  541. strX = strX + "<tr>";
  542. for (i=1;i<=rsmd.getColumnCount();i++) {
  543.         sLabel=rsmd.getColumnLabel(i);
  544.         //System.out.println(rsmd.getColumnTypeName(i));
  545. String sT=rs.getString(i);
  546. if (sT==null) sT="";
  547.         strX = strX + "<td>" + sT + "</td>";
  548. }
  549. if (showcheckbox.equals("TRUE"))
  550.   strX = strX + "<td width='30'><input type='checkbox'></input></td>"+"</tr>";
  551.                 else 
  552.                     strX = strX + "</tr>";
  553. }
  554. k=k+1;
  555. }
  556. if (k%intrownum>0)
  557.      pagenumber=k/intrownum+1;
  558.         else 
  559.             pagenumber=k/intrownum;
  560. strX=strX+"<pagenumber>"+pagenumber+"</pagenumber><intpage>"+intpage+"</intpage></root>";
  561. } catch (Exception e){
  562. strX=e.getMessage();
  563. }
  564. return strX;
  565. }
  566. }