Thursday, October 27, 2011

Checking of Crond Service is Running Or Not

In order to check wether "crond" service is running or not, First you have to be root user or you must have permissions of root/sudo user.

Command:-

sudo /sbin/service crond status

The Result for Above commad:

crond (pid 3342) is running...


Means crond service is running.

Friday, October 7, 2011

Calculating Difference between two dates Excluding Weekends


This script calculates the difference between two dates excluding weekdays. If Saturday and Sunday  are there in given dates it will exclude these days and gives the remaining days result.


<html>
<body>


<script type="text/javascript">
 var iWeeks, iDateDiff, iAdjust = 0;
      var fromDateSplit1 = "2011-08-05 18:31:36.0".split(" ");  // Date,mm,Year
      var toDateSplit1 = "2011-08-08 18:31:36.0".split(" ");
      var fromDateSplit =  fromDateSplit1[0].split("-");
      var toDateSplit =  toDateSplit1[0].split("-");
      
      var startDate = new Date(fromDateSplit[0],fromDateSplit[1]-1,fromDateSplit[2]);
      var endDate = new Date(toDateSplit[0],toDateSplit[1]-1,toDateSplit[2]);
      alert(startDate);
      alert(endDate );
       // if (endDate < startDate) return -1; // error code if dates transposed
        var iWeekday1 = startDate.getDay(); // day of week
        var iWeekday2 = endDate.getDay();
        iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; //change Sunday from 0 to 7
        iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
        if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; //bth days on weekend
        iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
        iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
      // differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
        iWeeks = Math.floor((endDate.getTime() - startDate.getTime()) / 604800000)


        if (iWeekday1 <= iWeekday2) {
          iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
        } else {
          iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
        }


        iDateDiff -= iAdjust // take into account both days on weekend


        alert(iDateDiff + 1); 
</script>


</body>
</html>

Wednesday, September 28, 2011

Connecting to MySql DB Through Terminal



Mysql Client Syntax :
mysql -u {mysql-user} -p {mysql-password} -h {mysql-server}

Example command:
mysql -u root -p testPassword -h localhost

 -- it will connect to mysql DB and session will be created.


As usual we can work on this session.
All normal commands will work.

For further Help: Type " man mysql "

It will show more additional options.

Friday, August 12, 2011

Converting Jsp/Html Form Values To XML

Requirements:

1. For this i used Castor Framework, it makes my work simple.
   http://www.castor.org/download.html

Go for the  " Full documentation including all JavaDocs and specs (big) "
b'cos it includes all the dependency jar files.

2. Include those Jars project.

Procedure:

Write a Jsp with minimal form elements and calling to servlet or action.
i called action in this example.




<form name="sample" action="AccessDB" method="get">

<table>

<tbody><tr>

<td>CUSTOMER Id</td>

<td> <input type="text" name="custid" value="" size="15" /></td>

</tr>

<tr>

<td>CUSTOMER NAME</td>

<td><input type="text" name="name" value="" size="25" /></td>

</tr>

<tr>

<td>DISCOUNT ID</td>

<td><select name="discountid">

<option>H</option>

<option>M</option>

<option>L</option>

<option>N</option>

</select></td>

</tr>

<tr><td> ZIP</td>

<td><input type="text" name="zip" value="" size="10" /></td>

</tr>

<tr>

<td><input type="submit" value="Submit"/></td>

</tr>

</tbody>

</form>


Here AccessDb is servlet.
below is the servlet code:



CustomerBean cb=new CustomerBean();

cb.setCustid(request.getParameter("custid").toString());

cb.setName(request.getParameter("name").toString());

cb.setDiscountid(request.getParameter("discountid").toString());

cb.setZip(request.getParameter("zip"));




FileWriter writer = new FileWriter("/home/miracle/Desktop/cbs.xml");

Marshaller.marshal(cb, writer);



CustomerBean  is a normal pojo class with normal setters and getters. 

It will create a XMl file on specified path.

<?xml version="1.0" encoding="UTF-8"?>
<customer-bean><discountid>H</discountid><name>Venkat</name><custid>1003</custid><zip>53662</zip></customer-bean>