By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,110 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,110 IT Pros & Developers. It's quick & easy.

problem with incrementing date!!!

P: 53
hi

i got problem in inserting the date into the database. my requirement is that when i choose a date ie from :2007-01-01 & to :2007-12-01 then all the dates starting from 2007-02-01 upto 2007-12-01 should be inserted. while inserting year,month and day should be incremented.i tried a lot and my code only increments the month. Following is my code:

public void save(String eventDt,String toDt) throws Exception{

Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;

try{
con = Utilities.getDbConnection();
//con.setAutoCommit(false);
Vector tempVector = new Vector();
StringBuffer tempBuffer = new StringBuffer();
for (int i=0; i < eventDt.length() ; i ++)
{
if (eventDt.charAt(i) == '-')
{
tempVector.add(tempBuffer.toString());
tempBuffer = new StringBuffer();
}

else
{
tempBuffer.append(eventDt.charAt(i));
}

}
tempVector.add(tempBuffer.toString());
String aa=((String)tempVector.elementAt(0));
String bb=((String)tempVector.elementAt(1));
String cc=((String)tempVector.elementAt(2));
System.out.println("day1....."+aa);
System.out.println("month1....."+bb);
System.out.println("year1....."+cc);

Vector tempVector1 = new Vector();
StringBuffer tempBuffer1 = new StringBuffer();
for (int i=0; i < toDt.length() ; i ++)
{
if (toDt.charAt(i) == '-')
{
tempVector1.add(tempBuffer1.toString());
tempBuffer1 = new StringBuffer();
}

else
{
tempBuffer1.append(toDt.charAt(i));
}

}
tempVector1.add(tempBuffer1.toString());
String dd=((String)tempVector1.elementAt(0));
String ee=((String)tempVector1.elementAt(1));
String ff=((String)tempVector1.elementAt(2));
System.out.println("day2....."+dd);
System.out.println("month2....."+ee);
System.out.println("year2....."+ff);

//String pp=(Integer.parseInt(String)tempVector1.elementAt( 1))((String)tempVector1.elementAt(1));
int a1=Integer.parseInt(bb);
int a2=Integer.parseInt(ee);
int a3=Integer.parseInt(cc);
int a4=Integer.parseInt(ff);
int a5=Integer.parseInt(aa);
int a6=Integer.parseInt(dd);

//int a3=a2-a1;
//System.out.println("a3.........."+a3);
if(a1<=a2){
for(;a1<=a2;)
//if(a1<=a2)
{
//eventDt=aa+"-"+a1+"-"+cc;
//eventDt=cc+"-"+aa+"-"+a1;
eventDt=cc+"-"+a1+"-"+aa;
stmt = con.prepareStatement("INSERT INTO TRAN_EVENT(" +
"event_cd,party_cd,project_cd,event_dt,event_dt_ne p,event_type," +
"remarks,event_updated_by,ac_ref_no) " +
"values(?,?,?,'"+eventDt+"',?,?,?,?,?)");

a1++;

stmt.setString(1,eventCd);
stmt.setString(2,partyCd);
stmt.setString(3,projectCd);
//stmt.setString(4,eventDt);
stmt.setString(4,eventDtNep);
stmt.setString(5,eventType);
stmt.setString(6,remarks);
stmt.setString(7,eventUpdatedBy);
stmt.setString(8,acRefNo);



stmt.executeUpdate();
con.commit();
log.info("Added a new tran event: " + eventCd);

}}} catch (SQLException errSql){
con.rollback();
log.debug(errSql);
throw new Exception("Could not save Event. "+errSql);
} catch (Exception err){
if (con != null) con.rollback();
throw err;
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (con != null) con.close();
} catch (SQLException errSql){}
}
}
Jul 11 '07 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
hi

i got problem in inserting the date into the database. my requirement is that when i choose a date ie from :2007-01-01 & to :2007-12-01 then all the dates starting from 2007-02-01 upto 2007-12-01 should be inserted. while inserting year,month and day should be incremented.i tried a lot and my code only increments the month. Following is my code:

public void save(String eventDt,String toDt) throws Exception{

Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;

try{
con = Utilities.getDbConnection();
//con.setAutoCommit(false);
Vector tempVector = new Vector();
StringBuffer tempBuffer = new StringBuffer();
for (int i=0; i < eventDt.length() ; i ++)
{
if (eventDt.charAt(i) == '-')
{
tempVector.add(tempBuffer.toString());
tempBuffer = new StringBuffer();
}

else
{
tempBuffer.append(eventDt.charAt(i));
}

}
tempVector.add(tempBuffer.toString());
String aa=((String)tempVector.elementAt(0));
String bb=((String)tempVector.elementAt(1));
String cc=((String)tempVector.elementAt(2));
System.out.println("day1....."+aa);
System.out.println("month1....."+bb);
System.out.println("year1....."+cc);

Vector tempVector1 = new Vector();
StringBuffer tempBuffer1 = new StringBuffer();
for (int i=0; i < toDt.length() ; i ++)
{
if (toDt.charAt(i) == '-')
{
tempVector1.add(tempBuffer1.toString());
tempBuffer1 = new StringBuffer();
}

else
{
tempBuffer1.append(toDt.charAt(i));
}

}
tempVector1.add(tempBuffer1.toString());
String dd=((String)tempVector1.elementAt(0));
String ee=((String)tempVector1.elementAt(1));
String ff=((String)tempVector1.elementAt(2));
System.out.println("day2....."+dd);
System.out.println("month2....."+ee);
System.out.println("year2....."+ff);

//String pp=(Integer.parseInt(String)tempVector1.elementAt( 1))((String)tempVector1.elementAt(1));
int a1=Integer.parseInt(bb);
int a2=Integer.parseInt(ee);
int a3=Integer.parseInt(cc);
int a4=Integer.parseInt(ff);
int a5=Integer.parseInt(aa);
int a6=Integer.parseInt(dd);

//int a3=a2-a1;
//System.out.println("a3.........."+a3);
if(a1<=a2){
for(;a1<=a2;)
//if(a1<=a2)
{
//eventDt=aa+"-"+a1+"-"+cc;
//eventDt=cc+"-"+aa+"-"+a1;
eventDt=cc+"-"+a1+"-"+aa;
stmt = con.prepareStatement("INSERT INTO TRAN_EVENT(" +
"event_cd,party_cd,project_cd,event_dt,event_dt_ne p,event_type," +
"remarks,event_updated_by,ac_ref_no) " +
"values(?,?,?,'"+eventDt+"',?,?,?,?,?)");

a1++;

stmt.setString(1,eventCd);
stmt.setString(2,partyCd);
stmt.setString(3,projectCd);
//stmt.setString(4,eventDt);
stmt.setString(4,eventDtNep);
stmt.setString(5,eventType);
stmt.setString(6,remarks);
stmt.setString(7,eventUpdatedBy);
stmt.setString(8,acRefNo);



stmt.executeUpdate();
con.commit();
log.info("Added a new tran event: " + eventCd);

}}} catch (SQLException errSql){
con.rollback();
log.debug(errSql);
throw new Exception("Could not save Event. "+errSql);
} catch (Exception err){
if (con != null) con.rollback();
throw err;
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (con != null) con.close();
} catch (SQLException errSql){}
}
}
1.) Use code tags when posting code
2.) Use GregorianCalendar to increment the days
Jul 11 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.