开发者

Java Oracle date

开发者 https://www.devze.com 2023-02-15 05:58 出处:网络
String sql=\"select id from period where \'\"+systemDate+\"\' between startDate and endDate\"; I need to execute this query and I am using oracle 10g. I want to get the id which falls between these
String sql="select id from period where '"+systemDate+"' between startDate and endDate";

I need to execute this query and I am using oracle 10g. I want to get the id which falls between these dates. Problem I am facing is in oracle my date format is"dd-mon-yy" and systemDa开发者_JS百科te is in format yyyy-mm-dd.So is ther any way to convert java date to dd-mon-format or any other way to execute the above query...

Kindly help.


You should convert your java.util.Date to a java.sql.Date and use a PreparedStatement as shown below:

java.util.Date jStartDate = ...; //your "java" date object
java.sql.Date startDate = new java.sql.Date(jStartDate.getTime());

java.util.Date jEndDate = ...;
java.sql.Date endDate = new java.sql.Date(jEndDate.getTime());

PreparedStatement p = connection.prepareStatement("select id from period where systemDate between ? and ?");
p.setDate(1, startDate);
p.setDate(2, endDate);
ResultSet rs = p.executeQuery();


Java has date formatting classes. DateFormat is the abstract parent class for these, SimpleDateFormat here is probably the one you want

SimpleDateFormat oracleStyle = new SimpleDateFormat("dd-MM-yy");
String dString = oracleStyle.format(systemDate);
String sql="select id from period where '"+systemDate+"' between startDate and endDate";

I havent tested this. The format call might need new string buffers as arguments, and my format string could be off, but this is the idea. Read the documentation

0

精彩评论

暂无评论...
验证码 换一张
取 消