I have a problem with my code. I don't understand what is the problem, I hope that you give me help. I have this servlet code:
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
String from = request.getParameter("p1");
String to = request.getParameter("p2");
String titolo = request.getParameter("p3");
Date date;
double price;
String dateString, priceString;
String str= "";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(".......");
Statement statement = connection.createStatement();
String query = "SELECT f.titolo, PRO.data, SUM(CASE WHEN tipobiglietto = 'intero' THEN 1 ELSE 0 END) as numeroInteri, "+
" SUM(CASE WHEN tipobiglietto = 'ridotto' THEN 1 ELSE 0 END) as numeroRidotti "+
"FROM film f join proiezione PRO on f.idfilm = PRO.idfilm join prenotazione PRE on PRE.idproiezione = PRO.idproiezione "+
"WHERE f.titolo ='"+titolo+"' AND PRO.data BETWEEN '"+from+"' AND '"+to+"' "+
"GROUP BY PRO.data "+
"ORDER BY f.idfilm";
ResultSet rs = statement.executeQuery(query);
// Se il result set è vuoto: chiudo la stringa che contiene la sintassi JSON
if(rs.next() != true){
str += " " ;
}
//Se il result set è pieno: riempo la stringa contenente la sintassi JSON
else{
rs.beforeFirst();
while(rs.next()){
date = rs.getDate("data");
price = rs.getDouble("numeroInteri")*(8.00)+rs.getDouble("numeroRidotti")*(5.00);
dateString = date.toString();
priceString = Double.toString(price);
if(rs.isLast()){
str += "{ \"data\":\""+dateString+"\" , \"incasso\":\""+priceString+"\" }";
}
else{
str += "{ \"data\":\""+dateString+"\" , \"incasso\":\""+priceString+"\" },";
}
}
connection.close();
rs.close();
statement.close();
out.println(" { \"info\" : ["+str+" ]} ");
}
}
catch (Exception e){
System.out.print("Do not connect to DB - Error:"+e);
}
}
}
And this is ajax method
$.ajax({
type: "POST",
url: "adminFilmInformation",
data: {p1:fieldFrom, p2:fieldTo, p3:fieldTitolo},
success: function(response){
var obj = JSON.parse(response);
/*Some code*/
},
error: function(xhr,status){
alert(status);
}
});
$.ajax() show me an alert whit error message. I try to solve the problem, but i don't find a solution. So, please give me some help. I apologize for my English, because I'm an Italian boy XD. Thank you for help.