Guys I been struggling with this issue quite a bit, I'm a novice in using ajax calls and jdbc related functions. Moving on to the point, I'm passing a datepicker value to the servlet side by using an ajax function, the function works right, I'm printing the value in a random div just to see if its working and it does work. Now how can I take that value and use it in a prepared statement to search on my database data depending on the date picked by the user, can this be done at all?
heres some code to add more context
$.ajax({
type:'POST',
data:{dateinput:dateinput},
url:'/Rest/sales?action=param2',
dataType: 'json',
success:function(result){
$('#altdate').html(result);
},
error: function (error) {
console.log(error);
}
});
});
I have a prepared statement with a static value just to see if it was working (it does) but here is where I can't make progress, how do I declare that value?
ConnectionPool pool = ConnectionPool.getInstance(DatosGeneral.DATABASE);
Connection connection = pool.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String[] ids = null;
double[] prices = null;
try{
String query = "SELECT * FROM [" + GeneralData.DATABASE + "].[dbo].[SalesDetails] "
+ "WHERE [Branch] = '" + GeneralData.bra + "' "
+ "AND [UpDate] = ?" ; //previous value 20190521
System.out.println(query);
ps = connection.prepareStatement(query,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = ps.executeQuery();
int rowcount = 0;
if (rs.last()) {
rowcount = rs.getRow();
rs.beforeFirst();
}
ids = new String[rowcount];
prices = new double[rowcount];
for (int i = 0; rs.next(); i++) {
if (rs.getString("Type").replaceAll("\\s+", "").equals("PROD")) {
ids[i] = rs.getString("ProductCode").replaceAll("\\s+", "");
prices[i] = rs.getDouble("Price");
}
}
Lastly my dopost function where I'm requesting the parameters
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action= request.getParameter("action");
if(action.equals("param1")){
int i = Integer.parseInt(request.getParameter("branch"));
GeneralData.bra = GeneralData.branches[i];
GeneralData.braName = GeneralData.braNames[i];
System.out.println(GeneralData.bra);
}else if (action.equals("param2")){
String altdate = request.getParameter("dateinput");
PrintWriter out = response.getWriter();
out.print(altdate);
}
any help would be appreciate it