javadatejdbc

How to put day month and year in single and double quotes when fetching from database in Java


How to put day, month and year in single and double quotes when fetching from database in Java?

    String first1 = request.getParameter("from_date");
    String Second2 = request.getParameter("to_date");
    
    String[] str = first1.split("/");
    String[] str1 = Second2.split("/");
    
    int month = Integer.parseInt(str[0]);
    int day = Integer.parseInt(str[1]);
    int year = Integer.parseInt(str[2]);
    
    int month1 = Integer.parseInt(str1[0]);
    int day1 = Integer.parseInt(str1[1]);
    int year1 = Integer.parseInt(str1[2]);
    
    System.out.println("From Date :-- Day is:" + day
            + "-Month is:" + month + "-Year is:" + year);
    System.out.println("End Date :-- Day is:" + day1
            + "-Month is:" + month1 + "-Year is:" + year1);

    rs = st.executeQuery("SELECT * FROM frt WHERE (dateinc >= '"
            + month + "/" + day + "/" + year
              "'&& dateinc <='"
            + month1 + "/" + day1 + "/" + year1'")");

in this query m not sure...where to put single(') and where to put double quotes ("). please help OR please tell me is it a right way to do it ??????


Solution

  • Not entirely sure what is it you are trying to achieve but i believe this method will make your code more readable and easier to edit in the future.

    String date = month + "/" + day + "/" + year;
    String date1 = month1 + "/" + day1 + "/" + year1;
    
    String sql = "SELECT * FROM frt WHERE (dateinc > = " + date + " AND dateinc <= " + date1 + ")";
    
    rs=st.executeQuery(sql);