WebService第三步 - 项目开发实战1
首先就是使用JDBC进行数据库数据的查询操作了。 在MyEclipse下建立一个WebService project项目,然后新建com.hxjr.util包,在包下建立连接数据库的类DBUtil.java,具体代码如下: package com.hxjr.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBUtil { private final static String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//数据库驱动 // 数据库连接字符串 192.168.88.220 private final static String URL = "jdbc:sqlserver://数据库的IP地址:1433;DatabaseName=数据库名称"; private final static String USERNAME = "sa";// 数据库用户名 private final static String PASSWORD = "root";// 数据库密码 private Connection conn = null; // 定义一个Connection对象 //--------构造方法-------------------------------- public DBUtil(){ try { Class.forName(DRIVER); // 加载数据库驱动,注册到驱动管理器 // 创建一个Connection连接 conn = DriverManager.getConnection(URL,USERNAME,PASSWORD); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } //---------获取数据库连接--------------------------- public Connection getConnection(){ if (conn != null) {// 如果连接不为空,说明创建连接成功 return conn; // 连接成功,返回一个Connection对象 } else { return null; // 如果连接失败,返回空值 } } } 然后建立com.hxjr.ben包,在包下建立一个类Container.java,具体代码如下所示。 package com.hxjr.bean; public class Container { int id; String container_no; String article_no; float g_v_no; String name_vessel; String file_number; String consignee; String shipping_company; String fyco_present; String descripion_of_goods; String type_of_declaration; float declaration_number; float country_of_origin; String cmd_input_onitor_instruction; Byte cmd_check_consignee; Byte cmd_check_specifications; Byte cmd_check_number; Byte cmd_check_weight; Byte cmd_check_classified; Byte cmd_check_GaCang; Byte cmd_check_misrepresenting; String destination_country; String customer_name; String number_of_colli; String container_contents1; String container_contents2; String container_contents3; String business_no; String ctn_no; //省略了get和set方法 } 下面就要读取数据库中的数据了。在com.hxjr.util包下新建一个GetData类,具体代码如下: package com.hxjr.util; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.hxjr.bean.Container; public class GetData { private Connection conn = null; // 数据库连接对象 private PreparedStatement pstmt = null; // 数据库连接操作 public GetData() { // 通过构造函数取得数据库的连接 DBUtil service = new DBUtil(); this.conn = service.getConnection(); // 取得数据库的连接 } public List<Container> get(String container_no) { ResultSet rs = null; // 声明ResultSet List<Container> list = new ArrayList<Container>();// 实例化List对象 // 查询SQL语句 String sql = "select * from EDI_JiJian_Webservice where container_no='" + container_no + "' order by id desc; "; try { pstmt = conn.prepareStatement(sql); // 预编译SQL语句 rs = pstmt.executeQuery(); // 执行查询语句 while (rs.next()) { Container ctn=new Container(); ctn.setId(rs.getInt("id")); ctn.setContainer_no(rs.getString("container_no")); ctn.setArticle_no(rs.getString("article_no")); ctn.setG_v_no(rs.getFloat("g_v_no")); ctn.setName_vessel(rs.getString("name_vessel")); ctn.setFile_number(rs.getString("file_number")); ctn.setConsignee(rs.getString("consignee")); ctn.setShipping_company(rs.getString("shipping_company")); ctn.setFyco_present(rs.getString("fyco_present")); ctn.setDescripion_of_goods(rs.getString("descripion_of_goods")); ctn.setType_of_declaration(rs.getString("type_of_declaration")); ctn.setDeclaration_number(rs.getFloat("declaration_number")); ctn.setCountry_of_origin(rs.getFloat("country_of_origin")); ctn.setCmd_input_onitor_instruction(rs.getString("cmd_input_onitor_instruction")); ctn.setCmd_check_consignee(rs.getByte("cmd_check_consignee")); ctn.setCmd_check_specifications(rs.getByte("cmd_check_specifications")); ctn.setCmd_check_number(rs.getByte("cmd_check_number")); ctn.setCmd_check_weight(rs.getByte("cmd_check_weight")); ctn.setCmd_check_classified(rs.getByte("cmd_check_classified")); ctn.setCmd_check_GaCang(rs.getByte("cmd_check_GaCang")); ctn.setCmd_check_misrepresenting(rs.getByte("cmd_check_misrepresenting")); ctn.setDestination_country(rs.getString("destination_country")); ctn.setCustomer_name(rs.getString("customer_name")); ctn.setNumber_of_colli(rs.getString("number_of_colli")); ctn.setContainer_contents1(rs.getString("container_contents1")); ctn.setContainer_contents2(rs.getString("container_contents2")); ctn.setContainer_contents3(rs.getString("container_contents3")); ctn.setBusiness_no(rs.getString("business_no")); ctn.setCtn_no(rs.getString("ctn_no")); list.add(ctn); } } catch (SQLException e) { e.printStackTrace(); } finally { try { rs.close(); // 关闭ResultSet对象 pstmt.close(); // 关闭PrepareStatement对象 conn.close(); // 关闭Connection对象 } catch (SQLException e) { e.printStackTrace(); } } return list; // 返回List对象 } } 这样就可以调用GetData.java类中的get()方法获取需要发布的数据了。下面使用WebService进行内容的发布。 建立com.hxjr.webservice包,在包下新建一个Service1Dao.java类,具体代码如下:
package com.hxjr.webservice; import javax.jws.WebService; @WebService public interface Service1Dao { public String GetEDI(String container_no); }实现类Service1如下: 其实这个实现类叫Service1也是他们规定的,由于他们在使用C#进行调用的时候,也会用到Service1这个类的名称,所以我就这样命名了。
package com.hxjr.webservice; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import com.hxjr.bean.Container; import com.hxjr.util.GetData; import com.hxjr.util.MyTools; public class Service1 implements Service1Dao { public String GetEDI(String container_no) { List<Container> list = new GetData().get(container_no); Document docXML = DocumentHelper.createDocument(); if (list.size() == 0) { Element element = DocumentHelper.createElement("return_query"); // 创建普通节点 docXML.setRootElement(element); element.addText("101:没有数据"); return docXML.asXML(); } Element element = DocumentHelper.createElement("inputinfo"); // 创建普通节点 docXML.setRootElement(element); // 设置根节点 // --------------为根节点添加general文档元素--------------------- for (int i = 0; i < list.size(); i++) { Container ctn = list.get(i); Element general = element.addElement("general"); Element container_contents = general .addElement("container_contents"); container_contents.addText(MyTools.isEmptyString(ctn .getContainer_contents1())); Element container = general.addElement("container"); Element container_nox = container.addElement("container_no"); container_nox.setText(MyTools.isEmptyString(ctn.getContainer_no())); Element article_no = container.addElement("article_no"); article_no.setText(MyTools.isEmptyString(ctn.getArticle_no())); Element g_v_no = container.addElement("g_v_no"); g_v_no.setText(ctn.getG_v_no() + ""); Element name_vessel = container.addElement("name_vessel"); name_vessel.setText(MyTools.isEmptyString(ctn.getName_vessel())); Element file_number = container.addElement("file_number"); file_number.setText(MyTools.isEmptyString(ctn.getFile_number())); Element consignee = container.addElement("consignee"); consignee.setText(MyTools.isEmptyString(ctn.getConsignee())); Element shipping_company = container.addElement("shipping_company"); shipping_company.setText(MyTools.isEmptyString(ctn .getShipping_company())); // --------------为根节点添加document文档元素--------------------- Element document = element.addElement("document"); Element control = document.addElement("control"); Element file_numberx = control.addElement("file_number"); file_numberx.setText(MyTools.isEmptyString(ctn.getFile_number())); Element fyco_present = control.addElement("fyco_present"); fyco_present.setText(MyTools.isEmptyString(ctn.getFyco_present())); Element descripion_of_goods = control .addElement("descripion_of_goods"); descripion_of_goods.setText(MyTools.isEmptyString(ctn .getDescripion_of_goods())); Element type_of_declaration = control .addElement("type_of_declaration"); type_of_declaration.setText(MyTools.isEmptyString(ctn .getType_of_declaration())); Element declaration_number = control .addElement("declaration_number"); declaration_number.setText(ctn.getDeclaration_number() + ""); Element country_of_origin = control.addElement("country_of_origin"); country_of_origin.setText(ctn.getCountry_of_origin() + ""); Element destination_country = control .addElement("destination_country"); destination_country.setText(MyTools.isEmptyString(ctn .getDestination_country())); Element customer_name = control.addElement("customer_name"); customer_name .setText(MyTools.isEmptyString(ctn.getCustomer_name())); Element number_of_colli = control.addElement("number_of_colli"); number_of_colli.setText(MyTools.isEmptyString(ctn .getNumber_of_colli())); } // 输出XML文档 return docXML.asXML(); } } 在发布之前,还需要讲解一下XML,由于WebService的数据都是以XML的格式发布出去的,而且必须按照对方要求的格式进行数据的组织,所以有必要先了解一下Java如何生成XML格式的数据。 在这里,我们使用了第三方的组件来生成XML文档,也就是需要在WebService项目中引入dom4j的jar包,然后就可以使用其中的API来生成我们想要的格式了。 如上的类的重点就是方法的定义完全是按照规定的要求进行了,也就是前面文档给出的标准。这样他们在使用已经开发好的C#调用时就不会存在任何问题了。 下一篇就是发布WebService了,将这个方法公布出去,供不同平台的程序进行调用。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |