Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with date

Member
 
Join Date: Sep 2007
Posts: 37
#1: Jan 29 '08
Hi .. friends.. in the below program

Expand|Select|Wrap|Line Numbers
  1.  public void actionPerformed(ActionEvent ae)
  2. {
  3.  int flag=0;
  4.  s1=(from.getText());
  5.  s2=(to.getText());
  6.  if(ae.getSource()==view)
  7. {
  8.  
  9.  
  10. try 
  11. {
  12.  
  13.       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  14.       con=DriverManager.getConnection("jdbc:odbc:prism");
  15.       stmt=con.createStatement();
  16.       stmt1=con.createStatement();
  17.  
  18.       rs=stmt.executeQuery("select SUM(NOSRECEIVED),SUM(REJPCS) from Visual where DATE BETWEEN '"+s1+"' AND '"+s2+"' ");
  19.       rs1=stmt1.executeQuery("select SUM(D1),SUM(D2),SUM(D3),SUM(D4),SUM(D5),SUM(D6),SUM(D7),SUM(D8),SUM(D9),SUM(D10),SUM(D11),SUM(D12),SUM(D13) FROM Rej where DATE BETWEEN'"+s1+"'AND '"+s2+"'");
  20.  
  21.  
  22.  
  23.  }
  24.  
  25.  
where s1,s2 are String Data Types
in the above program i got the sql error as Data type mismatch criteria.. i know that the problem is with data types..can u say wat kind of data type i have to declare to solve this problem...

Thanking you....



Regards
Pradeep

BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#2: Jan 29 '08

re: Problem with date


Date can be tricky but there is a simple rule: always use PreparedStatement; never use just Statement: http://java.sun.com/docs/books/tutor.../prepared.html
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#3: Jan 29 '08

re: Problem with date


Quote:

Originally Posted by pradeep84

Hi .. friends.. in the below program

Expand|Select|Wrap|Line Numbers
  1.  public void actionPerformed(ActionEvent ae)
  2. {
  3.  int flag=0;
  4.  s1=(from.getText());
  5.  s2=(to.getText());
  6.  if(ae.getSource()==view)
  7. {
  8.  
  9.  
  10. try 
  11. {
  12.  
  13.       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  14.       con=DriverManager.getConnection("jdbc:odbc:prism");
  15.       stmt=con.createStatement();
  16.       stmt1=con.createStatement();
  17.  
  18.       rs=stmt.executeQuery("select SUM(NOSRECEIVED),SUM(REJPCS) from Visual where DATE BETWEEN '"+s1+"' AND '"+s2+"' ");
  19.       rs1=stmt1.executeQuery("select SUM(D1),SUM(D2),SUM(D3),SUM(D4),SUM(D5),SUM(D6),SUM(D7),SUM(D8),SUM(D9),SUM(D10),SUM(D11),SUM(D12),SUM(D13) FROM Rej where DATE BETWEEN'"+s1+"'AND '"+s2+"'");
  20.  
  21.  
  22.  
  23.  }
  24.  
  25.  
where s1,s2 are String Data Types
in the above program i got the sql error as Data type mismatch criteria.. i know that the problem is with data types..can u say wat kind of data type i have to declare to solve this problem...

Thanking you....



Regards
Pradeep

What database are you using? Checks its specs for a to_date(it could be called something else depending on the database you are using) function that converts characters to dates.
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#4: Jan 29 '08

re: Problem with date


Quote:

Originally Posted by r035198x

What database are you using? Checks its specs for a to_date(it could be called something else depending on the database you are using) function that converts characters to dates.

No, it's still preferable to use a PreparedStatement and let the driver worry about that. The resulting code will be more vendor-independent.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#5: Jan 29 '08

re: Problem with date


Quote:

Originally Posted by BigDaddyLH

No, it's still preferable to use a PreparedStatement and let the driver worry about that. ....

I didn't dispute that. Using the PreparedStatement makes him convert the string to date in the code (which is a good idea) and then use setDate or setTimestamp. It won't automatically convert his strings to dates for camparisions with the database values.
BigDaddyLH's Avatar
Moderator
 
Join Date: Dec 2007
Location: Kelowna, BC Canada
Posts: 1,212
#6: Jan 29 '08

re: Problem with date


Quote:

Originally Posted by r035198x

I didn't dispute that. Using the PreparedStatement makes him convert the string to date in the code (which is a good idea) and then use setDate or setTimestamp. It won't automatically convert his strings to dates for camparisions with the database values.

I agree. He should be working with a Date object, not a string. That calls for SimpleDateFormat, if starting from a string:

http://java.sun.com/javase/6/docs/ap...ateFormat.html
Reply