代理发布那一段我就不贴出来了 ,这天气冷得要命,需要源码的可以问我要。
当然仅仅只做代理服务器,步骤跟我上次做的案例一样这边只贴客户端出来。需求就是根据数据生成如下图所示的xml文件
需求如下:
当然有人多在问servcemix用的什么做服务器,我想仔细一点的朋友应该就会知道,贴上图大家看看就明白:
看到了么 其实apache很多组件都是用的jetty做的服务器,比如cxf等等 ,这里就不多说了。
贴上代码 :
BaseDao.java
package org.xinyu.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.servlet.jsp.jstl.sql.Result; import javax.servlet.jsp.jstl.sql.ResultSupport; import javax.sql.DataSource; /** * 数据库连类BaseDao * @author 漆艾林 * @Date 2010年11月16日 01:55:01 */ public class BaseDao { //数据库连接对象connection public Connection conn = null; private PreparedStatement pstmt = null; //结果集 private ResultSet rs = null; //打开连接 public Connection openConn() throws NamingException, SQLException { Context ctx = new InitialContext(); DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/XmlDemo"); conn = ds.getConnection(); return conn; } //关闭连接 public void close() { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); } catch (Exception ex) { ex.printStackTrace(); } } //执行查询的方法 public ResultSet executeQuery(String sql, Object... params) { try { openConn(); pstmt = conn.prepareStatement(sql); for (int i = 0; i < params.length; i++) { pstmt.setObject(i + 1, params[i]); } rs = pstmt.executeQuery(); return rs; } catch (Exception ex) { ex.printStackTrace(); } finally { } return null; } //执行更新的方法 public int executeUpdate(String sql, Object... params) { try { openConn(); pstmt = conn.prepareStatement(sql); for (int i = 0; i < params.length; i++) { pstmt.setObject(i + 1, params[i]); } return pstmt.executeUpdate(); } catch (Exception ex) { ex.printStackTrace(); } finally { close(); } return -1; } }
XmlDao.java
package org.xinyu.dao; import java.sql.SQLException; public interface XmlDao { public void getXmlByData(String tableName) throws SQLException; }
XmlDaoImpl.java
package org.xinyu.dao.impl; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import javax.naming.NamingException; import org.jdom.Document; import org.jdom.Element; import org.jdom.output.XMLOutputter; import org.xinyu.dao.BaseDao; import org.xinyu.dao.XmlDao; /** * Xml操作实现类 * @author Administrator * */ public class XmlDaoImpl extends BaseDao implements XmlDao { public void getXmlByData(String tableName) throws SQLException { String sql = "select * from " + tableName; // 定义查询的SQL语句 ResultSet rs = super.executeQuery(sql); Document document = new Document(new Element("文档列表"));// 创建文档 ResultSetMetaData rsmd = rs.getMetaData(); // 获取字段名 int numberOfColumns = rsmd.getColumnCount(); // 获取字段数 DatabaseMetaData dmd = null;//获取数据字段 try { //得到这个对象 dmd = super.openConn().getMetaData(); } catch (NamingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // 获取主键rss 并进行遍历,得到主键的rss数据集 ResultSet rss = dmd.getPrimaryKeys(null, null, tableName); //定义一个字符串类型的数组 String[] resultArray = null; //主键的字段的变量 String tempPK = ""; while (rss.next()) { //根据API,ResultSet 会包含五列,分别是: TABLE_CAT 表目录一般会是NULL // COLUMN_NAME 主键名称 tempPK = rss.getString("COLUMN_NAME") + ","; } resultArray = tempPK.split(","); int i = 0; while (rs.next()) { // 将查询结果取出 Element element0 = new Element(tableName); // 创建元素 生成JDOM树 document.getRootElement().addContent(element0); for (i = 1; i <= numberOfColumns; i++) { String data = null; data = new String(rs.getString(i)); String pkNameString=rsmd.getColumnName(i); if(pkNameString.equals(resultArray[0])) { //根据需求给主键PK修饰 Element element=new Element("PK"); Element element2 = new Element(rsmd.getColumnName(i)).setText(data); element.addContent(element2); element0.addContent(element); }else { Element element = new Element(rsmd.getColumnName(i)).setText(data); element0.addContent(element); } } } super.close(); XMLOutputter outp = new XMLOutputter(); try { outp.output(document, new FileOutputStream("d://data.xml")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 输出XML文档 } }
Xml.java
package org.xinyu.web; //Generated by MyEclipse public interface Xml { public void getXmlByData(String tableName); }
XmlService.java
package org.xinyu.web; import java.sql.SQLException; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import org.xinyu.dao.XmlDao; import org.xinyu.dao.impl.XmlDaoImpl; @WebService(name="XmlService",serviceName="XmlService ",targetNamespace="xmldemo") public class XmlService implements Xml { XmlDao xmlDao=new XmlDaoImpl(); @WebMethod @WebResult public void getXmlByData(@WebParam String tableName) { try { xmlDao.getXmlByData(tableName); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
servlet
package org.xinyu.servlet; import java.io.IOException; import java.net.URL; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.codehaus.xfire.client.Client; public class BuildXml extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String tableName=request.getParameter("tableName"); // 客户端操作对象 Client client = null; try { client = new Client(new URL("http://localhost:8192/XmlService?wsdl")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } String message = ""; try { // 采用Client的invoke方法需要两个参数,第一个是方法的名称,第二个数组类型的参数,得到boolean类型的值 client.invoke("getXmlByData", new Object[] { tableName }); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } request.getRequestDispatcher("loginError.jsp").forward(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }