我想使用用java和WSDL编写的用java编写的Web服务从mysql数据库中检索数据我编写了代码并在main函数中进行了测试并运行良好但是当我部署它时,当我使用glassfish服务器时结果设置为0作为客户,这是代码。
@WebService(endpointInterface = "javasamples.two.Users")
公共类UsersImpl实现用户{
public int getUserCount() {
int numusers = 0;
String dbUrl = "jdbc:mysql://localhost:3306/javasql";
String dbClass = "com.mysql.jdbc.Driver";
String query = "Select count(*) FROM user";
String userName = "root", password = "admin";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection (dbUrl, userName, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
numusers = rs.getInt(1);
} //end while
con.close();
} //end try
catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
finally {
return numusers;
}
}
}
,界面代码为
@WebService
@SOAPBinding(style = Style.RPC) public interface Users {
@WebMethod
int getUserCount();
}
答案 0 :(得分:0)
我认为你应该关闭try循环内部的连接而不是最后一个!