Welcome to my blog, hope you enjoy reading

Friday, 28 June 2013

Eclipse to Merge Branch into Trunk

Eclipse to Merge Branch into Trunk

If the changes of branch project is merge to trunk then do the following steps in eclipse
  1. Find the revision number of the last merge 
    1. how to find the log of branch?
  2. Check out a copy of the trunk as, say, openmrs-trunk:

Thursday, 27 June 2013

Using If Condition In Select Query in mysql

Using If Condition In Select Query in mysql

For Example

select If(Amt>0,'Yes','no')

the above is not working


Select Case
When Amt > 0 Then 'Yes'
Else 'No'
End AliasYourColumnHere
From Product

Tuesday, 25 June 2013

HowTo: Run .jar files in Ubuntu

HowTo: Run .jar files in Ubuntu

  • To run a JAR file from the command line in ubuntu using the following command:
                 java -jar filename.jar
  • Otherwise it is also possible to enable double clicking to run jar files too by the following simple steps:
                 Find the .jar file in the File Browser (a.k.a Nautilus)
                 Right click the .jar file > Properties
                Click on the “Open With” tab along the top
                Change the bullet to be Sun Java 6 Runtime
                Click Close and you’re done.


create the executable jar file using java

create the executable jar file using java

To create the executable jar file using java do following steps
  1. create the swing based UI in java and generate the .class file 
  2. after creating the mainfast file your choice (.txt ) and you will give the java class name like           Main-Class: TestApp  
  3. the above step2 ,you have write the Main-Class: after given one space and press two times enter button in keyboard
  4. C:/>jar cmf test.txt myjarfilename.jar *
  5. you have generate the executable jar file
    1. for ubuntu os you can give the permistions
    2. chmod +x myjarfilename.jar.

Creating an Executable Jar Files in Eclipse

  1. Highlight the appropriate file/project
  2. File -> Export
  3.  Choose "Runnable JAR file" -> Next
  4. Make sure the right project is selected under the "Launch Configuration" dropdown and select "Package required libraries into generated JAR file" under the "Library Handling" section and make sure it's going to the correct export destination. 
  5. click Finish

Now you should have the jar file in the destination you told it to export to. Double click it and it should run. 
**HOWEVER** If this is a command line program it will not run. To run a command line program, after doing the above steps, open the command prompt, cd to the correct directory, and execute it by typing
  java -jar name_of_file.jar 


Compiling and Running Java Programs in Terminal on Ubuntu

Compiling and Running Java Programs in Terminal on Ubuntu

 You can install the open Java JDK by opening a terminal and running:

  • sudo apt-get install openjdk-6-jdk
this will also setup the classpath for you. Now you can simple compile and run you java source with the normal javac / java commands. Lets assume your file is called Myprog.java (with the class called Myprog) and is in your home folder... you can compile with:

  •  javac Myprog.java
Then run it with:

  •  java Myprog

Folder Lock without any S/W in Windows

Folder Lock without any S/W in Windows

Open Notepad and copy the be low code and save as lock e r.bat. At first tim e start it will cre ate folde r with Lock e r autom atically for u. Don't forge t to
change your password in the code its shown the place whe re to type your password.
afte r cre ation of Lock e r folde r again click on the lock e r.bat.it will ask .pre ss Y the n Lock e r folde r will be disappe are d.again to ge t it click on lock e r.bat.
and give ur password u will ge t the folde r again.

Monday, 24 June 2013

Control your keyboard with Java

Control your keyboard with Java

You may control your keyboard using this simple program.Please dont run this program directly, its just for reference

import java.awt.Robot; 
import java.awt.event.InputEvent; 
import java.awt.event.KeyEvent; 

 * To change this template, choose Tools | Templates 
 * and open the template in the editor. 
 * @author Anurag 
public class ControlKeyboard { 

    public static void main(String args[]) { 
        new ControlKeyboard().control_keyboard(); 

    public void control_keyboard() { 
        try { 
            Robot robot = new Robot(); 

            //Perform ALT+TAB which will switch your window 



            //Press Key A 
            //Similarly to press key Z use  

            //Press ALT key 

            //Press Control Key 

        } catch (Exception e) { 

Explaination :

First we make an object of Robot class.

If we want to press a keyboard key then we use the keyPress and keyRelease method.

Always make sure there is a delay of some small seconds before key press and key release events

Suppose if we want to click A then we use KeyEvent.VK_A, for B we use VK_B ...., for Z VK_Z.

To perform Control key use VK_CONTROL, ALT key we use VK_ALT and likewise there are other combinations.

To perform multiple key event we perform the key press for both keys and after that give a small delay. After that we perform the release operation.

For eg: In above example we perform keypress for both VK_ALT and VK_TAB together. Then we give small delay and then perform the keyrelease for VK_ALT and VK_TAB together

Thursday, 20 June 2013

How To Validate Date With Regular Expression

How To Validate Date With Regular Expression

Date Format (dd/mm/yyyy hh:mm:ss) Regular Expression Pattern

(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d) ([01][0-9]|2[0-3]):([012345][0-9]):([012345][0-9])


Wednesday, 19 June 2013

How To Get Installed Printer name using Java

How To Get Installed Printer name using Java

Get Installed printers in Java

  •     You can get installed printers by using both javax.print.PrintService and javax.print.PrintServiceLookup 
  •     PrintServiceLookup.lookupDefaultPrintService().getName(); will gives default printer name;

package com.javanotes2all.java.printers;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;

public class ShowPrinters {

String defaultPrinter;
    public void SearchPrinter() {
        PrintService[] ser = PrintServiceLookup.lookupPrintServices(null, null);
        System.out.println("**************** All Printers ******************");
        for (int i = 0; i < ser.length; ++i) {
            String p_name = ser[i].getName();
        defaultPrinter  =   PrintServiceLookup.lookupDefaultPrintService().getName();
        System.out.println("Default Printer  : "+defaultPrinter );
    public static void main(String[] args) {
        new ShowPrinters().SearchPrinter();


Tuesday, 18 June 2013

how to insert auto-increment varchar value in mysql

mysql insert a concat with convert (varchar to int)+1


In the mysql DB, there is a VARCHAR field with values something like this

I strip off the "AB, convert to integer and add one.
I really want the max value if the resulting integer, and this works so far.
It returns the number 1235.

SELECT MAX( CONVERT( REPLACE( myfield, 'AB', '' ) , UNSIGNED ) ) +1 FROM mytable

Now, what I really want to do is to INSERT back into mytable the result with the "AB" at the start of the result.
For the above, I want to insert myfield= 'AB1235' into mytable.


Sub-selects can only go in one place -- immediately after the list of fields. So, this would need to be written something like (untested):
  • INSERT INTO mytable (myfield) SELECT concat('AB',MAX( CONVERT( REPLACE( myfield, 'AB', '' ) , UNSIGNED ) ) +1) FROM mytable
the sub-select can only go immediately after the list of fields. In particular, it can't appear within another expression. In other words, you can only have "INSERT INTO ... VALUES ..." or "INSERT INTO ... SELECT ...", but not some combination of the two. However, you can select a constant:
  • INSERT INTO mytable (myfield1,myfield2) SELECT concat('AB',MAX( CONVERT( REPLACE(myfield1, 'AB', '' ),UNSIGNED))+1), 'sdfgr45t78ahlu' FROM mytable;

Sunday, 16 June 2013

How to get MAX value of numeric values in varchar column in mysql

How to get MAX value of numeric values in varchar column in mysql

Lets say that I have a table which contains a column for invoice number, the data type is VARCHAR with mixed string/int values like:


I tried to select max of it, but it returns with "HKL9", not the highest value "HKL15".

SELECT MAX( invoice_number )
FROM `invoice_header`


SELECT MAX(CAST(SUBSTRING(invoice_number, 4, length(invoice_number)-3) AS UNSIGNED)) FROM table

Tuesday, 11 June 2013

How To Add Password Protection To PDF Using IText In Java

How To Add Password Protection To PDF Using IText In Java

iText is very powerful library. It is used extensively to create PDF files in Java applications. Although not free, iText is the de-fecto Java library for working with PDF files. It has been extensively used in different Java related products for generating and manipulating PDF files.
Let us see how iText can be used to set password protection (encryption) to a PDF file. Also how to read an encrypted PDF file in Java.

How to create random string with random characters?

How to create random string with random characters?

package com.javanotes2all.java.randomgenerator;

import java.util.Date;
import java.util.Random;

public class RandomGenerator 
private static Random random = new Random((new Date()).getTime());
public static String generateRandomString(int length) 
      char[] values = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'};

      String out = "";

      for (int i=0;i<length;i++) {
          int idx=random.nextInt(values.length);
        out += values[idx];

      return out;
    public static void main(String[] args) 
      RandomGenerator generator=new RandomGenerator();



Monday, 10 June 2013

Convert ArrayList To Arrays In Java

Convert ArrayList To Arrays In Java

Convert ArrayList to Arrays
A lot of time I have to convert ArrayList to Arrays in my Java program. Although this is a simple task, many people don’t know how to do this and end up in iterating the java.util.ArrayList to convert it into arrays. I saw such code in one of my friends work and I thought to share this so that people don’t end up writing easy thing in complicated way.
ArrayList class has a method called toArray() that we are using in our example to convert it into Arrays.
Following is simple code snippet that converts an array list of countries into string array.

Wednesday, 5 June 2013

Changing the Font Size in Eclipse

Changing the Font Size in Eclipse

Within Eclipse click Window > Preferences. This will open up the Preferences dialog with gives you about a million configuration settings to tweak.

At the top of the list on the left you will see General. Click to open this. Next click Appearances. Finally, select Colors and Fonts from under Appearances.

This is where it gets confusing. Eclipse shows a tree of what appear to be languages or plugins or something. Underneath each one are many options for specific types of text. There are many choices in this dialog with no clearly obvious one.

Well, the one you’re looking for is “Text Font” under the “Basic” node. Select this and click the “Change…” button that appears. From here, it’s just a matter of selecting the new font size and applying it.

Oh, and when you’re ready to go back to the old font size just click the Reset button to go back to your old settings.