無名

大猫咪与小狮子

导航

java+mysql在tomcat服务器下建立SOAP(axis2)通过KSOAP2访问的诸多问题

本文介绍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 * @author ZhengCai Xie
27 * @return
28 * @throws SQLException
29 * @throws ClassNotFoundException
30 * @throws IllegalAccessException
31 * @throws InstantiationException
32 *
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 block
40 e.printStackTrace();
41 } catch (IllegalAccessException e) {
42 // TODO Auto-generated catch block
43 e.printStackTrace();
44 } catch (ClassNotFoundException e) {
45 // TODO Auto-generated catch block
46 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 * @author ZhengCai Xie
58 * @throws SQLException
59 */
60 public void operateEnd() throws SQLException{
61 conn.close();
62 }
63 /**
64 * 功能:操作数据库,Select
65 * @author ZhengCai Xie
66 * @param sql
67 * @return List<Map>,将获取的数据集循环放入list中,每一个Map表示一行的值,键为该行的字段
68 * @throws SQLException
69 */
70 public List<Map<String,String>> operateDataBaseBySelect(String sql) throws SQLException{
71 List<Map<String,String>> list=new ArrayList<Map<String,String>>();
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<String,String> map =new HashMap<String,String>();
78 for(int i=0;i<columnCount;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返回0
91 * @author ZhengCai Xie
92 * @param sql
93 * @return
94 * @throws SQLException
95 */
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 * @author ZhengCai Xie
12 *
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 * @author ZhengCai Xie
31 * @param userName
32 * @param userEmail
33 * @param userPhone
34 * @param userQQ
35 * @param userSex
36 * @param userNickName
37 * @param userPassword
38 * @throws SQLException
39 */
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下

 

打开http://localhost:8080/axis2/services/listServices可看到我们指定的service,

 

点击可看到相应的wsdl

 

如上的http://soap.exing.xiezhengcai.cn既是我们service的命名空间,在客服端下需要指定

到此,我们的客服端程序就可以通过KSOAP2与这个wsdl透明地与我们的服务端程序进行交互和通信了。

 

posted on 2012-02-26 22:36  xiezhengcai  阅读(1557)  评论(0编辑  收藏  举报