Java JDBC – Connect to MySQL Database

Download MySQL JDBC Driver:
http://dev.mysql.com/downloads/connector/j/

Code Snippet:

 Class.forName("com.mysql.jdbc.Driver");
  Connection connection = null;
  connection = DriverManager.getConnection(
          "jdbc:mysql://hostname:port/dbname","username", "password");
  connection.close();

Java – Run Garbage Collection Process

public class RunTheGarbageCollector {

	public static void main(String[] args) {

		/*
		 * To run the garbage collector thread use
		 * public static void gc() method of System class.
		 *
		 * Please note that, calling this method only suggest JVM
		 * to run the garbage collection process. It does not guarantee
		 * that all eligible objects will be garbage collected.
		 */

		System.out.println("Suggesting VM to run garbage collector");
		System.gc();
		System.out.println("Suggested VM to garbage collect objects");
	}

}

Java – URL Validation using Commons UrlValidator

import org.apache.commons.validator.UrlValidator;

public class ValidateUrlExample {

	public static void main(String[] args) {

	    UrlValidator urlValidator = new UrlValidator();

	    //valid URL
	    if (urlValidator.isValid("http://www.javac.in")) {
	       System.out.println("url is valid");
	    } else {
	       System.out.println("url is invalid");
	    }

	    //invalid URL
	    if (urlValidator.isValid("http://invalidURL^$&%$&^")) {
	        System.out.println("url is valid");
	    } else {
	        System.out.println("url is invalid");
	    }

	}
}

Page 1 of 3123