本文介绍axis2在tomcat下的搭建及java连接mysql注意问题,及利用eclipse插件将java class打包成arr文件操作及注意事项等
一、搭建
工具:mysql,jdk,tomcat,mysql-connector-java-5.1.7-bin.jar(jdbc),eclipse插件:axis2-eclipse-codegen-wizard.zip&axis2-eclipse-service-archiver-wizard.zip,axis2-1.5.6-war.zip,eclipse。
假定mysql,jdk,tomcat,eclipse都已正确安装。
1、安装eclipse插件,将axis2-eclipse-codegen-wizard.zip&axis2-eclipse-service-archiver-wizard.zip解压到eclipse的\plugins目录下,重启eclipse,点击file-new-other当出现下图时表示安装成功:
2、将axis2-1.5.6-war.zip中的axis2.war解压到\Tomcat\webapps下重启Tomcat,tomcat将自动解压axis2.war到\webapps目录下,会出现axis2文件夹,\axis2\WEB-INF目录下,lib文件夹用于存放axis2框架中要用到的jar文件,因此项目所要用到的jar文件都放在lib文件夹中,services文件夹存放提供服务的aar文件,classes文件夹存放已编译的java类文件,浏览器中键入http://localhost:8080/axis2/,如出现下图表示axis2框架成功搭建,
点击validate进行确认,出现下面的蓝色文字则正常
二、编写调试
java连接mysql数据库要用到mysql-connector-java-5.1.7-bin.jar jdbc类库:
View Code
1 package cn.xiezhengcai.exing;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.ResultSet;
6 import java.sql.ResultSetMetaData;
7 import java.sql.SQLException;
8 import java.sql.Statement;
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.Map;
13
14 public class DataBaseOperate{
15 private String url;
16 private String user ;
17 private String password;
18 private Connection conn;
19 public DataBaseOperate(){
20 url="jdbc:MySQL://127.0.0.1:3306/exing?user=root&password=";
21 //user = "root";22 //password="";23 }
24 /**25 * 功能:连接数据库;26 *@authorZhengCai Xie27 *@return28 *@throwsSQLException29 *@throwsClassNotFoundException30 *@throwsIllegalAccessException31 *@throwsInstantiationException32 *33 */
34 public boolean operateStart() throws SQLException{
35
36 try {
37 Class.forName("com.mysql.jdbc.Driver").newInstance();
38 } catch (InstantiationException e) {
39 //TODO Auto-generated catch block40 e.printStackTrace();
41 } catch (IllegalAccessException e) {
42 //TODO Auto-generated catch block43 e.printStackTrace();
44 } catch (ClassNotFoundException e) {
45 //TODO Auto-generated catch block46 e.printStackTrace();
47 }
48 conn = DriverManager.getConnection(url);
49 if(conn.isClosed())
50 return false;
51 else
52 return true;
53
54 }
55 /**56 * 功能:关闭数据库连接57 *@authorZhengCai Xie58 *@throwsSQLException59 */
60 public void operateEnd() throws SQLException{
61 conn.close();
62 }
63 /**64 * 功能:操作数据库,Select65 *@authorZhengCai Xie66 *@paramsql67 *@returnList,将获取的数据集循环放入list中,每一个Map表示一行的值,键为该行的字段68 *@throwsSQLException69 */
70 public List> operateDataBaseBySelect(String sql) throws SQLException{
71 List> list=new ArrayList>();
72 Statement statement = conn.createStatement();
73 ResultSet rs = statement.executeQuery(sql);
74 ResultSetMetaData rsMeta=rs.getMetaData();
75 int columnCount=rsMeta.getColumnCount();
76 while(rs.next()) {
77 Map map =new HashMap();
78 for(int i=0;i
79 map.put(rsMeta.getColumnName(i+1), rs.getString(i+1));
80 }
81 list.add(map);
82
83 }
84 rs.close();
85 return list;
86
87 }
88 /**89 * 功能:操作数据库,更新,删除,插入,90 * 对于数据库操作语言,返回行计数,对于什么都不返回的sql返回091 *@authorZhengCai Xie92 *@paramsql93 *@return94 *@throwsSQLException95 */
96 public int operateDataBaseByOther(String sql) throws SQLException{
97 Statement statement = conn.createStatement();
98 return statement.executeUpdate(sql);
99 }
100 }
101
102
注:如果
url="jdbc:MySQL://127.0.0.1:3306/exing;
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(URL,"root","");
请修改成
URL ="jdbc:MySQL://127.0.0.1:3306/exing?user=root&password=";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(URL);
否则有可能会出现:no suitable driver found for jdbc:mysql错误
编写服务端:SOAPService.java
View Code
1 package cn.xiezhengcai.exing.soap;
2
3 import java.sql.SQLException;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6
7 import cn.xiezhengcai.exing.DataBaseOperate;
8 /**9 * SOAP Actual Service,提供信息的获取,及数据更新、插入等。10 * 基于底层DataBaseOperate。11 *@authorZhengCai Xie12 *13 */
14 public class SOAPService{
15 private DataBaseOperate dbo;
16 public SOAPService(){
17 dbo=new DataBaseOperate();
18 }
19 public int userRegister(String userName,int userSex,String userNickName,String userPassword ) throws SQLException{
20 return userRegister(userName,null,null,null,userSex,userNickName,userPassword);
21 }
22 public int userRegister(String userName,String userEmail,int userSex,String userNickName,String userPassword ) throws SQLException{
23 return userRegister(userName,userEmail,null,null,userSex,userNickName,userPassword);
24 }
25 public int userRegister(String userName,String userEmail,String userPhone,int userSex,String userNickName,String userPassword ) throws SQLException{
26 return userRegister(userName,userEmail,userPhone,null,userSex,userNickName,userPassword);
27 }
28 /**29 * 用户注册,30 *@authorZhengCai Xie31 *@paramuserName32 *@paramuserEmail33 *@paramuserPhone34 *@paramuserQQ35 *@paramuserSex36 *@paramuserNickName37 *@paramuserPassword38 *@throwsSQLException39 */
40 public int userRegister(String userName,String userEmail,String userPhone,String userQQ,int userSex,String userNickName,String userPassword) throws SQLException{
41 String curDate=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
42 String sql="insert into tab_user(USER_NAME,USER_EMAIL,USER_PHONE,USER_QQ,USER_SEX,USER_ADD_TIME,USER_NICKNAME,USER_PASSWORD)values('"+userName+"','"+userEmail+"','"+userPhone+"','"+userQQ+"','"+userSex+"','"+curDate+"','"+userNickName+"','"+userPassword+"')";
43 dbo.operateStart();
44 int lineCount=dbo.operateDataBaseByOther(sql);
45 dbo.operateEnd();
46 return lineCount;
47 }
48 }
注:远程调用方法userRegister()不能返回值为void,会出现[ERROR] An access occurred that is not valid.java.lang.UnsupportedOperationException: An access occurred that is not valid. 错误,
编译这两个java文件javac DataBaseOperate.java soap\*.java
打包arr
利用eclipse插件axis2 Srvice Archiver向导将编译的文件打包成arr文件
注:在Class File Loction处要选择整个包文件夹一起,勾选Include .class files only。next...后选中Generate the service xml automatically,next...输入自己定制的severname类名处记得要将包一起带上,next...直到finish之后会得到一个打包好的arr文件
将此arr文件copy到
tomcat\wabapps\axis2\WEB-INF\services下,重启tomcat,
如果用到外部类库,请将类库放置到tomcat\wabapps\axis2\WEB-INF\lib下
点击可看到相应的wsdl
到此,我们的客服端程序就可以通过KSOAP2与这个wsdl透明地与我们的服务端程序进行交互和通信了。