hi! someone might help me to correct this code's part.I'll would take the date of system (i think it's called "now")and compare that with an other date (just present in data base that i created ). And if the date is before of "now", it's all right, if the date is after "now" i'll would that in the database appare "N" near the item selected. I write down this code:
<jsp:include page="include/defineConnection.jsp"/> <% java.sql.Connection conn = java.sql.DriverManager.getConnection((String) request.getAttribute("DBConnection"), "", ""); java.sql.PreparedStatement ps = conn.prepareStatement("select * from percorsi"); java.sql.ResultSet rs = ps.executeQuery(); while (rs.next()) { String id = (String) rs.getString(1); java.util.Date data = (java.util.Date) rs.getDate(5); String[] ids = java.util.TimeZone.getAvailableIDs(+1 * 60 * 60 * 1000); java.util.SimpleTimeZone pdt = new java.util.SimpleTimeZone(+1 * 60 * 60 * 1000, ids[0]); java.util.Calendar cal = new java.util.GregorianCalendar(pdt); //java.util.Date today = cal.get(java.util.Calendar.DATE); int d = cal.get(java.util.Calendar.DAY_OF_MONTH); int m = cal.get(java.util.Calendar.MONTH); int y = cal.get(java.util.Calendar.YEAR); String oggi = d + "/" + m + "/" + "y"; java.util.Date today = java.sql.Date.valueOf(oggi); /*if (E.before(today) == true) {*/if (data.before(today)) { java.sql.PreparedStatement ps2 = conn.prepareStatement("update percorsi set stato = ? where id_percorso = ?"); ps2.setString(1, "N"); ps2.setString(2, id); int result2 = ps2.executeUpdate(); ps2.close(); }} rs.close(); ps.close(); conn.close();%>
From your code it looks like you have a table with a date field and a state field (I guess you call it stato in your code). You want to set state fields to N if the date field contains future date. If this is all you are trying to accomplish then instead of retrieving all data to JSP and then updating the record one by one, you can issue one update statement to your Database. Use a statement like this.
update your_table_name set state = 'N' where your_date_column > the_function_to_get_today_date;
Note that, the_function_to_get_today_date depends on your database. If you are using Oracle then it will be sysdate. If you are not able to figure out the function name for your database that returns current date then let us know about your database name and someone will help you.