Hi
I just want to get datas of three months of specifying year. I want to give input from two check boxes , one for month and the other for year. Now i want to retrieve datas from database which is in there already.

Can u help me out........???:sad:

Dani AI

Generated

As observed, the original post lacks detail. As suggested, the very first step is to confirm how dates are stored in the database (DATE/TIMESTAMP vs VARCHAR). Best practice is to store dates in a real date/time column so the database can do range comparisons efficiently.

A simple, robust approach is to compute an inclusive start and an exclusive end date and pass them as parameters to a prepared statement. Using Java 8+ time API yields clear code and avoids "last day of month" math:

LocalDate start = LocalDate.of(year, month, 1);
LocalDate end = start.plusMonths(3); // exclusive end

PreparedStatement ps = conn.prepareStatement(
  "SELECT * FROM my_table WHERE date_col >= ? AND date_col < ?");
ps.setDate(1, java.sql.Date.valueOf(start));
ps.setDate(2, java.sql.Date.valueOf(end));
ResultSet rs = ps.executeQuery();

The half-open interval (>= start AND < end) handles TIMESTAMP values cleanly and avoids off-by-one problems. If the UI is meant to pick three arbitrary months instead of a contiguous 3-month block, filter with YEAR(date_col)=? AND MONTH(date_col) IN (?,?,?). If the column is TEXT, either convert it in SQL with the DB's date-conversion function or, preferably, migrate the column to DATE/TIMESTAMP and normalize existing data.

Practical tips: use prepared statements for safety and indexing, keep DB logic out of JSP pages (use a servlet/DAO), verify timezone handling when TIMESTAMPs are involved, and log the computed start/end dates during testing to confirm the intended range is being used.

Recommended Answers

All 2 Replies

Such a vague request...

Start from database. Remember everything coming out of database will be String and you have to to transfer it to appropriate format.
So in what type/form is your date stored?
What do you have to do, get your database date format to be comparable with data which you get from JSP/servlet ?
Once you sort this, getting relevant query will be the easies part of the job

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.