Eclipse axis开发WebService(访问MySQL数据库)

来源:互联网 发布:mac os x 10.12升级包 编辑:程序博客网 时间:2024/05/19 08:41

该文只为说明如何发布及使用WebService,并为考虑代码的设计,因此希望读者不要计较代码质量的优劣。

数据库用户名和密码都是test1,数据库名字为test,数据库test中有一个表newstable,该表中有两个属性id(int 10)和name(varchar 20)

用javaEE版的eclipse

1.      配置好Tomcat

2.      创建一个Dynamic Web Project,名为WebServiceStudy

3.      写一个service,名为TableInfoService,其中该类引用了其他两个类(MySQLUtilsRequestInfoFromMySQL)

import java.io.Serializable;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class TableInfoService implements Serializable{private static final long serialVersionUID = 677484458789332877L;private RequestInfoFromMySQL requestInfo = new RequestInfoFromMySQL();private String nameList[]; public String getSingleColoumName(){return requestInfo.getSingleColoumName();}public String getName(String name){return requestInfo.getName(name);}}

import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;import java.sql.Statement;/* * 该处的HandleMySQL负责与本地数据库连接,因此只需要修改数据库名,用户名,密码即可 * */public class MySQLUtils {private String driver = "com.mysql.jdbc.Driver";private String dbName = "test";//数据库名字private String ip = "127.0.0.1";private String url = "jdbc:mysql://127.0.0.1/";//数据库地址以及名称private String user = "test1";//数据库用户名private String password = "test1";//数据库用户密码private Connection conn;public Statement linkMySQL(){try {Class.forName(driver);//加载驱动程序conn = DriverManager.getConnection(url+dbName, user, password);//连接数据库if(!conn.isClosed())System.out.println("Succeeded connecting to the Database!");Statement statement = conn.createStatement();// statement用来执行SQL语句return statement;} catch (ClassNotFoundException e) {// TODO Auto-generated catch blockSystem.err.println("HandleMySQL Class.forName出错");System.err.println(e.getMessage());e.printStackTrace();return null;}catch (SQLException e) {// TODO Auto-generated catch blockSystem.err.println("HandleMySQL Connection出错");System.err.println(e.getMessage());//e.printStackTrace();return null;}}public PreparedStatement linkMySQL(String sql){try {Class.forName(driver);//加载驱动程序Connection conn = DriverManager.getConnection(url+dbName, user, password);//连接数据库if(!conn.isClosed())System.out.println("Succeeded connecting to the Database!");PreparedStatement preStatement = conn.prepareStatement(sql);// PreparedStatement用来执行SQL语句return preStatement;} catch (ClassNotFoundException e) {// TODO Auto-generated catch blockSystem.err.println("HandleMySQL Pre Class.forName出错");System.err.println(e.getMessage());//e.printStackTrace();return null;}catch (SQLException e) {// TODO Auto-generated catch blockSystem.err.println("HandleMySQL Pre Connection出错");System.err.println(e.getMessage());//e.printStackTrace();return null;}}public void closeMySQL(Statement statement){try {if(statement == null)return;statement.close();System.out.println("close the link to datebase");//conn.close();} catch (SQLException e) {// TODO Auto-generated catch block//e.printStackTrace();System.err.println("HandleMySQL Pre closeMySQL出错");System.err.println(e.getMessage());}}public void closeMySQL(PreparedStatement preStatement){try {if(preStatement == null)return;preStatement.close();System.out.println("close the link to datebase");//conn.close();} catch (SQLException e) {// TODO Auto-generated catch block//e.printStackTrace();System.err.println("HandleMySQL Pre closeMySQL出错");System.err.println(e.getMessage());}}}

import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class RequestInfoFromMySQL {private MySQLUtils mysqlUtils = null;private String nameList[];private String name;public String getSingleColoumName(){mysqlUtils = new MySQLUtils();Statement statement = mysqlUtils.linkMySQL();String sql = "select name from newstable";try {ResultSet rs = statement.executeQuery(sql);int i = 0;while(rs.next()){name = rs.getString(1);System.out.println(name);i++;}} catch (SQLException e) {// TODO Auto-generated catch block//e.printStackTrace();System.err.println("TableInfoService get出错");System.err.println(e.getMessage());}mysqlUtils.closeMySQL(statement);return name;}public String getName(String name){return "test1:"+name;}}


4.      右击TableInfoService类,选择WebService下的Create WebService(如果TableInfoService类有更新,重复执行该步,并选择重写文件就行)

5.      在创建WebService的过程中,一路next就行 

6.      最后会生成TableInfoService.wsdl文件,我们就是要根据这个文件来访问WebService

7.      复制.wsdl文件中的http://localhost:8080/WebServiceStudy/services/TableInfoService,访问,会出现


8.      创建一个Java Project,名字随便

9.      编写WebServiceClientTest类

import java.net.MalformedURLException;import java.net.URL;import java.rmi.RemoteException;import javax.xml.rpc.ServiceException;import org.apache.axis.client.Call;import org.apache.axis.client.Service;public class WebServiceClientTest {/** * @param args * @throws AxisFault */public static void main(String[] args) {try {String endpoint = "http://localhost:8080/WebServiceStudy/services/TableInfoService";Service service = new Service();Call call =  (Call)service.createCall();call.setTargetEndpointAddress(new URL(endpoint));call.setOperationName("getSingleColoumName");//String ret = (String)call.invoke(new Object[]{});System.out.println("return value is "+ret);} catch (ServiceException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}


 

WebService项目文件结构:(将所有引用的包放入WebContent\WEB-INF\lib下,否则会出现数据库无法连接等错误


客户端引用的包:


0 0
原创粉丝点击