gpt4 book ai didi

javascript - 捕获 SQLException : Unknown column in 'where clause'

转载 作者:行者123 更新时间:2023-11-29 08:07:36 25 4
gpt4 key购买 nike

我正在制作一个日历系统,我可以在其中选择两个日期,然后返回我在这段时间内工作了多少小时。下图是一天的情况。日历系统如下所示:

DatePicker

如您所见,我有开始日期和结束日期。我应该从 javascript 中获取值并将其发送到我的 servlet。

这是 JavaScript:

<script>

$(function() {
$(".datepicker" ).datepicker({dateFormat: 'yy-mm-dd'});
});
</script>

</head>
<body>
DatePicker Ajax Post:

<form method="post" id="myForm" action="../getHoursSQL">
Start Date:
<input type="text" name="startDate" class="datepicker"/><br/>
End Date:
<input type="text" name="endDate" class="datepicker"/><br/>

<input type="submit"/>
</form>
<div id="startresult"></div>
<div id="endresult"></div>

</body>
<script type="text/javascript">

$("input[name=endDate]").datepicker({
dateFormat: 'yy-mm-dd',
onSelect: function(date, instance){
var jsonStr = $('#myForm').serializeArray();
console.log(JSON.stringify(jsonStr));
$.ajax({
url: "../getHoursSQL",
type: "post",
data: JSON,
success: function(data){
start: $("#startDate").val();
alert("success");
$("#startresult").html(data);

},
error:function(){
alert("failure");
$("#startresult").html('there is error while submit');
}
});
}
});
</script>

我的 servlet 在这里:

import java.io.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet("/getHoursSQL")
public class getHoursSQL extends HttpServlet{

private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException{



String connectionURL = "jdbc:mysql://localhost/NekiWork";
Connection connection=null;

try {
//Load database driver
Class.forName("com.mysql.jdbc.Driver");
//Connection to the database
connection = DriverManager.getConnection(connectionURL, "root", "");
//Getting the data from database

String sql = "SELECT *, (Day_hours + (Day_minutes / 60)) AS Allday_hours FROM Workdata "
+ "WHERE startDate = ? AND endDate = ? ";
PreparedStatement pst = connection.prepareStatement(sql);

SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");

java.util.Date util_StartDate = format.parse( req.getParameter("startDate") );
java.sql.Date sql_StartDate = new java.sql.Date( util_StartDate.getTime() );

java.util.Date util_EndDate = format.parse( req.getParameter("endDate") );
java.sql.Date sql_EndDate = new java.sql.Date( util_EndDate.getTime() );
pst.setDate( 1, sql_StartDate );
pst.setDate(2, sql_EndDate );
System.out.println(req.getParameter("startDate"));
System.out.println(req.getParameter("endDate"));
//Show the result from database
ResultSet rs = pst.executeQuery();

float Allday_hours_sum = 0;
while (rs.next()){
Allday_hours_sum += rs.getFloat("Allday_hours");

}
res.setContentType("text/html;charset=UTF-8");
res.getWriter().print(Allday_hours_sum);

pst.close();

}
catch(ClassNotFoundException e){

System.out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e){
System.out.println("SQLException caught: " + e.getMessage());
}
catch (Exception e){
e.printStackTrace();
}
finally {

try {
if (connection != null) connection.close();
}
catch (SQLException ignored){
System.out.println(ignored);
}
}
}
}

所以问题是,当我运行页面时,我收到错误 SQLException catch: Unknown column 'startDate'in 'where Clause'。我的数据库如下所示:

          P_id   |     Date         |   Day_hours    |        Day_minutes   |     Allday_hours  
Job 1 1 2014-03-10 4 45 4.45
Job 2 2 2014-03-10 5 15 5.15
Job 3 3 2014-03-09 3 30 3.30
Job 4 4 2014-03-09 1 45 1.45

Printet out: **15.15**

那当然是因为我的数据库中没有任何“startDate”。但是我怎样才能使用下面的 SQL 查询来获取日期呢?

String sql = "SELECT *, (Day_hours + (Day_minutes / 60)) AS Allday_hours FROM Workdata "
+ "WHERE startDate = ? AND endDate = ? ";

当我点击“提交”时,我在控制台中看到了这个 printet:

2014-03-032014-03-21

SQLException caught: Unknown column 'startDate' in 'where clause'

最佳答案

您需要在 WHERE 关键字之前添加一个空格,如下所示,

String sql = "SELECT *, (Day_hours + (Day_minutes / 60)) AS Allday_hours FROM Workdata "
+ " WHERE Date Between ? AND ? ";

关于javascript - 捕获 SQLException : Unknown column in 'where clause' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22399693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com