Wednesday, December 15, 2010

SQL where clause with date in Java

Using date in where clause with Java is a bit tricky, this example explains it all

import java.sql.Connection;     

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;

import java.sql.ResultSet;

public class WhereClause {   

    public static void main(String[] args) {

       try {         

            Statement stmt;

            Connection con;

              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

              con = DriverManager.getConnection("jdbc:odbc:DBNAME",null,null);

              stmt=con.createStatement();         

              ResultSet rs=stmt.executeQuery("Select * from Job_Status where Date ="+"#2010-11-27#");

            while( rs.next() )

        {       

         String str1= rs.getString("Date");

         System.out.println(str1);

      }

      stmt.close();

      con.close();

        }

        catch (Exception e) {

      System.out.println(e);

      return;

        }

    }

}

No comments:

Post a Comment