【发布时间】:2014-12-03 12:04:33
【问题描述】:
我需要构建一个从数据库返回多行的 Web 服务,并且每行应包含由标签分隔的多个字段。我使用 Netbeans 创建了这个 ws,它在 glassfish 服务器上运行。我只设法返回包含由连接值(字段)组成的字符串的标记行。您能否就如何修改我的退货给我一些建议?
现在我的网络服务返回如下:
<?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:extractCSResponse xmlns:ns2="http://ws/">
<ROW xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string"><Firstname>Mark</Firstname><Lastname>Thomas</Lastname><ID>1112546</ID></ROW>
<ROW xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string"><Firstname>Mike</Firstname><Lastname>Jackson</Lastname><ID>1112547</ID></ROW>
</ns2:extractCSResponse>
</S:Body>
</S:Envelope>
我的回报应该是这样的:
<S:Body>
<ns2:extractCSResponse xmlns:ns2="http://ws/">
<ROWS>
<ROW>
<Firstname>Mark</Firstname>
<Lastname>Thomas</Lastname>
<IDname>1112546</IDname>
</ROW>
<ROW>
<Firstname>Mike</Firstname>
<Lastname>Jackson</Lastname>
<IDname>1112547</IDname>
</ROW>
</ROWS>
</ns2:extractCSResponse>
</S:Body>
Java 代码如下:
@WebService(serviceName = "GetCS2")
@Stateless()
public class GetCS2 {
/**
* This is a sample web service operation
*/
@WebMethod(operationName = "extractCS")
@WebResult(name = "ROW")
public List extractCS()
{
List l = new ArrayList();
//username and password for database connection
String userName = "username";
String password = "password";
//db connection
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://172.19.125.222:1433"+";databaseName=Test";
Connection con = DriverManager.getConnection(url, userName, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from list");
//loop through selection
while(rs.next())
{
l.add("<Firstname>" + rs.getString("fn") + "</Firstname>" + "<Lastname>" + rs.getString("ln") + "</Lastname>" + "<IDname>" + rs.getDate("ID") + "</ID>");
}
//close connections
stmt.close();
rs.close();
con.close();
} catch(SQLException e)
{
System.out.println("SQL Exception: " + e.toString());
}
catch(ClassNotFoundException cE)
{
System.out.println("Class Not Found Exception: " + cE.toString());
}
return l;
}
}
【问题讨论】:
-
我用一个简单的例子编辑了我的答案。希望有帮助
标签: java xml web-services netbeans tags