我正在编写一个webapp,其中有一些必须在jsp表中显示的数据透视。该查询在SQL Server中正常工作。但是我无法理解如何在jsp上投射相同内容。
我的jsp中基本上有2个日期选择器。当我选择任何一个或两个时,必须显示包含完整数据的表(基于日期范围),如果没有选择日期,我需要显示数据透视表。以下是代码。
Table.jsp
<html>
<head>
<link rel="stylesheet"
href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
</head>
<body>
<marquee>
<h1>This is an example of ajax</h1>
</marquee>
<p>
Date: <input type="text" id="startDatePicker">
</p>
<p>
Date: <input type="text" id="endDatePicker">
</p>
<span id="somediv"> <span id="err">Select Correct Dates</span>
</span>
<script type="text/javascript" src="tableGenerator.js"></script>
</body>
</html>
tableGenerator.js
var startDate = $("#startDatePicker").datepicker({
dateFormat : 'yy-mm-dd',
onClose : function(selectedDate) {
$("#endDatePicker").datepicker("option", "minDate", selectedDate);
}
});
var endDate = $("#endDatePicker").datepicker({
dateFormat : 'yy-mm-dd',
onClose : function(selectedDate) {
$("#startDatePicker").datepicker("option", "maxDate", selectedDate);
}
});
$('#startDatePicker,#endDatePicker').change(
function() {
$('#somediv > table').remove();
$.getJSON('Controller', {
'startDate' : startDate.val(),
'endDate' : endDate.val()
}, function(searchList) {
var $table = $('<table>').appendTo($('#somediv'));
if (searchList.length < 1) {
$('#err').show();
$('#err').text("No Records found in Specified Dates");
} else {
alert("second block");
$.each(searchList, function(index, userBean) {
$('#err').hide();
$('<tr>').appendTo($table).append(
$('<td>').text(userBean.caseNumber)).append(
$('<td>').text(userBean.caseOwner)).append(
$('<td>').text(userBean.status)).append(
$('<td>').text(userBean.issue)).append(
$('<td>').text(userBean.reason)).append(
$('<td>').text(userBean.age));
});
}
});
});
DataDAO.java
package com.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.bean.UserBean;
public class DataDao {
private Connection connection;
public DataDao() throws Exception {
connection = DBUtility.getConnection();
}
public ArrayList<UserBean> getFrameWork(String startDate, String endDate) throws SQLException {
String startDateFromController = startDate;
String endDateFromController = endDate;
System.out.println("DAO VAlues are " + startDateFromController + " and " + endDateFromController);
ArrayList<UserBean> list = new ArrayList<UserBean>();
PreparedStatement ps = null;
try {
String query;
if (!(startDateFromController == null) && endDateFromController == null) {
System.out.println("first block imvoked");
query = "select * from StatusTable where convert(date, [Start Time]) >= ?";
ps = connection.prepareStatement(query);
ps.setString(1, startDateFromController);
} else if (startDateFromController == null && !(endDateFromController == null)) {
System.out.println("end date is " + endDateFromController);
System.out.println("second");
query = "select * from StatusTable where convert(date, [Start Time]) <= ?";
ps = connection.prepareStatement(query);
ps.setString(1, endDateFromController);
} else if (startDateFromController == null && endDateFromController == null) {
System.out.println("Thuird block");
query = "with ctePivot as (select * from (select [case owner] as AGENT, [final status] from StatusTable) src pivot(count([final status])"
+ " for [final status] in ([CLOSED], [OPEN], [REASSIGNED])) piv) select * from ctePivot union all select 'TOTAL', sum([CLOSED]), sum([OPEN]), sum([REASSIGNED]) from ctePivot";
System.out.println("Query invoked is" + query);
ps = connection.prepareStatement(query);
} else {
System.out.println("Forth block");
query = "select * from StatusTable where convert(date, [Start Time]) between ? and ?";
ps = connection.prepareStatement(query);
ps.setString(1, startDate);
ps.setString(2, endDate);
}
ResultSet rs = ps.executeQuery();
if (!rs.next()) {
System.out.println("No records found");
} else {
do {
UserBean userBean = new UserBean();
userBean.setCaseNumber(rs.getString("Case Number"));
userBean.setCaseOwner(rs.getString("Case Owner"));
userBean.setStatus(rs.getString("Status"));
userBean.setIssue(rs.getString("Issue"));
userBean.setReason(rs.getString("Reason"));
userBean.setDateOpened(rs.getString("Date/Time Opened"));
userBean.setAge(rs.getInt("Age"));
list.add(userBean);
} while (rs.next());
}
} catch (Exception e) {
System.out.println(e + " DAO Error");
}
return list;
}
}
Controller.java
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.bean.UserBean;
import com.dao.DataDao;
import com.google.gson.Gson;
@WebServlet("/Controller")
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
/* Date Start */
String startDateStr = request.getParameter("startDate");
String endDateStr = request.getParameter("endDate");
System.out.println("Start date got is " + startDateStr + " and end date is " + endDateStr);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat print = new SimpleDateFormat("yyyy-MM-dd");
Date startParsedDate = null, endParsedDate = null;
String startDate = null, endDate = null;
if (startDateStr != null && !startDateStr.equals("")) {
startParsedDate = sdf.parse(startDateStr);
startDate = print.format(startParsedDate);
}
if (endDateStr != null && !endDateStr.equals("")) {
endParsedDate = sdf.parse(endDateStr);
endDate = print.format(endParsedDate);
}
System.out.println(startDate + " value and " + endDate);
/* Date End */
DataDao dataDao = new DataDao();
ArrayList<UserBean> list = dataDao.getFrameWork(startDate, endDate);
String searchList = new Gson().toJson(list);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(searchList);
System.out.println("servlet Done");
} catch (Exception e) {
System.err.println(e.getMessage() + " servlet Errotr");
}
}
}
MySQLQuery 在SQL SERVER中运行良好
with ctePivot as (
select *
from
(
select [case owner] as AGENT, [final status]
from StatusTable
) src
pivot
(
count([final status])
for [final status] in ([CLOSED], [OPEN], [REASSIGNED])
) piv
)
select * from ctePivot
union all
select 'TOTAL', sum([CLOSED]), sum([OPEN]), sum([REASSIGNED]) from ctePivot
无法取消我如何根据我的选择重新构建以下内容。抛出的错误就在这里。
UserBean userBean = new UserBean();
userBean.setCaseNumber(rs.getString("Case Number"));
userBean.setCaseOwner(rs.getString("Case Owner"));
userBean.setStatus(rs.getString("Status"));
userBean.setIssue(rs.getString("Issue"));
userBean.setReason(rs.getString("Reason"));
userBean.setDateOpened(rs.getString("Date/Time Opened"));
userBean.setAge(rs.getInt("Age"));
list.add(userBean);
我得到的例外是
com.microsoft.sqlserver.jdbc.SQLServerException: The column name Case Number is not valid. DAO Error
下面是我的SQL表。
当我在我的sql server中运行查询时,我得到以下结果。
请让我知道如何完成这项工作。