Archive for the ‘Coding’ Category

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();
}
}
}

Using ImageMagick with VBScript

Wednesday, August 17th, 2005

First first ever lines of VB Script code. This script generates thumbnails out of all files in a given directory and saves them to another. Additionally it creates a resized version of the images to fit into a certain max dimension. Note: ImageMagick needs to be installed in order to run this script.
Image Magick resizing test with VBScript

Web Design patterns

Wednesday, July 20th, 2005

Martijn van Welie collected some nice tipps and tricks concerning design patterns for different types of web sites from artists sites over eCommerce sites to web-based applications. In his artcles he points out the differences one needs to consider and even shows some best practices for page elements like login boxes shopping carts etc. . A very useful resource while planning a new project and a nice read in general.
Check it out at http://www.welie.com/patterns/.

pear package installer

Sunday, July 10th, 2005

As I’m always forgetting this quite easy to remember switch a little mental note: --alldeps makes pear install command resolving all needed dependencies automatically… dumb me

LiveUser Beta 0.16.0 released

Sunday, June 26th, 2005

I just realised the guys from LiveUser released Beta 0.16.0 this week. I hope there are not that many changes as I just managed to integrate this Pear package into one of my running projects.
LiveUser is a Pear package for user management including Auth and Permission handling functionality. Unfortunately allthough being a quite a nice piece of code it is aswell one of the least well documented Pear projects I’ve ever seen. As I have to check out the new version anyway I’ll try to contribute some tips on how to get started with this package here during the next time.
Check out LiveUser at the Pear home: http://pear.php.net/package/liveuser

CSS style problem with Windows XP themes

Thursday, June 23rd, 2005

Ok, I knew that Internet Explorer is highly integrated into the OS, but this issue was new to me…
Smooth 3 column layout, a navigation to the left, some teasers and a searchbox to the right. All three cols have a fixed width. The content area is filled up with a medium complex form. I used fieldset and legend attributes but I don’t want to have a border line all around the fieldset. So I give the fieldset a border:none and let the legend be underlined by a 1px div which has the full with of the parent div container. “All fine” I think and check it in into cvs. Some hours later my co-worker:

- “Hey, this form page you made … kinda messy, eh?”
- “Hu?”
- “The whole teaser area isn’t there where it is supposed to be.”
- “No it isn’t, look at my screen!”
- “Yes it is, look at my screen!”

After some hours of code comparison and several tests we finally figured out the problem. We both work with almost the same notebook, same OS (Windows XP with identical Service Pack and patch status) but different screen resolution (which couldn’t be the problem as we have a fixed layout here) and (here’s the crux!) different windows themes. I’m on “Windows Classic” theme and he has the standard Win XP theme. Funnily the Win XP theme adds a padding to the fieldset element (!!!) so all we had to do was adding a padding:0 to the fieldset css. To be honest, there are much more important things I am willing to waste my time with…
So as a conclusion for you WebDevs out there: Always test your layout on different Windows tehemes aswell ;)
(Just to make another line on the Mozilla vs. IE list – Mozilla just behaved as expected and all was fine before and afterwards…)

CSS styled submit buttons

Tuesday, June 21st, 2005

Yay, first Coding post!
Needed a neat little submit button design today. Here’s what I ended up with:

As I wanted to have an rollover effect but IE doesn’t support usage of :hover with other elements than the a-tag, I decided to use a simple 3-liner for “on the fly (hover)” style switching:

function changeStyle(id, newClass) {
identity=document.getElementById(id);
identity.className=newClass;
}

Very simple, but quite useful for several circumstances. id is the unique id of the element I want to switch the style of, newClass is the style I want to switch to. Only disadvantage is the non-functional hover effect with disabled JS in Internet Explorer.

Here’s the CSS code:


input.fancysubmit {padding-left:3px; border:0; color:#fff; background-color:#abcdef; cursor:pointer; font-weight:bold; background-image: url(./img/pfeil_button_white.gif); background-repeat:no-repeat; background-position:left center;}
html>body input.fancysubmit {padding-left:12px}


input.fancysubmit:hover {padding-left:3px; border:0; background-color:#abefcd; color:#fff; cursor:pointer; font-weight:bold; background-image: url(./img/pfeil_button_white.gif); background-repeat:no-repeat; background-position:left center;}
html>body input.fancysubmit:hover {padding-left:12px}


/* workaround for wacky IE CSS2 implementation */
input.fancysubmitmouseover {padding-left:3px; border:0; background-color:#abefcd; color:#fff; cursor:pointer; font-weight:bold; background-image: url(./img/pfeil_button_white.gif); background-repeat:no-repeat; background-position:left center;}
html>body input.fancysubmitmouseover {padding-left:12px}

As you can see, I’ve added an background image. It is just a tiny arrow in this case, but there are several other possibilities for using the background. You can download my all-in-one HTML file and the image here:

Fancy button HTML source code

For those of you not that into browser hacks, the html>body makes IE ignoring the following style definitions. A good way to correct display problems concerning different box models of the browsers.