我正在开发一个用于电子商务网站的Web服务,并且我将基于某些Supplier_id的所有产品从Java返回到php客户端,但这给了我这个错误
Fatal error: Uncaught SoapFault exception: [soapenv:Server] Before start of result set in C:\wamp\www\eshop\viewProductsSupplier.php:194 Stack trace: #0 C:\wamp\www\eshop\viewProductsSupplier.php(194): SoapClient->__soapCall('viewProducts', Array) #1 {main} thrown in C:\wamp\www\eshop\viewProductsSupplier.php on line 194
我试图检查查询是否执行,但这一切都很好
// php代码
$soapclient = new SoapClient('http://localhost:8090/Eshop_ecommerce/services/Eshop_ecommerce?wsdl', ['trace' => 1]);
session_start();
$id=array('id'=>@$_SESSION['supplierId']);
$response=$soapclient->__soapCall("viewProducts",$id);
if (isset($response->Name))
{
print $response;
}
else
{
print "Data is null";
}
// Java代码
ViewProductsResponse res = new ViewProductsResponse();
Class.forName(driver);
conn = DriverManager.getConnection(url,username,password);
stmt = conn.createStatement();
String Query=null;
String in=viewProducts.getViewProductsRequest();
if(!in.isEmpty())
{
Query="SELECT * FROM product";
}
else
{
Query="SELECT * FROM product WHERE product_supplier_id='"+viewProducts.getViewProductsRequest()+"'";
}
ProductArray pa=new ProductArray();
ResultSet result= stmt.executeQuery(Query);
//System.out.println(result);
while(result.next())
{
Product p= new Product();
//System.out.println(result.getString("product_id"));
p.setId(result.getString("product_id"));
p.setName(result.getString("product_name"));
p.setPrice(result.getString("product_price"));
p.setStock(result.getString("product_stock"));
String supplierNameQuery="SELECT supplier_id,supplier_name FROM supplier WHERE supplier_id='"+result.getString("product_supplier_id")+"'";
ResultSet resultSupplierName = stmt.executeQuery(supplierNameQuery);
p.setSupplierName(resultSupplierName.getString("supplier_name"));
String catQuery="SELECT * FROM product_category WHERE category_id='"+result.getString("product_category_id")+"'";
ResultSet resCat= stmt.executeQuery(catQuery);
p.setCategoryName(resCat.getString("category_name"));
pa.setProduct(p);
res.setProducts(pa);
}
//res.setViewProductsResponse("Data Entered");
return res;
我希望它可以在其中打印所有最终产品
答案 0 :(得分:1)
从ResultSet返回的Statement#executeQuery位于第一行之前(请参阅链接的JavaDoc)。为了使此类ResultSet
指向第一行,它需要重新定位到第一行,例如使用next()方法。这样做是针对外部SELECT
,而不是针对两个内部next()
。此外,需要检查ResultSet
调用的返回值,以查看该 if(resultSupplierName.next()) {
p.setSupplierName(resultSupplierName.getString("supplier_name"));
}
if(resCat.next()) {
p.setCategoryName(resCat.getString("category_name"));
}
中是否有任何行。
例如可以这样实现:
{{1}}
请注意,发布的代码还有其他未解决的问题,例如JDBC资源未关闭。我强烈建议您研究一下JEE或Spring(因为该代码显然在应用服务器中运行)或至少在this处进行研究。更容易产生SQL injection
的代码