时间:2021-07-01 10:21:17 帮助过:1人阅读
代码2:实现接口的类,重写其中的方法。
package myweb.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.util.Date;
import java.util.Scanner;
import javax.jws.WebService;
/*
@WebService - 它是一个注解,用在类上指定将此类发布成一个ws.
Endpoint – 此类为端点服务类,它的方法publish用于将一个已经添加了@WebService注解对象绑定到一个地址的端口上。
*/
@WebService(endpointInterface = "myweb.service.SqlServer")
public class SqlServerImpl implements SqlServer {
public String readSql(int ID){
String url="jdbc:mysql://localhost:3306/company";
String user="root";
String password="";//没有密码
String sqll="select * from staff where staffID=";
//String staff="";
//System.out.println("11");
String fanhui="";
try {
Class.forName("org.mariadb.jdbc.Driver");
Connection connection;
connection = DriverManager.getConnection(url,user,password);
System.out.println("数据库连接成功");
Statement statement =connection.createStatement();
ResultSet result=statement.executeQuery(sqll+ID);
while(result.next()){
String staffID=result.getString("staffID");
String staffName=result.getString("staffName");
String department=result.getString("department");
String title=result.getString("title");
String position=result.getString("position");
String birth=result.getString("birth");
String joinDay=result.getString("joinDay");
//staff=staffID+staffName;
fanhui=staffID+" "+staffName+" "+department+" "+title+" "+position+" "+" "+birth+" "+joinDay;
//System.out.println(staffID+" "+staffName+" "+department+" "+title+" "+position+" "+" "+birth+" "+joinDay);
}
if(result!=null){result.close();result=null;}
if(statement!=null){statement.close();statement=null;}
if(connection!=null){connection.close();connection=null;}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fanhui;
}
代码3:发布服务
package myweb.service;
import javax.xml.ws.Endpoint;
public class SqlServerPublisher {
public static void main(String[] args){
// 第一个参数是发布的URL
// 第二个参数是服务的实现
Endpoint.publish("http://127.0.0.1:10101/myweb", new SqlServerImpl());
}
}
代码4:客户端调用服务
package myweb.client;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.URL;
import java.util.Scanner;
import myweb.service.*;
public class SqlClient {
public static void main(String[] args) throws Exception{
URL url = new URL("http://localhost:10103/myweb?wsdl");
// 第一个参数是服务的URI
// 第二个参数是在WSDL发布的服务名
QName qname = new QName("http://service.myweb/","SqlServerImplService");
// 创建服务
Service service = Service.create(url, qname);
// 提取端点接口,服务“端口”。
SqlServer eif = service.getPort(SqlServer.class);
System.out.println("请输入员工ID号:");
Scanner scanner=new Scanner(System.in);
int ID=scanner.nextInt();
System.out.println("staffID staffName department title position birth joinDay");
System.out.println(eif.readSql(ID));
}
}
6.2 第二个服务-根据department和title查询用户title。
1) 服务端查询数据库代码及截图:服务实现部分只有黄色加注不同于服务一,其余相同。
package myweb.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.util.Date;
import java.util.Scanner;
import javax.jws.WebService;
/*
@WebService - 它是一个注解,用在类上指定将此类发布成一个ws.
Endpoint – 此类为端点服务类,它的方法publish用于将一个已经添加了@WebService注解对象绑定到一个地址的端口上。
*/
@WebService(endpointInterface = "myweb.service.SqlServer")
public class SqlServerImpl implements SqlServer {
public String readSql2(String depar, String tit){
String url="jdbc:mysql://localhost:3306/company";
String user="root";
String password="";//没有密码
String fanhui2="";
try {
Class.forName("org.mariadb.jdbc.Driver");
Connection connection;
connection = DriverManager.getConnection(url,user,password);
System.out.println("数据库连接成功");
Statement statement =connection.createStatement();
String temp = "select * from staff where department = ‘"+depar+"‘ and title = ‘"+tit+"‘";
ResultSet result=statement.executeQuery(temp);
while(result.next()){
String staffID=result.getString("staffID");
String staffName=result.getString("staffName");
String department=result.getString("department");
String title=result.getString("title");
String position=result.getString("position");
String birth=result.getString("birth");
String joinDay=result.getString("joinDay");
fanhui2=staffID+" "+staffName+" "+department+" "+title+" "+position+" "+" "+birth+" "+joinDay;
//return fanhui2;
//System.out.println(staffID+" "+staffName+" "+department+" "+title+" "+position+" "+" "+birth+" "+joinDay);
}
if(result!=null){result.close();result=null;}
if(statement!=null){statement.close();statement=null;}
if(connection!=null){connection.close();connection=null;}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fanhui2;
}
}
2) 发布服务,端口号从新选择为10103:
package myweb.service;
import javax.xml.ws.Endpoint;
public class SqlServerPublisher {
public static void main(String[] args){
// 第一个参数是发布的URL
// 第二个参数是SIB实现,10103是服务绑定的地址端口号。
Endpoint.publish("http://127.0.0.1:10103/myweb", new SqlServerImpl());
}
}
3) 服务二的wsdl截图如下:
4)客户端代码:
package myweb.client;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.net.URL;
import java.util.Scanner;
import myweb.service.*;
public class SqlClient {
public static void main(String[] args) throws Exception{
URL url = new URL("http://localhost:10103/myweb?wsdl");
// 第一个参数是服务的URI
// 第二个参数是在WSDL发布的服务名
QName qname = new QName("http://service.myweb/","SqlServerImplService");
// 创建服务
Service service = Service.create(url, qname);
// 提取端点接口,服务“端口”。
SqlServer eif = service.getPort(SqlServer.class);
System.out.println("请输入员工department:");
Scanner scanner=new Scanner(System.in);
String depart=scanner.nextLine();
System.out.println("请输入员工title:");
String tit=scanner.nextLine();
System.out.println("staffID staffName department title position birth joinDay");
//System.out.println(eif.readSql(ID));
//Scanner scanner=new Scanner(System.in);
//String dep=scanner.nextLine();
//String tit=scanner.nextLine();
System.out.println(eif.readSql2(depart, tit));
}
}
利用jws发布一个查询员工信息的Web服务(员工信息存储在数据库中)
标签: