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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

setiawanCommented:
Hi pbhag,

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

 danny
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.