Tuesday, July 13, 2010

String Vs. StringBuffer Vs. StringBuilder


String

StringBuffer

StringBuilder

Mutable

No

Yes

Yes

Synchronized

No

Yes

No

Serialized

Yes

Yes

Yes



When to use these classes?

String – When your text is not going to change
StringBuffer – When your text will be changed by multiple threads
StringBuilder – When your text will be changed by a single thread

Comparison between mutable and immutable objects

String is always considered an immutable class because String objects can't be changed. Usually the "+" or concatenation operator is used to add a character sequence to an existing String variable, but that does not mean its making alterations to the existing object. If you have a look at the example below first a String object is created and then it is concatenated with another String which eventually creates a new object for Str1 to refer to in the memory.


String str1 = "abc"; //1st Object

str1 = str1 + "d"; //2nd Object


The scenario is different for a StringBuffer or a StringBuilder. Once an object is created, it can be changed as many times as possible using methods like append(). If you look at the example below, 1st an object is created and later changes are made to the existing object in the memory using append method.

StringBuffer sbf1 = new StringBuffer("abc"); //1st Object


sbf1.append("d"); //appends "d" to the 1st Object




Author: Thiagu

Monday, July 12, 2010

Updating an existing Trans-SQL Job's interval programmatically

The title itself suggests that the topic about to be discussed now has very little thing to do with Java, but this is something that any Java programmer needs to know when they are working along with SQL Server database.

Let’s look at the scenario now, let’s say you have created a Trans-SQL JOB to run a particular process periodically, but you want to provide the feasibility for the users to change the frequency to run the Trans-SQL JOB from your Java application itself. Now how is that possible? Ya, that’s what we are about to explore now, how to change the frequency from the Java application itself.
First create a JDBC connection as shown below
Once you have created the Connection class, make use of it in the main class to establish the connection with the Database. Create a callable statement where you will call the SQL Server System stored procedure called “sp_update_schedule”. Take note that the parameters assigned below are mandatory parameters for the said Stored Procedure. If you look at the code below, the value of the “schedule_id” is assigned to “10” and “name” is assigned to “null”. This is because either 1 value only should be initialized for “sp_update_schedule” to identify the schedule need to be updated.
For further reference about “sp_update_schedule”, please refer to: http://msdn.microsoft.com/en-us/library/ms187354.aspx

Author: Thiagu