Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

compering 2 dates and get the resultset in sql

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
KMAWijayasinha
Asked:
KMAWijayasinha
1 Solution
 
objectsCommented:
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
 
CleanupPingCommented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now