Oracle-java How to fetch data between two Timestamps

hai...

I am using a program wherei have to get all the data entered into oracle db between two dates

(The first date being manually inputted and the second one is inputdate-24hrs) . The code is as follows....

Connection c =null;

PreparedStatement pStmt =null;

ResultSet rs =null;

try

{

//eg String dt=14/03/2007, hr=03, min=27, ampm=am

DateFormat dateFormat =new SimpleDateFormat("dd/MM/yyyy");

DateFormat outForm =new SimpleDateFormat("yyyy/MM/dd");

Calendar cl = Calendar.getInstance();

Date inDate =new Date();

inDate = dateFormat.parse(dt);

cl.setTimeInMillis(inDate.getTime());

cl.add(Calendar.DAY_OF_MONTH, -1);

String str1 = outForm.format(new Date(cl.getTimeInMillis()));

String str2 = outForm.format(dateFormat.parse(dt));

System.out.println("Changed date = "+str1);

System.out.println("Original date = "+str2);

String toStr = str2+" "+hr+":"+min+" "+ampm.toUpperCase();

String fromStr = str1+" "+hr+":"+min+" "+ampm.toUpperCase();

System.out.println("From date = "+fromStr);

System.out.println("To date = "+toStr);

// For example, if u r confused, From date = 2007/03/14 03:27 AM

//To date = 2007/03/13 03:27 AM

String sql ="SELECT TRANS_ID, TO_CHAR(CONCAT('Rs.',TRANS_VALUE)) AS TRANS_VALUE, " +

"TO_CHAR(TRANS_DATE,'DD/MM/YYYY HH:MM AM') AS TRANS_DATE, ACCOUNT_ID, " +

"TO_CHAR(CREATED_DATE,'DD/MM/YYYY HH:MM AM') AS CREATED_DATE, CREATED_BY, " +

"INSTR_D_BANK_CD FROM TRANS_TB WHERE " +

"ACCOUNT_ID=? AND TO_CHAR(TRANS_DATE,'YYYY/MM/DD HH:MM AM') " +

"BETWEEN ? AND ? ;

c=ds.getConnection();

pStmt = c.prepareStatement(sql);

pStmt.setString(1, accountId);

pStmt.setString(2, fromStr);

pStmt.setString(3, toStr);

rs = pStmt.executeQuery();

}

The problem is that i am not getting results as expected

Can anyone point out if there is something wrong in the logic or in the program....

[2924 byte] By [Hannobeana] at [2007-11-26 22:13:50]
# 1
Why are you binding your parameters as VARCHARs? if you're interested in retuning rows that fall between a date range, you should be binding DATE fields.
bckrispia at 2007-7-10 11:04:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

But how to make that string to the Timestamp object.

I tried this way but failed !!!

// Eg :fromStr = 2007/03/14 03:27 AM

//toStr= 2007/03/13 03:27 AM

SimpleDateFormat sdfOutput = new SimpleDateFormat("yyyy/MM/dd hh:mm a");

Date fromDate = sdfOutput.parse(fromStr);

Date toDate = sdfOutput.parse(toStr);

SELECT TRANS_ID, TO_CHAR(CONCAT('Rs.',TRANS_VALUE)) AS TRANS_VALUE, " +

"TO_CHAR(TRANS_DATE,'DD/MM/YYYY HH:MM AM') AS TRANS_DATE, ACCOUNT_ID, " +

"TO_CHAR(CREATED_DATE,'DD/MM/YYYY HH:MM AM') AS CREATED_DATE, CREATED_BY, " +

"INSTR_D_BANK_CD FROM TRANS_TB WHERE " +

"ACCOUNT_ID=? AND TRANS_DATE BETWEEN ? AND ? ;

//Eg TRANS_DATE = "11-MAR-07 12.30.59.729000000 PM"(Timestamp)

c=ds.getConnection();

pStmt = c.prepareStatement(sql);

pStmt.setString(1, accountId);

pStmt.setString(2, fromDate);

pStmt.setString(3, toDate);

rs = pStmt.executeQuery();

I think there is problem in both String-Date conversion and in the query.....

Hannobeana at 2007-7-10 11:04:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

> pStmt.setString(1, accountId);

> pStmt.setString(2, fromDate);

> pStmt.setString(3, toDate);

> rs = pStmt.executeQuery();

change that to:

pStmt.setString(1, accountId);

pStmt.setDate(2, new java.sql.Date(fromDate.getTime());

pStmt.setDate(3, new java.sql.Date(toDate.getTime());

rs = pStmt.executeQuery();

Why use setString() when you are querying on a date value? Use setDate().

itupjalua at 2007-7-10 11:04:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4
> Why use setString() when you are querying on a> date value? Use setDate().Or if the time component is important as well as the date component, then use setTimestamp() and use java.sql.Timestamp objects.
DrClapa at 2007-7-10 11:04:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 5

Besides the above you might want verify whether each end is inclusive or exclusive.

Time period reporting should always be inclusive on one end an exclusive on the other. That prevents any possibility that one entry will show up during two different time periods. It is always a requirement when calculations/summaries are involved.

jschella at 2007-7-10 11:04:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 6

Finaly its done......

Thanks everybody.

//This is what the final change i have made

.........

.........

pStmt.setTimestamp(3, new java.sql.Timestamp(fromDate.getTime()));

pStmt.setTimestamp(4, new java.sql.Timestamp(toDate.getTime()));

...........

Hannobeana at 2007-7-10 11:04:25 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...