结果集在执行查询时只返回一行
result set returning only one row on executing the query
我试图通过 java 函数从数据库中检索数据。我的数据库中有多于 1 行,但结果集仅返回 table 的第一行。
public class checkoutDAOj {
static Connection con=DBConnection.getConnection();
static PreparedStatement ps=null;
static ResultSet rs=null;
static String stmnt;
public static ArrayList<products> selectcart(String uname)
{
ArrayList<products> ap=ap=new ArrayList<>();
products p=null;
ps=null;
rs=null;
stmnt="select PID,P_QTY,P_SIZE from cart where uname=?";
try {
ps=con.prepareStatement(stmnt);
ps.setString(1, uname);
rs=ps.executeQuery();
if(rs!=null)
{
int cou=0;
while(rs.next())
{
p=new products();
p.setPid(rs.getInt(1));
p.setP_quantity(rs.getInt(2));
p.setP_size(rs.getInt(3));
p=selectproduct(p);
ap.add(p);
p=null;
System.out.println(cou++);
}
System.out.println(ap);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ap;
}
private static products selectproduct(products p)
{
ps=null;
rs=null;
stmnt="select p_name,p_image,p_price from Products where pid=?";
try
{
ps=con.prepareStatement(stmnt);
ps.setInt(1,p.getPid());
rs=ps.executeQuery();
if(rs!=null)
{
while(rs.next())
{
p.setName(rs.getString(1));
p.setP_image(rs.getBlob(2));
p.setP_price(rs.getDouble(3));
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
return p;
}
}
我在这段代码中使用了两个函数来检索有关产品的完整信息。
我在 sqldeveloper
中使用 Oracle 作为数据库
当您在 selectproduct
中 prepareStatement
时,光标在 selectcart
中的连接上关闭。您需要 n
Statements
s,其中 n
是您要 运行 的查询数量(此处您需要 2
)。此外,您不能显式 null
共享 ResultSet
和 PreparedStatement
(s)。事实上,你也不能分享这些。
private static products selectproduct(products p)
{
ps=null; // <-- ends the query in the other method.
rs=null; // <-- ends the query in the other method.
我试图通过 java 函数从数据库中检索数据。我的数据库中有多于 1 行,但结果集仅返回 table 的第一行。
public class checkoutDAOj {
static Connection con=DBConnection.getConnection();
static PreparedStatement ps=null;
static ResultSet rs=null;
static String stmnt;
public static ArrayList<products> selectcart(String uname)
{
ArrayList<products> ap=ap=new ArrayList<>();
products p=null;
ps=null;
rs=null;
stmnt="select PID,P_QTY,P_SIZE from cart where uname=?";
try {
ps=con.prepareStatement(stmnt);
ps.setString(1, uname);
rs=ps.executeQuery();
if(rs!=null)
{
int cou=0;
while(rs.next())
{
p=new products();
p.setPid(rs.getInt(1));
p.setP_quantity(rs.getInt(2));
p.setP_size(rs.getInt(3));
p=selectproduct(p);
ap.add(p);
p=null;
System.out.println(cou++);
}
System.out.println(ap);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ap;
}
private static products selectproduct(products p)
{
ps=null;
rs=null;
stmnt="select p_name,p_image,p_price from Products where pid=?";
try
{
ps=con.prepareStatement(stmnt);
ps.setInt(1,p.getPid());
rs=ps.executeQuery();
if(rs!=null)
{
while(rs.next())
{
p.setName(rs.getString(1));
p.setP_image(rs.getBlob(2));
p.setP_price(rs.getDouble(3));
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
return p;
}
}
我在这段代码中使用了两个函数来检索有关产品的完整信息。
我在 sqldeveloper
中使用 Oracle 作为数据库当您在 selectproduct
中 prepareStatement
时,光标在 selectcart
中的连接上关闭。您需要 n
Statements
s,其中 n
是您要 运行 的查询数量(此处您需要 2
)。此外,您不能显式 null
共享 ResultSet
和 PreparedStatement
(s)。事实上,你也不能分享这些。
private static products selectproduct(products p)
{
ps=null; // <-- ends the query in the other method.
rs=null; // <-- ends the query in the other method.