Tags in my blog

19 April 2009

Spring getting started

To start using spring just add this two jars:
- spring.jar
- commons-logging.jar

Put this spring xml bean configuration file with .xml extension. For example beans.xml.
<beans xmlns="http://www.springframework.org/schema/beans"
xsi="http://www.w3.org/2001/XMLSchema-instance"
schemalocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="beanName" class="path.to.ClassName">
<property name="propName" value="1"/>
<property name="otherPropName" value="2"/>
</bean>
</beans>


Load the xml bean configuration by using this syntax in your java class:
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

You can get your object using that context:
ClassName beanName = (ClassName) context.getBean("beanName");

16 April 2009

Send email with JavaMail

Require to download mail.jar and activation.jar from JavaMail library.

Example:

Main.java

package sendmail;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
*
* @author Stefan
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String host = "mail.hostname.com";
String from = "user1@hostname.com";
String to = "user2@hostname.com";
Properties props = new Properties();
props.put("mail.smtp.host", host);
Authenticator auth = new MyAuth();
Session session = Session.getDefaultInstance(props, auth);
MimeMessage message = new MimeMessage(session);
try {
Address addressFrom = new InternetAddress(from);
Address addressTo = new InternetAddress(to);
message.setText("Hello");
message.setSubject("First");
message.setFrom(addressFrom);
message.addRecipient(RecipientType.TO, addressTo);
Transport.send(message);
} catch (MessagingException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}


MyAuth.java

package sendmail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

/**
*
* @author Stefan
*/
public class MyAuth extends Authenticator {

@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user1@hostname.com", "password");
}
}

13 April 2009

How to use JUnit

To perform unit testing using JUnit, simply just add @org.junit.Test annotation before the test method.

You can run your testing class by invoking main method from JUnitCore like this:

java org.junit.runner.JUnitCore TestClass1 TestClass2 ...

If you expect certain Exception have to thrown by a JUnit test method, you can annotate the method like this:

@Test(expected = IndexOutOfBoundsException.class) public void empty() {
new ArrayList<object>().get(0);
}

How to create JavaDB database embeded and client/server

Embeded Mode

Run locally:
java -jar derbyrun.jar ij;
ij> connect 'jdbc:derby:firstdb;create=true';


To connect from java:
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String dbName="jdbcDemoDB";
String connectionURL = "jdbc:derby:" + dbName + ";create=true";



Client/Server Mode

Start the server:
java -jar derbyrun.jar server start

Start the client:
java -jar derbyrun.jar ij
ij> connect 'jdbc:derby://localhost:1527/seconddb;create=true';


To connect from java:
String driver = "org.apache.derby.jdbc.ClientDriver";
...
String connectionURL = "jdbc:derby://localhost:1527/" + dbName + ";create=true";