?
Solved

compering  2 dates and get the resultset in sql

Posted on 2003-02-27
3
Medium Priority
?
211 Views
Last Modified: 2011-10-03
the following code does not work to compare two days in access database and the date instantiated from date class please help me to solve this problem with this code or your own simple code. thanks.
 
import java.text.*;
import java.sql.*;
import java.sql.SQLException.*;

public class DatabaseCmd
{
     Connection con;
     Statement stmt;
     ResultSet rs;
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", java.util.Locale.US);
     
     public DatabaseCmd(String name)
     {
          java.util.Date date = dateFormat.parse(name, new ParsePosition(0));
          try
          {
               Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
               con =DriverManager.getConnection("jdbc:odbc:idm");               stmt=con.createStatement();
               
               rs=stmt.executeQuery("SELECT * FROM student WHERE Date='"+date+"'");
               while(rs.next())
               {
                    System.out.print(rs.getString(1)+"  ");
                    System.out.print(rs.getString(2)+"  ");
                    System.out.println(rs.getDate(3));
               }
          }
          catch(Exception eee){System.out.println(eee);}
     }

     public static void main(String args[])
     {
          DatabaseCmd a=new DatabaseCmd(args[0]);
     }    
}
0
Comment
Question by:KMAWijayasinha
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 8031924
Use a prepared statement.
something like:

public class DatabaseCmd
{
    Connection con;
    PreparedStatement stmt;
    ResultSet rs;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", java.util.Locale.US);
   
    public DatabaseCmd(String name)
    {
         java.util.Date date = dateFormat.parse(name, new ParsePosition(0));
         try
         {
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              con =DriverManager.getConnection("jdbc:odbc:idm");               stmt=con.prepareStatement("SELECT * FROM student WHERE Date=?");
              stmt.setDate(new java.sql.Date(date.getTime());
              rs=stmt.executeQuery();
              while(rs.next())
              {
                   System.out.print(rs.getString(1)+"  ");
                   System.out.print(rs.getString(2)+"  ");
                   System.out.println(rs.getDate(3));
              }
         }
         catch(Exception eee){System.out.println(eee);}
    }

    public static void main(String args[])
    {
         DatabaseCmd a=new DatabaseCmd(args[0]);
    }    
}

0
 

Expert Comment

by:CleanupPing
ID: 9059043
KMAWijayasinha:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question