MySQL’s default DATE field format is
YYYY-MM-DD
and DATETIME type is a date and time combination which stores data in
YYYY-MM-DD HH:MM:SS
format.
Whereas in Java, the Date class’ (available in java.util package) default format is,
dow mon dd hh:mm:ss zzz yyyy
where:
dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).
mon is the month (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec).
dd is the day of the month (01 through 31), as two decimal digits.
hh is the hour of the day (00 through 23), as two decimal digits.
mm …
Whenever a user enters a date information, we get that as String type. There is a need to convert this String to java.util.Date Object.
Here is where, Java provides a class called SimpleDateFormat available in java.text package which allows for date formatting (date -> text) through format() method and parsing (text -> date) through parse() method.
In this tutorial, we will convert the String in a particular date format to java.util.Date object.
1. Assume the date text (got from user or by any other means) is available in String variable.
String myDateStr = “04/23/2011″;
2. …
Once a String object is created, its contents cannot be altered.
If you need to change a string, it always creates a new one that contains the modifications and makes the reference variable refer to the new object.
package com.ibytecode.strings.basics;
public class ImmutableStringObjects {
public static void main(String[] args) {
String s1 = “iByte”; //Statement 1
String s2 = s1; //Statement 2
s2 = s1.concat(“Code’s”); //Statement 3
s2 = s2 + ” theopentutorials.com”; //Statement 4
System.out.println(“s1 = ” + s1);
System.out.println(“s2 = ” + s2);
}
}
s1 = iByte
s2 = iByteCode’s theopentutorials.com
How many String objects and reference variables are created?
Two Reference …
Method
Purpose
concat(String)
Concatenates one string onto another.
length()
Returns the length of the string.
replace(char old, char new)
Replaces all occurrences of old character with new character.
toLowerCase()
Converts the entire string to lowercase.
toUpperCase()
Converts the entire string to uppercase.
trim()
Trims both leading and trailing whitespace from the string. It will remove the tabs (\t), carriage return (\r), new line (\n), and space characters at both ends of the string:
String Concatenation
Method Signature: public String concat(String s)
package com.ibytecode.strings.methods;
public class BasicStringMethods {
public static void main(String[] args) {
String name = "Java";
System.out.println(name.concat(" Training"));
/*name will refer to the …
Environment Used
JDK 6 (Java SE 6)
EJB 3.0
Eclipse Indigo IDE for Java EE Developers (3.7.1)
JBoss Tools – Core 3.3.0 M5 for Eclipse Indigo (3.7.1)
JBoss Application Server (AS) 5.1.0
Installing JDK
JDK should be installed with proper environment set up. Read this page for installing the JDK and setting up the environment.
Installing Eclipse IDE
We use Eclipse IDE through out this tutorial. If you need to install Eclipse, you can read this page.
Installing JBoss Tools
JBoss Tools has set of Eclipse plug-ins that supports …
Environment Used
JDK 6 (Java SE 6)
EJB 3.0 (stateless session bean)
Eclipse Indigo IDE for Java EE Developers (3.7.1)
JBoss Tools – Core 3.3.0 M5 for Eclipse Indigo (3.7.1)
JBoss Application Server (AS) 6.1.0 Final
Setting up development environment:
Read this page for installing and setting up the environment for developing and deploying EJB 3.0 Session bean on JBoss application server.
Project Description:
We are going to create a simple EJB 3 HelloWorld stateless session bean project and a remote Java application client which will call/invoke …
String concatenation with + operator
In general, Java does not allow operators to be applied to String objects. The one exception to this rule is the plus (+) operator, which concatenates two strings, producing a String object as the result.
Example,
package com.ibytecode.strings.operators;
public class PlusOperator {
public static void main(String[] args) {
String str = "the" + "open" + "tutorials" + ".com";
System.out.println("Welcome to " + str);
}
}
Welcome to theopentutorials.com
Line 4 creates seven String objects in the memory which is shown in below picture
“the”
“open”
“tutorials”
“.com”
“theopen”
“theopentutorials”
“theopentutorials.com”
Line 5 creates additional two String object “Welcome to” and “Welcome to theopentutorials.com”
String …
String objects are immutable in Java; a method that is passed a reference to a String object cannot change the original object.
package com.ibytecode.strings.parampassing;
public class PassingStringToMethod {
public static void main(String[] args) {
String str = "Hello";
System.out.println("In main: Before Passing String " +
"to method: " + str);
method(str);
System.out.println("In main: After returning " +
"from method: " + str);
}
public static void method(String strTest) {
strTest += " World";
System.out.println("In method(): " + strTest);
}
}
In main: Before Passing String to method: Hello
In method(): Hello World
In main: After returning from method: Hello
By using any Java Decompiler we can see what the enum converted class looks like.
Here we use Jad software for decompiling java class files.
Download Jad for your platform from this link – http://www.varaneckas.com/jad
Create a simple java file with the following code.
public enum PizzaSize
{
SMALL, MEDIUM, LARGE
}
Save it as “PizzaSize.java” and compile using javac to PizzaSize.class.
From Command Prompt (or terminal in linux) in windows, go to the location of Jad directory and issue the following command.
jad.exe [Path to class file]
The output file will be created in the …
Environment Used
JDK 6 (Java SE 6)
EJB 3.0 (stateless session bean)
EJB 3.0 Java Persistence API (JPA)
Eclipse Indigo IDE for Java EE Developers (3.7.1)
JBoss Tools – Core 3.3.0 M5 for Eclipse Indigo (3.7.1)
JBoss Application Server (AS) 7.1.0.CR1b / Final
MySQL 5.5 (To install MySQL refer this page)
MySQL Connector/J 5.1
Setting up development environment:
Read this page for installing and setting up the environment for developing and deploying EJB 3.0 on JBoss application server.
Project Description:
We are going to create a simple EJB 3 JPA …
Environment Used
JDK 6 (Java SE 6)
JMS Sender/Client – Java Application Client (main())
JMS Consumer – EJB 3.0 Message Driven Bean (MDB)
Eclipse Indigo IDE for Java EE Developers (3.7.1)
JBoss Tools – Core 3.3.0 M5 for Eclipse Indigo (3.7.1)
JBoss Application Server (AS) 7.1.0 Final
Setting up development environment:
Read this page for installing and setting up the environment for developing and deploying EJB 3.0 Session bean on JBoss application server.
Project Description
We are going to create a simple EJB 3 Message Driven Bean and a Java Application client …
Environment Used
JDK 6 (Java SE 6)
JMS Sender/Client – Servlet 2.5 API
JMS Consumer – EJB 3.0 Message Driven Bean (MDB)
Eclipse Indigo IDE for Java EE Developers (3.7.1)
JBoss Tools – Core 3.3.0 M5 for Eclipse Indigo (3.7.1)
JBoss Application Server (AS) 7.1.0 Final
Setting up development environment:
Read this page for installing and setting up the environment for developing and deploying EJB 3.0 Session bean on JBoss application server.
Project Description
We are going to create a simple EJB 3 Message Driven Bean and a Web client (Servlet) which …
In the following example, we create an AccountBean with two instance variables ‘name’ and ‘balance’. We show that the container assigns the same instance to the client on multiple invocations. The stateful session bean instance is associated with the client until the client session completes or times out.
Create a business interface either Remote or Local
package com.ibytecode.business;
import javax.ejb.Remote;
@Remote
public interface Account {
void deposit(double amount);
String printAccountDetails();
public String getName();
public void setName(String name);
public double getBalance();
public void setBalance(double balance);
}
Create the bean implementation class.
package com.ibytecode.businesslogic;
import com.ibytecode.business.Account;
import javax.ejb.Stateful;
@Stateful
public class AccountBean implements Account {
private String name;
private double balance;
…
The application container can pool instances of stateless session beans to increase performance.
When the client requests an operation on the bean, the container can assign an available instance from the pool reducing instance creation overhead.
After completion the bean is either returned to the pool or destroyed.
It should be noted that the same client can get different bean instances on different invocations. In the below example, bean ‘A’ has 4 instances pooled A1 to A4. Client 1 on 1st invocation gets A1 whereas on next invocation it gets A2 …
Environment Used
JDK 6 (Java SE 6)
Eclipse Indigo IDE for Java EE Developers (3.7.1)
Apache Tomcat 6.x
Java EE 5 API (Servlet 2.5)
[Optional] For monitoring and analyzing HTTP headers between the browser and web servers, you can use one of these add-ons of Firefox
Live HTTP Headers
HttpFox
Setting up development environment
If you are new to developing Servlet with Tomcat and Eclipse, you can read this page before proceeding with this example.
Sending multiple values for single parameter
If the same parameter name appears in the form data more …
Environment Used
JDK 6 (Java SE 6)
Eclipse Indigo IDE for Java EE Developers (3.7.1)
Apache Tomcat 6.x (To install Tomcat refer this page)
MySQL 5.5 (To install MySQL refer this page)
MySQL Connector/J 5.1 JAR file
JSTL JAR file (jstl-1.2.jar)
Java EE 5 API (Servlet 2.5, JSP 2.1, JSTL 1.2, Expression Language (EL))
Java Database Connectivity (JDBC) API
[Optional] For monitoring and analyzing HTTP headers between the browser and web servers, you can use one of these add-ons of Firefox
Live HTTP Headers
HttpFox
Setting up development environment
If you are new to developing …
Environment Used:
JDK 6 (Java SE 6) or later.
Any notepad or IDE for writing the Java code.
JAXB 2.1 API
JAXB Introduction
Java Architecture for XML Binding (JAXB) allows Java developers to map Java classes to XML representations. JAXB provides two main features: the ability to marshal Java objects into XML and the inverse, i.e. to unmarshal XML back into Java objects.
JAXB provides “xjc” tool to convert XML Schema to class representations. In addition, JAXB includes a “schemagen” tool which can essentially perform the inverse of “xjc”, creating an XML Schema from a set …
In the previous example provided here, we saw how to generate XML Schema from Java classes using ‘schemagen’ command line tool. Now we will see how to create the same in Eclipse IDE. We will use the same XML document as mentioned in the previous example.
Environment Used:
JDK 6 (Java SE 6) or later.
Eclipse Indigo IDE for Java EE Developers (3.7.1).
JAXB 2.1 API
Create JAXB Project
Open Eclipse IDE and create a new JAXB project which can be done in many ways,
File menu -> New -> Other -> JAXB -> JAXB Project
Click …
Environment Used
JDK 6 (Java SE 6) (To install JDK – Windows , Ubuntu)
Eclipse Indigo IDE for Java EE Developers (3.7.1) (To install Eclipse, refer this page)
Apache Tomcat 6.x (To install Tomcat refer this page)
MySQL 5.5 (To install MySQL refer this page)
MySQL Connector/J 5.1 JAR file
Java EE 5 API (Servlet 2.5)
Java Database Connectivity (JDBC) API (java.sql.*, javax.sql.*)
[Optional] For monitoring and analyzing HTTP headers between the …
Not many like the admin bar at the top provided by WordPress. Even though in WordPress 3.3, admin bar being called toolbar is much better, viewing my website as a user, the toolbar adds some unneeded options and it doesn’t fit into the site in terms of look and feel. I do not mind the admin bar at the back-end especially for one reason – I use W3C total cache and the ‘Empty All Caches’ option is the only reason I have the toolbar at the back-end.
So how do we …
In the example provided here, we saw how to generate Java classes from XML schema using JAXB binding compiler ‘xjc’ command. In this tutorial we will see how to generate the same in Eclipse IDE with the help of MOXy JAXB implementation which is available as part of Eclipselink project.
Environment Used:
JDK 6 (Java SE 6) or later.
Eclipse Indigo IDE for Java EE Developers (3.7.1).
EclipseLink 2.3.2 (Download EclipseLink from here and extract the zip file). We need this for MOXy which is an implementation of JAXB API.
Generate Java classes …
Environment Used
JDK 6 (Java SE 6) (To install JDK – Windows , Ubuntu)
Eclipse Indigo IDE for Java EE Developers (3.7.1) (To install Eclipse, refer this page)
Apache Tomcat 6.x (To install Tomcat refer this page)
MySQL 5.5 (To install MySQL refer this page)
MySQL Connector/J 5.1 JAR file
Java EE 5 API (Servlet 2.5)
Java Database Connectivity (JDBC) API (java.sql.*, javax.sql.*)
[Optional] For monitoring and analyzing HTTP headers between the …
In the previous examples provided here and here, we saw how to generate XML Schema from Java classes using ‘schemagen’ command line tool and in Eclipse IDE. Now we will see how to do the reverse (i.e.) generate Java classes from XML Schema. This is done using JAXB binding compiler ‘xjc’ command.
Environment Used:
JDK 6 (Java SE 6) or later.
JAXB 2.1 API
Introduction
Before using JAXB to create or access an XML document from Java application, we have to do the following steps:
Binding the schema
Binding a schema means generating a set …
In the previous example provided here, we saw how to generate XML Schema from Java classes using ‘schemagen’ command line tool. Now we will see how to create the same in Eclipse IDE. We will use the same XML document as mentioned in the previous example.
Environment Used:
JDK 6 (Java SE 6) or later.
Eclipse Indigo IDE for Java EE Developers (3.7.1).
JAXB 2.1 API
Create JAXB Project
Open Eclipse IDE and create a new JAXB project which can be done in many ways,
File menu -> New -> Other -> JAXB -> JAXB Project
Click …