-
环境搭建
下载Axis2,eclipse插件
-
服务端开发
数据库连接:这里用的是以前现成的DBConnector.java
package net.everestinfo; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import javax.swing.JOptionPane; import oracle.jdbc.pool.OracleDataSource; import com.microsoft.sqlserver.jdbc.SQLServerDataSource; import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; public class DBConnector { private String database; private String user; private String password; private String url; private String host; private String port; private String dbType; private static Connection conn; public DBConnector(){ setConnection(); } private void setConnection(String dbType,String host,String port,String database,String user,String password) { try{ dbType = "Oracle"; database = "orcl"; host = "192.168.1.109"; port = "1521"; user = "nc57"; password = "nc57"; url = "jdbc:oracle:thin:@"+host+":"+port+":"+database; DataSource ds = null; if(dbType.equalsIgnoreCase("MySQL")){ ds=new MysqlDataSource(); url="jdbc:mysql://"+host+":"+port+"/"+database+"?user="+user+"&password="+password; ((MysqlDataSource) ds).setURL(url); } if(dbType.equalsIgnoreCase("Oracle")){ ds=new OracleDataSource(); url="jdbc:oracle:thin:@"+host+":"+port+":"+database; ((OracleDataSource) ds).setUser(user); ((OracleDataSource) ds).setPassword(password); ((OracleDataSource) ds).setURL(url); } if(dbType.equalsIgnoreCase("SQLServer2005")){ ds=new SQLServerDataSource(); url="jdbc:sqlserver://"+host+":"+port+";DatabaseName="+database; ((SQLServerDataSource) ds).setUser(user); ((SQLServerDataSource) ds).setPassword(password); ((SQLServerDataSource) ds).setURL(url); } conn = ds.getConnection(); } catch (SQLException e) { JOptionPane.showMessageDialog(null,"连接数据库失败!","错误",JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } } public static Connection getConnection() { return conn; } private void setConnection(){ try{ Properties props = new Properties(); FileReader in = new FileReader("datebaseConfig.properties"); props.load(in); in.close(); //设置数据库 dbType=props.getProperty("dbType"); host=props.getProperty("host"); port=props.getProperty("port"); database=props.getProperty("database"); user = props.getProperty("username"); password = props.getProperty("password"); DataSource ds = null; if(dbType.equalsIgnoreCase("MySQL")){ ds=new MysqlDataSource(); url="jdbc:mysql://"+host+":"+port+"/"+database+"?user="+user+"&password="+password; ((MysqlDataSource) ds).setURL(url); } if(dbType.equalsIgnoreCase("Oracle")){ ds=new OracleDataSource(); url="jdbc:oracle:thin:@"+host+":"+port+":"+database; ((OracleDataSource) ds).setUser(user); ((OracleDataSource) ds).setPassword(password); ((OracleDataSource) ds).setURL(url); } if(dbType.equalsIgnoreCase("SQLServer2005")){ ds=new SQLServerDataSource(); url="jdbc:sqlserver://"+host+":"+port+";DatabaseName="+database; ((SQLServerDataSource) ds).setUser(user); ((SQLServerDataSource) ds).setPassword(password); ((SQLServerDataSource) ds).setURL(url); } conn = ds.getConnection(); } catch (SQLException e) { JOptionPane.showMessageDialog(null,"连接数据库失败!","错误",JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null,"数据库配置文件找不到!","错误",JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } catch (IOException e) { JOptionPane.showMessageDialog(null,"加载数据库配置文件错误!","错误",JOptionPane.ERROR_MESSAGE); e.printStackTrace(); } } }
POJO类:User.java
必须实现序列化,否则Axis2无法解析
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.Serializable; /** * * @author moon */ public class User implements Serializable{ private String 姓名; private String id; /** * Get the value of id * * @return the value of id */ public String getId() { return id; } /** * Set the value of id * * @param id new value of id */ public void setId(String id) { this.id = id; } /** * Get the value of 姓名 * * @return the value of 姓名 */ public String get姓名() { return 姓名; } /** * Set the value of 姓名 * * @param 姓名 new value of 姓名 */ public void set姓名(String 姓名) { this.姓名 = 姓名; } }
测试类:Test_Netbeans.java
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import net.everestinfo.DBConnector; /** * * @author moon */ public class Test_Netbeans { private Connection conn; public Test_Netbeans(){ conn=new DBConnector().getConnection(); } /** * @param args the command line arguments */ public static void main(String[] args) { System.out.print(new Test_Netbeans().getUser("admin").get姓名()); } public User getUser(String id){ User user=new User(); user.setId(id); String s = ""; String sql = "select 用户表.user_name as 用户名 " + "from sm_user 用户表 " + "where 用户表.user_code='" + user.getId() + "'"; PreparedStatement prepstmt; try { prepstmt = conn.prepareStatement(sql); ResultSet rs = prepstmt.executeQuery(); while(rs.next()){ s=rs.getString("用户名"); } } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); }finally{ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } user.set姓名(s); return user; } }
部署服务:
注意添加JDBC驱动
选择要发布的服务
点完成,输出到service文件夹热部署
测试服务:
http://localhost:8080/axis2/services/listServices
http://localhost:8080/axis2/services/Test_Netbeans?wsdl
http://localhost:8080/axis2/services/Test_Netbeans/getUser?id=admin
注意链接内的参数(getUser?id=admin),其中id为getUser方法的参数变量名称,admin为参数值
-
客户端开发