Skip to content

Using Quotes In a Java String

Simply put a slah (\) before the double quote, you want use in the string.

System.out.println("\"Java!\"");

This will print out!

“Java!”

Advertisements

Java Timer Example

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Timer;
import java.util.TimerTask;



public class TimerTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int delay = 1000;   // start after a delay of 1 sec 
		int period = 50000;  // repeat 50 seconds
		Timer timer = new Timer();

		timer.scheduleAtFixedRate(new TimerTask() {
		        public void run() {
		        	//Do whatever you want
		   
		        }
		    }, delay, period);

	}

}

Connecting MySQL Database from Java with JDBC

First of all download the MySQL connector from
http://www.mysql.com/downloads/connector/j/

import java.sql.*;


public class ConnectDB {

	public static void main(String[] args) {
		Connection con = null;

		String username ="root";
		String password="password";
		String dbName = "myDatabaseName";

		String url = "jdbc:mysql://localhost:3306/" + dbName;
		String driver = "com.mysql.jdbc.Driver";

		String genre="classic";
		String mood ="happy";

		try {
			Class.forName(driver);
			con = DriverManager.getConnection(url, username, password);
			try {
				Statement st = con.createStatement();
				int val = st
						.executeUpdate("INSERT INTO test (genre, mood) VALUES('classic','happy')");

	} catch (SQLException s) {
				System.out.println("SQL statement is not executed!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

HTTP POST Request Example with Apache HttpClient

First of all download HttpClient Library from
http://hc.apache.org/downloads.cgi

import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;</code>


public class PostExample {
	public static void main(String[] args){
		String url = "http://search.yahoo.com/search";
		InputStream in = null;

		try {
			HttpClient client = new HttpClient();
			PostMethod method = new PostMethod(url);

			//p is the parameter of the search box on yahoo!
			//apple is what i want to search on yahoo!
			method.addParameter("p", "apple");

			int statusCode = client.executeMethod(method);

			if (statusCode != -1) {
				in = method.getResponseBodyAsStream();
			}

			System.out.println(in);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

Conversation in Java Example

public class Conversation {
	 public static void main(String[] args) {

		<strong> // String to Integer</strong>
		 int abc = Integer.parseInt("5");

		<strong> // Integer to String</strong>
		 String xyz = Integer.toString(5);

		 <strong>// Double to String</strong>
		 String klm = Double.toString(5);
	 }
}

How to Count the Words in a String in Java Example

public void count(String text) {
		boolean inWord = false;
		int numChars = 0;
		int numWords = 0;
		int numLines = 0;

		for (int i = 0; i &lt; text.length(); i++) {
			final char c = text.charAt(i);
			numChars++;

			switch (c) {
			case '\n':
				numLines++;

			case '\t':

			case ' ':
				if (inWord) {
					numWords++;
					inWord = false;
				}
				break;
			default:
				inWord = true;
			}
		}

		System.out.println("Number of Lines: " + numLines);
		System.out.println("Number of Words" + numWords);
		System.out.println("Number of Characters" + numChars);
	}