For Exporting to Excel

December 8, 2009

change the directive content in jsp page- contentType=”application/vnd.ms-excel”;

Nested Replace query in mysql

December 8, 2009

SELECT replace( replace( replace( replace( `Name` , ‘A’, ‘a’ ) , ‘B’, ‘b’ ) , ‘C’, ‘c’ ) , ‘D’, ‘d’ ) AS Smallcase
FROM `tbldetails`
WHERE `UserName` = ‘Vishal’

over here Name is a coumn in table tbldetails and it replaces the Uppercase letters specified by us eg ‘A’ with small case ‘a’ and so on…in one go

Few Regex Tricks

December 8, 2009

site for regex
———————————————————————-
http://www.grymoire.com/Unix/Regular.html
http://www.regular-expressions.info/email.html
———————————————————————-

for checking if the value is 1 of these here w h e are 3 values

if (!returntype1.matches(“[whe]{1}”))
———————————————————————–
for cheking if email id is valid

!returnaddress1.matches(“.+@[^\\.]+\\.[a-z]+”)//start with any characcter
or
(“[^_-]+@[^\\.]+\\.[a-z]+”)//start with any character except for (- _)(hiphen and underscore)

———————————————————————–
for checking if mobile no entered is proper

if (mobileno.matches(“[1-9][0-9]{8,16}”)
(8,16 for allowing internatioanl nos having 16 digits or 8,10 for allowing 10 digit nos)
———————————————————————–
for checking if proper htttp address is entered

!returnAddress.matches(“^http:\\/\\/www\\.[a-z]+\\.(([a-z]+)|” +
“([a-z]+\\.[a-z]+))”
or

!returnAddress.startsWith(“http://”)//just chks if it starts with http

Important
—————————————————————————————————————————-
If you want to match specific characters, you can use the square brackets to identify the exact characters you are searching for. The pattern that will match any line of text that contains exactly one number is
^[0123456789]$

This is verbose. You can use the hyphen between two characters to specify a range:
^[0-9]$

You can intermix explicit characters with character ranges. This pattern will match a single character that is a letter, number, or underscore:
[A-Za-z0-9_]

—————————————————————————————————————————–

here the ^ sign(it is before the bracket) indictaes start and $ sign indicates finish

and
[^0-9] Any character other than a number

here ^ sign is withtin the braket acts as not.

———————————————————————
This allows max 2 digits after point

[0]\\.[0]{1,4}

this does the same doenot allow 0.0

\\d{1}(\\.” + “\\d{1,4}){0,1}

Web Crawling in JAVA

December 8, 2009

public class TestConnection {

/**
* @param args
*/
/**
18.
* @param args the command line arguments
19.
*/

public static void main(String[] args) {

try {
URL my_url = new URL(“http://www.vimalkumarpatel.blogspot.com/”);
BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream()));

String strTemp = “”;

while(null != (strTemp = br.readLine())){

System.out.println(strTemp);

}
} catch (Exception ex) {

ex.printStackTrace();

}

}

}

——————————————————————————-
Another method to do the same
——————————————————————————

public static void main(String[] args) {

try {
URL my_url = new URL”http://www.vimalkumarpatel.blogspot.com/”);
URLConnection urlc = my_url.openConnection();
//BufferedReader br = new BufferedReader(new InputStreamReader(my_url.openStream()));
BufferedInputStream br = new BufferedInputStream(urlc.getInputStream());
StringBuilder builder = new StringBuilder();
int byteRead;
while ((byteRead = br.read()) != -1)
builder.append((char) byteRead);
String a=builder.toString();
System.out.println(“a=”+a);
} catch (Exception ex) {

ex.printStackTrace();

}

}

}

Password Encryption using MD5

December 8, 2009

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
//import org.myorg.SystemUnavailableException;
import sun.misc.BASE64Encoder;
import sun.misc.CharacterEncoder;
import java.io.*;
public final class PasswordService
{
private static PasswordService instance;
private PasswordService()
{
}
public static synchronized String encrypt(String plaintext) throws Exception
{
MessageDigest md = null;
try
{
md = MessageDigest.getInstance(“MD5”); //step 2
}
catch(NoSuchAlgorithmException e)
{
throw new Exception(e.getMessage());
}
try
{
md.update(plaintext.getBytes(“UTF-8”)); //step 3
}
catch(UnsupportedEncodingException e)
{
throw new Exception(e.getMessage());
}
byte raw[] = md.digest(); //step 4
String hash = (new BASE64Encoder()).encode(raw); //step 5
return hash; //step 6
}
public static synchronized PasswordService getInstance() //step 1
{
if(instance == null)
{
return new PasswordService();
}
else
{
return instance;
}
}

public static void main(String[] args) {

PasswordService pwd =new PasswordService();

try{
System.out.println(pwd.encrypt(“99”));
System.out.println(pwd.encrypt(“passwordabcd”));
}catch(Exception e){

}

}

}

Email validation using java script

September 13, 2009

/**
* DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
*/

function echeck(str) {

var at=”@”
var dot=”.”
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
alert(“Invalid E-mail ID”)
return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
alert(“Invalid E-mail ID”)
return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
alert(“Invalid E-mail ID”)
return false
}

if (str.indexOf(at,(lat+1))!=-1){
alert(“Invalid E-mail ID”)
return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
alert(“Invalid E-mail ID”)
return false
}

if (str.indexOf(dot,(lat+2))==-1){
alert(“Invalid E-mail ID”)
return false
}

if (str.indexOf(” “)!=-1){
alert(“Invalid E-mail ID”)
return false
}

return true
}

function ValidateForm(){
var emailID=document.frmSample.txtEmail

if ((emailID.value==null)||(emailID.value==””)){
alert(“Please Enter your Email ID”)
emailID.focus()
return false
}
if (echeck(emailID.value)==false){
emailID.value=””
emailID.focus()
return false
}
return true
}

http://www.smartwebby.com/dhtml/email_validation.asp

Hello world!

September 4, 2009

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!