Sunday, August 11, 2013

Java Check Number Value

public boolean checkIfNumber(String in) {

        try {

            Integer.parseInt(in);

        } catch (NumberFormatException ex) {
            return false;
        }

        return true;
    }

Friday, August 9, 2013

Java Data Log File Create

MyLogger log = new MyLogger();
lg = "Query....................."
log.main(lg);


Library Class

package dcl_payroll;

/**
 *
 * @author sakib
 */
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class MyLogger {
   
   // public static void main(String[] args) {
    public static void main(String s) {
       
        Logger logger = Logger.getLogger("MyLog");
        FileHandler fh;
       
        try {
           
            // This block configure the logger with handler and formatter
            int limit = 1000000; // 1 Mb
            fh = new FileHandler("C:\\temp\\test\\MyLogFile.log", limit,1,true);
         //   fh = new FileHandler("C:/temp/test/MyLogFile.log");
            logger.addHandler(fh);
            //logger.setLevel(Level.ALL);
            SimpleFormatter formatter = new SimpleFormatter();
            fh.setFormatter(formatter);
           
            // the following statement is used to log any messages
            logger.info(s);
           
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
       
      //  logger.info("Hi How r u?");
       
    }
   
}

Java Call System Notepad


 private void jMenuItem_NotepadActionPerformed(java.awt.event.ActionEvent evt) {                                                 
        runComponents("Notepad.exe");
    }

    protected void runComponents(String sComponents)
    {
        Runtime rt = Runtime.getRuntime();
        try{rt.exec(sComponents);}
        catch(IOException evt){JOptionPane.showMessageDialog(null,evt.getMessage(),"Error Found",JOptionPane.ERROR_MESSAGE);}
    }

Java Call System Calculator

private void jMenuItem_CalculatorActionPerformed(java.awt.event.ActionEvent evt) {                                                    
         runComponents("Calc.exe");
    } 



    protected void runComponents(String sComponents)
    {
        Runtime rt = Runtime.getRuntime();
        try{rt.exec(sComponents);}
        catch(IOException evt){JOptionPane.showMessageDialog(null,evt.getMessage(),"Error Found",JOptionPane.ERROR_MESSAGE);}
    }

Java SearchKeyTyped

private void jTextField_SearchKeyTyped(java.awt.event.KeyEvent evt) {                                          
         clear();
                
                ax = jTextField_Search.getText().trim();
                String sql = "Select * from dclemployee where EmpId like ? or DclFirstname like ? or DclLastname like ? or DclPhoneC like ? or DclEmail like ?";
              
            //JOptionPane.showMessageDialog(this, jTable_Roles.getModel().getValueAt(row, 0).toString());
          
        try {
             
              pst = conn.prepareStatement(sql);
              pst.setString(1,  "%" + ax );
              pst.setString(2, ax + "%");
              pst.setString(3, ax + "%");
              pst.setString(4, ax + "%");
              pst.setString(5, ax );
              rs = pst.executeQuery();

}

Java Image Upload with Crop And Bolb Creation

private void jButton_Image_UploadActionPerformed(java.awt.event.ActionEvent evt) {                                                    
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File f = chooser.getSelectedFile();

        filename = f.getAbsolutePath();

        String fileName1 = f.getName();
        String filePath = filename.substring(0, filename.lastIndexOf(File.separator));

        //  JOptionPane.showMessageDialog(null, filePath);
        JOptionPane.showMessageDialog(null, fileName1);

        jTextField_Path.setText(filename);
        //JOptionPane.showMessageDialog(null, filename);

        try {
            imagex = new File(filename);
            fis = new FileInputStream(imagex);

            // image scale down method

            BufferedImage originalImage = ImageIO.read(new File(filename));//change path to where file is located
            int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();

            BufferedImage resizeImageJpg = resizeImage(originalImage, type, 100, 100);
            ImageIO.write(resizeImageJpg, "jpg", new File("d:\\test\\Dcl" + fileName1));

            // image scale down method

            // send to DB start
            image2 = new File("d:\\test\\Dcl" + fileName1);
            inputStream = new FileInputStream(image2);
            // send to DB End


            //  JOptionPane.showMessageDialog(null, os);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            for (int readNum; (readNum = fis.read(buf)) != -1;) {
                bos.write(buf, 0, readNum);

            }
            upload_image = bos.toByteArray();
            // view uploaded image
            ImageIcon tinyPicture = new ImageIcon(upload_image);
            Image imagexx = tinyPicture.getImage();
            //  JOptionPane.showMessageDialog(null, imagexx);
            imagexx = imagexx.getScaledInstance(105, -1, imagexx.SCALE_SMOOTH);
            tinyPicture.setImage(imagexx);
            jLabel_Image.setIcon(tinyPicture);

        } catch (Exception e) {
            System.out.println("Exception Occured:" + e);
        }
    }             

   private static BufferedImage resizeImage(BufferedImage originalImage, int type, int IMG_WIDTH, int IMG_HEIGHT) {
                        BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
                        Graphics2D g = resizedImage.createGraphics();
                        g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
                        g.dispose();

                        return resizedImage;
        }                                     

Java View Bolb Image Form Database

  if( rs.getBlob("DclImage") == null  ){
                           
                            jLabel_Image.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/datacraft.jpg")));
                       
                        }else{
                           
                            java.sql.Blob imageBlob = rs.getBlob("DclImage");

                            byte[] imageBytes = imageBlob.getBytes(1, (int) imageBlob.length());

                                // for scalling image code
                            ImageIcon tinyPicture= new ImageIcon(imageBytes);
                            Image image= tinyPicture.getImage();
                            image = image.getScaledInstance(105, -1, image.SCALE_SMOOTH);
                            tinyPicture.setImage(image);
                            jLabel_Image.setIcon(tinyPicture);
                        }

Java Close Window With Confirmation Dialog

   public void close(){
              int p = JOptionPane.showConfirmDialog(null, "Do you really want to Exit","Exit",JOptionPane.YES_NO_OPTION);
                if(p == 0){
                try{
                        WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
                        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
                }catch(Exception e){
                       
                   JOptionPane.showMessageDialog(this, e, "Invalid", JOptionPane.ERROR_MESSAGE);
        }
           
       }
    }

Java Clear Text Box And Combo Box

 public void clear(){
       
             try{

                 jText_Allowence.setText("");
                 jTextArea_Details.setText("");
                 jText_Deduction.setText("");
                  jCombo_EmpList.removeAllItems();
                  jCombo_Month.removeAllItems();
                  // set one blank
                  FillCombo();
                  // call dropdown
              
              }catch (Exception e) {
           
                       JOptionPane.showMessageDialog(this, e, "Invalid", JOptionPane.ERROR_MESSAGE);
        }
        }

Java Combo Box

  private void FillCombo(){

                 try {
                        jCombo_EmpList.addItem("");
                        rs = u.getAll_Active_Emp();
                        while(rs.next()){
                            jCombo_EmpList.addItem(rs.getString("EmpId"));
                        }
                  
                            
                     }catch (Exception e) {

                    JOptionPane.showMessageDialog(this, e);
                }
            }

Thursday, August 1, 2013

Java Given Number To Words


package dcl_payroll;

import java.io.*;


public class Words
{
 long num;
 private Words()
 {
  num=0;
 }
 private Words(long num)
 {
  this.num=num;
 }
 public void setNumber(long num)
 {
  this.num=num;
 }
 public long getNumber()
 {
  return num;
 }
 public static Words getInstance(long num)
 {
  return new Words(num);
 }
 public static String leftChars(String str,int n)
 {
  if(str.length()<=n)
   return str;
  else
   return str.substring(0,n);
 }
 public static String rightChars(String str,int n)
 {
  if(str.length()<=n)
   return str;
  else
   return str.substring(str.length()-n,str.length());
 }
 public long leftChars(int n)
 {
  return new Long(leftChars(new Long(num).toString(),n)).longValue();
 }
 public long rightChars(int n)
 {
  return new Long(rightChars(new Long(num).toString(),n)).longValue();
 }
 public long leftChars(long num,int n)
 {
  return new Long(leftChars(new Long(num).toString(),n)).longValue();
 }
 public long rightChars(long num,int n)
 {
  return new Long(rightChars(new Long(num).toString(),n)).longValue();
 }
 public int length(long num)
 {
  return new Long(num).toString().length();
 }
 private String belowTen(long x)
 {
  switch((int)x)
  {
   case 1:
    return "One ";
   case 2:
    return "Two ";
   case 3:
    return "Three ";
   case 4:
    return "Four ";
   case 5:
    return "Five ";
   case 6:
    return "Six ";
   case 7:
    return "Seven ";
   case 8:
    return "Eight ";
   case 9:
    return "Nine ";
  }
  return "";
 }
 private String belowTwenty(long x)
 {
  if(x<10)
   return belowTen(x);
  switch((int)x)
  {
   case 10:
    return "Ten ";
   case 11:
    return "Eleven ";
   case 12:
    return "Twelve ";
   case 13:
    return "Thirteen ";
   case 14:
    return "Fourteen ";
   case 15:
    return "Fifteen ";
   case 16:
    return "Sixteen ";
   case 17:
    return "Seventeen ";
   case 18:
    return "Eighteen ";
   case 19:
    return "Nineteen ";
  }
  return "";
 }
 private String belowHundred(long x)
 {
  if(x<10)
   return belowTen(x);
  else if(x<20)
   return belowTwenty(x);
  switch((int)leftChars(x,1))
  {
   case 2:
    return "Twenty "+belowTen(rightChars(x,1));
   case 3:
    return "Thirty "+belowTen(rightChars(x,1));
   case 4:
    return "Fourty "+belowTen(rightChars(x,1));
   case 5:
    return "Fifty "+belowTen(rightChars(x,1));
   case 6:
    return "Sixty "+belowTen(rightChars(x,1));
   case 7:
    return "Seventy "+belowTen(rightChars(x,1));
   case 8:
    return "Eighty "+belowTen(rightChars(x,1));
   case 9:
    return "Ninety "+belowTen(rightChars(x,1));
  }
  return "";
 }
 private String belowThousand(long x)
 {
  if(x<10)
   return belowTen(x);
  else if(x<20)
   return belowTwenty(x);
  else if(x<100)
   return belowHundred(x);
  return belowTen(leftChars(x,1))+"Hundred "+belowHundred(rightChars(x,2));
 }
 private String belowLakh(long x)
 {
  if(x<10)
   return belowTen(x);
  else if(x<20)
   return belowTwenty(x);
  else if(x<100)
   return belowHundred(x);
  else if(x<1000)
   return belowThousand(x);
  if(length(x)==4)
   return belowTen(leftChars(x,1))+"Thousand "+belowThousand(rightChars(x,3));
  else
   return belowHundred(leftChars(x,2))+"Thousand "+belowThousand(rightChars(x,3));
 }
 public String belowCrore(long x)
 {
  if(x<10)
   return belowTen(x);
  else if(x<20)
   return belowTwenty(x);
  else if(x<100)
   return belowHundred(x);
  else if(x<1000)
   return belowThousand(x);
  else if(x<100000)
   return belowLakh(x);
  if(length(x)==6)
   return belowTen(leftChars(x,1))+"Lakh "+belowLakh(rightChars(x,5));
  else
   return belowHundred(leftChars(x,2))+"Lakh "+belowLakh(rightChars(x,5));
 }
 public String belowBilion(long x)
 {
  if(x<10)
   return belowTen(x);
  else if(x<20)
   return belowTwenty(x);
  else if(x<100)
   return belowHundred(x);
  else if(x<1000)
   return belowThousand(x);
  else if(x<100000)
   return belowLakh(x);
  else if(x<100000000)
   return belowCrore(x);
 
  if(length(x)==8)
   return belowTen(leftChars(x,1))+"Bilion "+belowCrore(rightChars(x,7));
  else
   return belowHundred(leftChars(x,2))+"Bilion "+belowCrore(rightChars(x,7));
 }
 public String getNumberInWords()
 {
  if(num<10)
   return belowTen(num);
  else if(num<20)
   return belowTwenty(num);
  else if(num<100)
   return belowHundred(num);
  else if(num<1000)
   return belowThousand(num);
  else if(num<100000)
   return belowLakh(num);
  else if(num<10000000)
   return belowCrore(num);
  else if(num<1000000000)
   return belowBilion(num);
  return "";
 }
 public static void main(String[] args) throws Exception
 {
  System.out.println("Enter one number:");
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  Words w=Words.getInstance(Integer.parseInt(br.readLine()));
  System.out.println(w.getNumberInWords());
 }
}