Java 从数据库中检索特定数据并显示
如何从数据库中检索特定数据并显示Java 从数据库中检索特定数据并显示,java,sql,database,prepared-statement,Java,Sql,Database,Prepared Statement,如何从数据库中检索特定数据并显示 String sql = "Select Product_ID,Order_Quantity,Sub_Total from Order_Menu Where Order_ID='"+order_id_1+"'"; pst = conn.prepareStatement(sql); rs = pst.executeQuery(); while(rs.next()){ list.add(rs.getString("
String sql = "Select Product_ID,Order_Quantity,Sub_Total from Order_Menu Where Order_ID='"+order_id_1+"'";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while(rs.next()){
list.add(rs.getString("Product_ID"));
list.add(rs.getString("Order_Quantity"));
list.add(rs.getString("Sub_Total"));
for(int i = 0;i<list.size();i++){
String item = list.get(i);
System.out.println("Item" + i + ":" + item);
}
我的数据库只包含两个项目,分别是P0001和P0002,为什么结果会显示另外一个项目
很抱歉,我对数组有点困惑,每次从结果集中添加记录时,您都会打印整个列表内容。 在第一次迭代中,
list.size()
项目0:P0001项目1:1项目2:37.0
在第二次迭代中,list.size()
项目0:P0001项目1:1项目2:37.0项目3:P0002项目4:2项目5:666.0
从而使您的最终输出:
项目0:P0001项目1:1项目2:37.0项目0:P0001项目1:1项目2:37.0项目3:P0002项目4:2项目5:666.0
将for循环移到while语句之外应该可以修复它
while(rs.next()){
list.add(rs.getString("Product_ID"));
list.add(rs.getString("Order_Quantity"));
list.add(rs.getString("Sub_Total"));
}
for(int i = 0;i<list.size();i++){
String item = list.get(i);
System.out.println("Item" + i + ":" + item);
}
while(rs.next()){
添加(rs.getString(“产品标识”);
添加(rs.getString(“订单数量”);
添加(rs.getString(“小计”);
}
对于(int i=0;i您在迭代之间没有清除列表。这意味着当您处理第一行时,列表将包含三个项目,当您显示第二行时,列表将包含第一行和第二行中的项目
你为什么不把清单忘了,就这样
for (int i = 0; rs.next(); ++i){
System.out.println("Item " + i);
System.out.println("Product ID : " + rs.getString("Product_ID"));
System.out.println("Order Quantity: " + rs.getString("Order_Quantity"));
System.out.println("Sub_Total : " + rs.getString("Sub_Total"));
}
当然,不要忘了将整个过程包装在中,最后尝试并:
rs.close();
pst.close();
确保您正在迭代aray列表之外的列表
while(){
//implementation
}
for(){
//Iterate your list
}
首先,这不是使用PreparedStatement
的正确方法。应按以下方式使用:
String sql = "Select Product_ID,Order_Quantity,Sub_Total from Order_Menu Where Order_ID=?";
pst = conn.prepareStatement(sql);
pst.setString(order_id_1);
rs = pst.executeQuery();
现在,您输出的原因。
假设在列表的每个索引处仅放置一个产品,则在列表中添加记录。但事实并非如此。例如,如果程序获得产品P0001
,则在索引0
处添加p001
,在索引1
处添加订单数量1
,在i处添加小计37
ndex2
。因此,您将获得这样的输出。我建议您为产品
创建一个单独的类,如下所示:
class Product
{
String product_id;
String order_quantity;
String sub_total;
public Product(String product_id, String order_quantity, String sub_total)
{
this.product_id = product_id;
this.order_quantity = order_quantity;
this.sub_total = sub_total;
}
public void setProduct_id(String product_id)
{
this.product_id = product_id;
}
public void setOrder_quantity(String order_quantity)
{
this.order_quantity = order_quantity;
}
public void setSub_total(String sub_total)
{
this.sub_total = sub_total;
}
public String getProduct_id()
{
return product_id;
}
public String getOrder_quantiy()
{
return order_quantity;
}
public String getSub_total()
{
return sub_total;
}
@Override
public boolean equals(Object obj)
{
if ( obj instanceof Product)
{
temp = (Product)obj;
if (product_id.equals(temp.product_id))
{
return true;
}
}
return false;
}
@Override
public int hashCode()//override it in such a way so that you get different hashCode for each product ID.
{
return product_id.hashCode();
}
@Override
public String toString()
{
return "product_id:"+product_id+", order_quantity:"+order_quantity+", sub_total:"+sub_total;
}
}
List<Product> list = new ArrayList<Product>();
while(rs.next()){
Product product = new Product(rs.getString("Product_ID"),rs.getString("Order_Quantity"),rs.getString("Sub_Total"));
list.add(product);
}
for(int i = 0;i<list.size();i++){
Product item = list.get(i);
System.out.println("Item" + i + ":" + item);
}
Product_ID Order_Quantity Sub_Total
P0001 1 37.0
P0002 2 666.0
最后,您可以使用Product
类的对象,如下所示:
class Product
{
String product_id;
String order_quantity;
String sub_total;
public Product(String product_id, String order_quantity, String sub_total)
{
this.product_id = product_id;
this.order_quantity = order_quantity;
this.sub_total = sub_total;
}
public void setProduct_id(String product_id)
{
this.product_id = product_id;
}
public void setOrder_quantity(String order_quantity)
{
this.order_quantity = order_quantity;
}
public void setSub_total(String sub_total)
{
this.sub_total = sub_total;
}
public String getProduct_id()
{
return product_id;
}
public String getOrder_quantiy()
{
return order_quantity;
}
public String getSub_total()
{
return sub_total;
}
@Override
public boolean equals(Object obj)
{
if ( obj instanceof Product)
{
temp = (Product)obj;
if (product_id.equals(temp.product_id))
{
return true;
}
}
return false;
}
@Override
public int hashCode()//override it in such a way so that you get different hashCode for each product ID.
{
return product_id.hashCode();
}
@Override
public String toString()
{
return "product_id:"+product_id+", order_quantity:"+order_quantity+", sub_total:"+sub_total;
}
}
List<Product> list = new ArrayList<Product>();
while(rs.next()){
Product product = new Product(rs.getString("Product_ID"),rs.getString("Order_Quantity"),rs.getString("Sub_Total"));
list.add(product);
}
for(int i = 0;i<list.size();i++){
Product item = list.get(i);
System.out.println("Item" + i + ":" + item);
}
Product_ID Order_Quantity Sub_Total
P0001 1 37.0
P0002 2 666.0
List List=new ArrayList();
while(rs.next()){
产品产品=新产品(rs.getString(“产品ID”)、rs.getString(“订单数量”)、rs.getString(“小计”);
列表。添加(产品);
}
对于(int i=0;i来说,您的数据库表如下所示:
class Product
{
String product_id;
String order_quantity;
String sub_total;
public Product(String product_id, String order_quantity, String sub_total)
{
this.product_id = product_id;
this.order_quantity = order_quantity;
this.sub_total = sub_total;
}
public void setProduct_id(String product_id)
{
this.product_id = product_id;
}
public void setOrder_quantity(String order_quantity)
{
this.order_quantity = order_quantity;
}
public void setSub_total(String sub_total)
{
this.sub_total = sub_total;
}
public String getProduct_id()
{
return product_id;
}
public String getOrder_quantiy()
{
return order_quantity;
}
public String getSub_total()
{
return sub_total;
}
@Override
public boolean equals(Object obj)
{
if ( obj instanceof Product)
{
temp = (Product)obj;
if (product_id.equals(temp.product_id))
{
return true;
}
}
return false;
}
@Override
public int hashCode()//override it in such a way so that you get different hashCode for each product ID.
{
return product_id.hashCode();
}
@Override
public String toString()
{
return "product_id:"+product_id+", order_quantity:"+order_quantity+", sub_total:"+sub_total;
}
}
List<Product> list = new ArrayList<Product>();
while(rs.next()){
Product product = new Product(rs.getString("Product_ID"),rs.getString("Order_Quantity"),rs.getString("Sub_Total"));
list.add(product);
}
for(int i = 0;i<list.size();i++){
Product item = list.get(i);
System.out.println("Item" + i + ":" + item);
}
Product_ID Order_Quantity Sub_Total
P0001 1 37.0
P0002 2 666.0
while循环将元素添加到列表中,如下所示:
第一次迭代:
list=["P0001","1","37.0"]
list=["P0001","1","37.0","P0002","2","666.0"]
第二次迭代:
list=["P0001","1","37.0"]
list=["P0001","1","37.0","P0002","2","666.0"]
所以当while循环第一次执行时,
您得到的输出为:
Item0:P0001
Item1:1
Item2:37.0
在第二次迭代中,由于列表是[“P0001”、“1”、“37.0”、“P0002”、“2”、“666.0”],因此您可以得到进一步的输出
Item0:P0001
Item1:1
Item2:37.0
Item3:P0002
Item4:2
Item5:666.0
结合以上两种输出,您将获得:
Item0:P0001
Item1:1
Item2:37.0
Item0:P0001
Item1:1
Item2:37.0
Item3:P0002
Item4:2
Item5:666.0
因此,您需要二维数据结构。我建议在列表中添加一维字符串数组,如下所示:
String sql = "Select Product_ID,Order_Quantity,Sub_Total from Order_Menu Where Order_ID='"+order_id_1+"'";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
String[] row=new String[3]
while(rs.next()){
row[0]=rs.getString("Product_ID");
row[1]=rs.getString("Order_Quantity");
row[2]=rs.getString("Sub_Total");
list.add(row);
}
首先,这不是一个数组,它是数组列表,而循环没有关闭解决它…非常感谢…只是因为循环。现在我明白了,清楚的解释我解决了问题…感谢清楚的解释…谢谢你的帮助…现在我明白了为什么我的输出会是这样。