Archive for the ‘Java’ Category

Integrate PostgreSQL Data Source in Websphere Application Server

Thursday, October 12th, 2006

A quite unusual configuration but this short guidance might be useful for some persons (like me):

  1. Open Integrated Solution Console
  2. Navigate to “Resources -> JDBC -> JDBC Providers”
  3. Select the right scope for your application
  4. Click “New”
  5. Select “User-defined” from “Database Type” dropdown
  6. Insert “org.postgresql.ds.PGConnectionPoolDataSource” at “Implementation class name” field
  7. Provide a name and a descriotion of your choice and click “next”
  8. If you haven’t done so before download the JDBC Driver from http://jdbc.postgresql.org/download.html which suits your needs (should be JDBC3 driver for your DB version) and save it at some local library path (e.g. {WAS_HOME}/lib)
  9. Insert the path and jar file name in the next dialog, click “next” and “finish” afterwards
  10. Navigate to “Resources -> JDBC -> Data sources”
  11. Again select the right scope and click “new”
  12. Provide a Data source and a jndi name (e.g. jdbc/myPGdb)
  13. On step 2 dialog select your already created JDBC provider
  14. Just leave step 3 at default values and finish creation, that’s it!

Tested on Websphere Application Server 6.1

J2EE Development with Rational Application Developer

Friday, September 29th, 2006

As I have to develop a serious application during my thesis first time ever with Rational Application Developer I was seraching for a good introduction on how to use this mighty tool. I found it, who yould have thought, in an IBM redbook. “Rational Application Developer V6 Programming Guide” is an awesome entrance into the RAD world. Abutted on the classical SUN J2EE tutorial this book describes in detail how to realise a sample banking application with RAD and Websphere Application Server. It covers the realisation of JSP and Servlet-, JSF and SDO-, even Struts based Dynamic Web Application and the integration of EJB Business logic using integrated UML- and other Data modelling tools.
A must read for any person who thinks Eclipse is too much code work ;)

Java Instant Messenger

Friday, May 19th, 2006

After some time here’s another code example. I had to write a Java based Instant Messenger for university. The code demonstrates the usage of object serialisation mechanisms in Java and sending streams via TCP and UDP. Features included are Unicast based chatting, Multicast based Chatting and File Transfer via TCP. As it’s just a proof of concept there might be some minur issues when running it and as there was a deadline there are some “unaesthetic” code parts (don’t look at the GUI part, I had to adapt that from my professor ;) ) but the main concepts should be pointed out quite ok. Especially the file transfer via TCP migth be interesting because most of the tutorials one can find just handle Strings or something else.
The complete eclispe project can be found here.

JDBC and DOM Parser example

Friday, October 14th, 2005

The following code snippet demostrates how Java can be used to read database tables and generate a valid xml file out of it. The example uses a DB2 database but should be ported to other databases with small effort. the data is provied by the sample database provided by DB2. Used techniques are JDBC and DOM Parser.

import java.io.FileOutputStream;
import java.sql.*;
import org.apache.crimson.tree.XmlDocument;
import org.w3c.dom.*;


public class DB2Mitarbeiter {


public static void main(String[] args) {
String url = "jdbc:db2://127.0.0.1:50000/SAMPLE";
String SQLString=null;
String lastDep = null;
Element emp = null;
Node lastname, firstname = null;
XmlDocument document = new XmlDocument();
Element dep = document.createElement("department");
Node company = document.createElement("company");
ResultSet rs = null;
// Load Type4 DB Driver
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException");
e.printStackTrace();
}
System.out.println("DB Type 4 Driver loaded.");
// DB connect with url,login and pass
try {
FileOutputStream fo = new FileOutputStream("test.xml");
Connection con = DriverManager.getConnection(url,"username","password");
Statement stmt = con.createStatement();
System.out.println("DB connection established.");
SQLString = "SELECT * FROM DEPARTMENT,EMPLOYEE WHERE EMPLOYEE.WORKDEPT = DEPARTMENT.DEPTNO order by DEPARTMENT.DEPTNO";
//SQLString = "SELECT * FROM DEPARTMENT LEFT JOIN EMPLOYEE ON EMPLOYEE.WORKDEPT = DEPARTMENT.DEPTNO";
rs = stmt.executeQuery(SQLString);
while(rs.next()){
//System.out.println("rs: " + rs.getString("DEPTNO") + " - lastdep: " + lastDep);
if (lastDep == null) {
dep.setAttribute("depid",rs.getString("DEPTNO"));
lastDep = rs.getString("DEPTNO");
emp = document.createElement("employee");
emp.setAttribute("empid", rs.getString("EMPNO"));
lastname = emp.appendChild(document.createElement("lastname"));
lastname.appendChild(document.createTextNode(rs.getString("LASTNAME")));
firstname = emp.appendChild(document.createElement("firstname"));
firstname.appendChild(document.createTextNode(rs.getString("FIRSTNME")));
dep.appendChild(emp);
}
else if (lastDep.equals(rs.getString("DEPTNO"))) {
emp = document.createElement("employee");
emp.setAttribute("empid", rs.getString("EMPNO"));
lastname = emp.appendChild(document.createElement("lastname"));
lastname.appendChild(document.createTextNode(rs.getString("LASTNAME")));
firstname = emp.appendChild(document.createElement("firstname"));
firstname.appendChild(document.createTextNode(rs.getString("FIRSTNME")));
dep.appendChild(emp);
}
else {
company.appendChild(dep);
dep = document.createElement("department");
dep.setAttribute("depid",rs.getString("DEPTNO"));
emp = document.createElement("employee");
emp.setAttribute("empid", rs.getString("EMPNO"));
lastname = emp.appendChild(document.createElement("lastname"));
lastname.appendChild(document.createTextNode(rs.getString("LASTNAME")));
firstname = emp.appendChild(document.createElement("firstname"));
firstname.appendChild(document.createTextNode(rs.getString("FIRSTNME")));
dep.appendChild(emp);
}
lastDep = rs.getString("DEPTNO");
}
document.appendChild(company);
document.write(fo);
fo.close();
} catch (Exception e1) {
System.out.println("Fehler");
e1.printStackTrace();
}
}
}