这是jsp页面,它没有在变量PreparedStatement pst
中显示查询的任何结果,下面是附加的jsp页面以及必须从中获取记录的表CD的描述。如何纠正?
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page session="true" import="java.util.*,
shopping.CD,java.sql.*,shopping.getParam" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body bgcolor="#33CCFF">
<center>
<table border="0" cellpadding="0" width="100%" bgcolor="#FFFFFF">
<tr>
<td><b>ALBUM</b></td>
<td><b>ARTIST</b></td>
<td><b>COUNTRY</b></td>
<td><b>PRICE</b></td>
<td><b>QUANTITY</b></td>
<td></td>
</tr>
user is :<%= session.getAttribute("username")%>
<%
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1522:xe","xyz","xyz");
Statement st= conn.createStatement();
ResultSet rs=st.executeQuery("select distinct(TRUNC(DATEBOUGHT)) FROM CD where User_name='"+session.getAttribute("username")+"' AND STATUS IS NOT NULL ");
PreparedStatement pst = conn.prepareStatement("select Album,Artist,Country,Price,Quantity,Status from CD where Status='final' AND User_name='"+session.getAttribute("username")+"' AND DATEBOUGHT=?");
java.sql.Date str;
while(rs.next())
{
str = rs.getDate (1);
out.print(str);
pst.setDate(1,str);
ResultSet rs1= pst.executeQuery();
while(rs1.next())
{
%>
<tr>
<td><b><%= rs1.getString(1) %></b></td>
<td><b><%= rs1.getString(2) %></b></td>
<td><b><%= rs1.getString(3) %></b></td>
<td><b><%= rs1.getString(4) %></b></td>
<td><b><%= rs1.getString(5) %></b></td>
<td>
<% } }%>
</table>
</center>
<%
} catch(Exception e){e.printStackTrace();
out.println(e.getMessage());
}
%>
</body>
</html>
Name Null? Type
---------- ----- ------------
ALBUM VARCHAR2(40)
ARTIST VARCHAR2(40)
COUNTRY VARCHAR2(40)
PRICE VARCHAR2(40)
QUANTITY VARCHAR2(40)
TOTALCOST VARCHAR2(40)
USER_NAME VARCHAR2(30)
STATUS VARCHAR2(7)
DATEBOUGHT DATE
答案 0 :(得分:1)
您的第一个疑问是:
select distinct(TRUNC(DATEBOUGHT))
FROM CD
where User_name='"+session.getAttribute("username")+"'
AND STATUS IS NOT NULL
这有几个问题:
DISTINCT
是一个关键字,而不是一个函数。但回到它为什么不起作用,第二个问题是:
select Album,
Artist,
Country,
Price,
Quantity,
Status
from CD
where Status='final'
AND User_name='"+session.getAttribute("username")+"'
AND DATEBOUGHT=?
您正在比较DATEBOUGHT
和第一个查询TRUNC( DATEBOUGHT )
的值 - 除非DATEBOUGHT
值在午夜的笔划中,否则它们永远不会匹配。
您可以使用:
AND TRUNC(DATEBOUGHT)=?
或者,您可以将两个查询重写为单个查询。像这样:
select Album,
Artist,
Country,
Price,
Quantity,
Status
FROM (
SELECT c.*,
COUNT( status ) OVER ( PARTITION BY TRUNC( datebought ) ) AS ct
from CD
WHERE User_name=?
)
WHERE Status='final'
AND ct > 0
但是,Status='final'
过滤器只有在ct > 0
时才会生效,因此两者似乎都是多余的,所以我不确定为什么你需要第一个查询。