Tuesday, February 28, 2012

How to Validate Email Address using Java Pattern

8 comments


Validate an email address is easy if you have good valid pattern expression.   

package com.jsupport;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author JSupport
 */
public class ValidateEmailAddress {

    public boolean isValidEmailAddress(String emailAddress) {
        
        String expression = "^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
        CharSequence inputStr = emailAddress;
        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputStr);
        return matcher.matches();
        
    }

    public static void main(String[] args) {
        
        ValidateEmailAddress vea = new ValidateEmailAddress();

        System.out.println(vea.isValidEmailAddress("info@jsupport.info"));      // Valide emaill address
        System.out.println(vea.isValidEmailAddress("info@jsup@port.info"));     // Invalide emaill address
        System.out.println(vea.isValidEmailAddress("info@jsupport.in_fo"));     // Invalide emaill address
        System.out.println(vea.isValidEmailAddress("in@fo@jsupport.info"));     // Invalide emaill address
        System.out.println(vea.isValidEmailAddress("info@jsupport.com.lk"));    // Valide emaill address
    }
}


Here you will find how to validate email address using Java Mail API
Read more...

Friday, February 24, 2012

How to Resize Image according to the screen size using Java

0 comments
package jsupport.com;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

/**
 *
 * @author JSupport
 */
public class ResizeImage {

    private  URL url = null;

    public  BufferedImage resize() {
        try {
                url = getClass().getResource("/images/image.jpeg");
                BufferedImage originalImage = ImageIO.read(url);
                Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

                int screenWidth = (int) dim.getWidth();
                int screenHeight = (int) dim.getHeight();

                int w = originalImage.getWidth();
                int h = originalImage.getHeight();
                BufferedImage dimg = dimg = new BufferedImage(screenWidth, screenHeight, originalImage.getType());
                Graphics2D g = dimg.createGraphics();
                g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
                g.drawImage(originalImage, 0, 0, screenWidth, screenHeight, 0, 0, w, h, null);
                g.dispose();

                return dimg;
        }catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

Read more...

How To Get Installed Printer name using Java

1 comments
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 jsupport.com;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;

/**
 *
 * @author Jsupport
 */

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();
            System.out.println(p_name);
        }
        System.out.println("***********************************************\n");
        defaultPrinter  =   PrintServiceLookup.lookupDefaultPrintService().getName();
        System.out.println("Default Printer  : "+defaultPrinter );
    }

    public static void main(String[] args) {
        new ShowPrinters().SearchPrinter();
    }
}


Read more...

Friday, February 17, 2012

How to Read Property file using Java - Get Database connection parameters from property file

7 comments
  • Here is the java class to read property file which will help to create database connection. 







package jsupport.com.db;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 *
 * @author JSupport http://javasrilankansupport.blogspot.com/
 */
public class ReadPropertyFile {

    public static String db_driver;
    public static String db_url;
    public static String db_username;
    public static String db_password;

    static {
        try {
            Properties prop = new Properties();
            prop.load(new FileInputStream("Config.properties"));

            String driver = prop.getProperty("db_driver");
            String ip = prop.getProperty("db_ip");
            String dbName = prop.getProperty("db_name");

            db_username = prop.getProperty("db_username");
            db_password = prop.getProperty("db_password");

            if (driver.equals("SQL2000")) {
                db_driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
                db_url = "jdbc:microsoft:sqlserver://" + ip + ":1433;DatabaseName=" + dbName;
            } else if (driver.equals("SQL2005")) {
                db_driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
                db_url = "jdbc:sqlserver://" + ip + ":1433;databaseName=" + dbName;
            } else if (driver.equals("MySQL")) {
                db_driver = "com.mysql.jdbc.Driver";
                db_url = "jdbc:mysql://" + ip + ":3306/" + dbName;
            }

            System.out.println("Database Driver :"+db_driver+ " Database URL :"+db_url);

        } catch (IOException ex) {
            System.out.println("Can not read property file...");
        }
    }
}




  • Create Config.properties under your source code package and add following properties 


Config.properties


# JSupport http://javasrilankansupport.blogspot.com/

#databace properties
#db_driver=SQL2000
#db_driver=SQL2005
db_driver=MySQL
db_ip=localhost
db_name=mysql
db_username=root
db_password=123

Read more...

Wednesday, February 15, 2012

Connect To Access Database Using Java

11 comments
package jsupport.com.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jsupport
 */
public class AccessDatabaseConnection {

    public static Connection connect() {

        Connection con;
        try {

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=Data.mdb;";
            con = DriverManager.getConnection(database, "Admin", "64");

        } catch (Exception ex) {
            return null;
        }
        return con;
    }

    public void getData(){

        try {

            Statement stmt = connect().createStatement();
            ResultSet rset = stmt.executeQuery("SELECT * FROM tblData");

            if (rset.next()) {

                String name     = rset.getString("user_name");
                String email    = rset.getString("user_email");

                System.out.println("Name  : " +name +"  Email : "+email);
            }


        } catch (SQLException ex) {
            
        }
    }

    public static void main(String[] args) {
        new AccessDatabaseConnection().getData();
    }
}



Access Database table


     Data.mdb




Read more...