怎么输出List中的每个元素
packagecom.huayu.oper;importjava.sql.Connection;importjava.sql.PreparedStatement;impo...
package com.huayu.oper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.huayu.db.DBConnection;
import com.huayu.impl.ProductInterface;
import com.huayu.voo.Product;
public class ProductOperate implements ProductInterface {
ArrayList a = new ArrayList();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = null;
DBConnection dbcn = new DBConnection();
Product p = new Product();
public ArrayList SelectAll() {
try {
sql = "select productid,productname,unitprice from products";
conn = dbcn.getConn2000();
int n = 0;
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
p.setProductid(rs.getInt(1));
p.setProductname(rs.getString(2));
p.setUnitprice(rs.getFloat(3));
a.add(p);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return a;
}
public static void main(String[] args) {
ProductOperate po=new ProductOperate();
Product p=new Product();
List l=po.SelectAll();
for (int i = 0; i < l.size(); i++) {
p=(Product)l.get(i);
System.out.println(p.getProductid()+"\t"+p.getProductname()+"\t"+p.getUnitprice());
}
}
}
一直输出77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
怎么解决 展开
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.huayu.db.DBConnection;
import com.huayu.impl.ProductInterface;
import com.huayu.voo.Product;
public class ProductOperate implements ProductInterface {
ArrayList a = new ArrayList();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = null;
DBConnection dbcn = new DBConnection();
Product p = new Product();
public ArrayList SelectAll() {
try {
sql = "select productid,productname,unitprice from products";
conn = dbcn.getConn2000();
int n = 0;
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
p.setProductid(rs.getInt(1));
p.setProductname(rs.getString(2));
p.setUnitprice(rs.getFloat(3));
a.add(p);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return a;
}
public static void main(String[] args) {
ProductOperate po=new ProductOperate();
Product p=new Product();
List l=po.SelectAll();
for (int i = 0; i < l.size(); i++) {
p=(Product)l.get(i);
System.out.println(p.getProductid()+"\t"+p.getProductname()+"\t"+p.getUnitprice());
}
}
}
一直输出77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
77 Original Frankfurter grüne So?e 13.0
怎么解决 展开
2个回答
展开全部
p.setProductid(rs.getInt(1));
p.setProductname(rs.getString(2));
p.setUnitprice(rs.getFloat(3));
可能是rs.getInt rs.getString的问题。后面的参数为数据库表对应的列名,你并没有用占位符,1 2 3 没有意义的吧。。
p.setProductname(rs.getString(2));
p.setUnitprice(rs.getFloat(3));
可能是rs.getInt rs.getString的问题。后面的参数为数据库表对应的列名,你并没有用占位符,1 2 3 没有意义的吧。。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询