SQL query in Java

I want to craete a report in a Java application. I am basically working with java and using MS access as a database.
Some of the fields in the table of the database is of type Date/time.
I want to put a quey to retieve data from this date to this date.
for eg, i want to retrieve data from month January to February. So how do i go about and write a query which will split the date of dd/mm/yyyy and will select out the complete month as such.
Kindly explain with the code.

Thank u
pbhagAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
setiawanConnect With a Mentor Commented:
Hi pbhag,

You can use MONTH function to split date into month
SELECT "Month_Number" = MONTH('12/03/1998')

 danny
0
 
Ravindra76Commented:
Hi pbhaq,

Here is sample code.



import java.io.*;
import java.sql.*;

class rep{
      public static void main(String args[]) {

      Connection con;
      PreparedStatement pst;
      ResultSet rs;
      
      try
      {


         
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con = DriverManager.getConnection("jdbc:odbc:yourdsn");

        String query = "select count(*) from yourtable where yourDateFiled>= and ? and <= ?";
        pst = con.prepareStatement(query);
        String startdate = "1999-01-01"; // yyyy-mm-dd form must be compulsory
        String enddate   = "1999-12-31";

// Between january and december

        pst.setDate(1,Date.valueOf(startdate));
        pst.setDate(2,Date.valueOf(enddate));
       
         rs = pst.executeQuery();

         while ( rs.next()) {
                  
                  String dateFromDataBase = rs.getString("yourDateField");
                   dateFromDataBase = dateFromDataBase.substring(0,10); // To cut off time from date
                  System.out.println("Date:"+dateFromDataBase);
         }

       


        }
        catch ( Exception ee)
         {
            ee.printStackTrace();
      }
      }
}

      
0
 
Ravindra76Commented:
Hi pbhaq,

Here is sample code.



import java.io.*;
import java.sql.*;

class rep{
      public static void main(String args[]) {

      Connection con;
      PreparedStatement pst;
      ResultSet rs;
      
      try
      {


         
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        con = DriverManager.getConnection("jdbc:odbc:yourdsn");

        String query = "select count(*) from yourtable where yourDateFiled>= and ? and <= ?";
        pst = con.prepareStatement(query);
        String startdate = "1999-01-01"; // yyyy-mm-dd form must be compulsory
        String enddate   = "1999-12-31";

// Between january and december

        pst.setDate(1,Date.valueOf(startdate));
        pst.setDate(2,Date.valueOf(enddate));
       
         rs = pst.executeQuery();

         while ( rs.next()) {
                  
                  String dateFromDataBase = rs.getString("yourDateField");
                   dateFromDataBase = dateFromDataBase.substring(0,10); // To cut off time from date
                  System.out.println("Date:"+dateFromDataBase);
         }

       


        }
        catch ( Exception ee)
         {
            ee.printStackTrace();
      }
      }
}

      
0
All Courses

From novice to tech pro — start learning today.