Archive

Archive for the ‘Coding’ Category

pl/sql splitting strings/varchar by delimiter

January 11th, 2008 Wytze No comments

I created the following function for oracle with pl/sql so Strings/varchar items can be split and put into a varray.

CREATE OR REPLACE TYPE my_array IS varray(1000) OF VARCHAR2(255);
 
CREATE OR REPLACE FUNCTION my_split(p_string IN VARCHAR2, p_delim IN VARCHAR2)
RETURN my_array
AS
    p_last_index NUMBER := 1;
    p_current_index NUMBER := 1;
 
    p_array_pointer NUMBER := 1;
    p_items my_array := my_array();
    p_item VARCHAR2(255);
BEGIN
     -- get index of split character
     p_last_index := INSTR(p_string,p_delim,p_current_index,1);
 
     -- while split characters are found
     -- add it to the varray
     WHILE( p_last_index > 0 ) LOOP
    	-- get first item
        p_item := SUBSTR(p_string, p_current_index, (p_last_index - p_current_index));
 
	-- put item in varray
        p_items.extend;
        p_items(p_array_pointer) := p_item;
        p_array_pointer := p_array_pointer + 1;
 
    	-- update indexes
        p_current_index := p_last_index + LENGTH(p_delim);
        p_last_index := INSTR(p_string,p_delim,p_current_index,1);
    END LOOP;
 
    -- get last item
    p_item := SUBSTR(p_string, p_current_index);
    -- put item in varray
    p_items.extend;
    p_items(p_array_pointer) := p_item;
 
    --dbms_output.put_line(substr('Value of p_receiver='||p_receiver,1,255));
 
    /*
    Example of how to loop through the items:
 
    for a_index in 1..p_items.count loop
    	dbms_output.put_line(substr('Value of array('||a_index||'):'||p_items(a_index),1,255));
    end loop;
    */
 
    RETURN p_items;
END;

Small example how you can use this now:

DECLARE
	test my_array;
	input VARCHAR2(255) := 'a;b;c';
	delim VARCHAR2(1) := ';';
BEGIN
	test := my_split(input,delim);
 
	FOR a_index IN 1..test.COUNT LOOP
		DBMS_OUTPUT.put_line(SUBSTR('Value of array('||a_index||'):'||test(a_index),1,255));
	END LOOP;
END;
Categories: Coding Tags:

BlazeDS

December 18th, 2007 Wytze No comments

Finally it is here! Adobe released BlazeDS. It will allow you to make calls in AMF3 format to your Java server. Have a look at the site for specs and samples.

Categories: Coding Tags: ,

From bytes to hex

November 27th, 2007 Wytze No comments

When creating checksums or generating keys it might be helpful sometimes to represent the key/checksum as a hexadecimal string value. The key is most of the times a byte array which will need conversion. You can convert it with the following code (java 1.5+)

	public String toHexString(byte[] b){
		StringBuilder sb = new StringBuilder();
		for( byte hexByte : b ){
			sb.append(Integer.toHexString(hexByte & 0xFF).toUpperCase());
		}
		return sb.toString();
	}

By using the 0xFF with a bitwise AND operator we ensure that the byte to be converted is unsigned.

Categories: Coding Tags:

Appfuse 2 helps lazy people being lazy

November 13th, 2007 Wytze No comments

AppFuseOk, let’s agree to this one: You are lazy. Some things in life help you being lazy. Why develop your own maven templates for instance when the appfuse guys already built them? That’s right, you’re nuts if you build your own.

Just have a look at there site to see what the possibilities are: appfuse

Categories: Coding Tags:

MoBlock and Hamachi

October 12th, 2007 Wytze No comments

After installing moblock (a p2p guardian program) I found that my hamachi was no longer working. :(

To fix this I had to modify the moblock.conf file in the /etc/moblock directory.
I did so by specifying a network address with a netmask.

Hamachi’s ip-range starts with a 5. So the address would be 5.0.0.0
The netmask is 255.0.0.0 to allow all hamachi ip’s to connect. Because 255.0.0.0 means we have 8 bits on 1. The line becomes: 5.0.0.0/8

Add this line to the IP_TCP_IN line for example and restart moblock with moblock-control restart. After I did this I was able to reconnect with my linux server from hamachi. Hurray!

[update]Ok, maybe you also want to add LOGMEIN to your ignore block list. :S
Do so by changing the IP_REMOVE line into IP_REMOVE=”LOGMEIN”.
Do a moblock-control reload after this to reload the new configuration[/update]

Categories: Coding, General Tags:

Eclipse search and destroy

October 11th, 2007 Wytze No comments

Eclipse (Popular IDE) offers the possibility to do ‘search and replace’ with regular expressions. Today this came in quite handy as I had to change a large amount of sql insert statements into sql update statements.

Although loading the file of 4MB took quite some time (probably because of the syntax highlighting eclipse was trying to apply) the file could be loaded. After that I opened the search & replace option from the menu and started creating something that looked like a regular expression.

Here is one line of the original input:

INSERT INTO MEDIA (MEDIA_ID,UPLOADFILE_FILENAME,UPLOADFILE_FILESIZE,UPLOADFILE_HEIGHT,UPLOADFILE_WIDTH)
VALUES (1579,'1579.jpg',1095082,2575,4075);

This is the regex I came up with:

.*?\(.*?\((\d+),(.*?),(\d+),(\d+),(\d+)\)\;

I had it replaced with:

update MEDIA
set UPLOADFILE_FILENAME=$2, UPLOADFILE_FILESIZE=$3,UPLOADFILE_HEIGHT=$4,UPLOADFILE_WIDTH=$5
where MEDIA_ID=$1;

Worked like a charm. The lines now looked like this:

UPDATE MEDIA
SET UPLOADFILE_FILENAME='1579.jpg', UPLOADFILE_FILESIZE=1095082,UPLOADFILE_HEIGHT=2575,UPLOADFILE_WIDTH=4075
WHERE MEDIA_ID=1579;

Seems like abracadabra? Well it is, since the outcome was magical. :)

Categories: Coding Tags:

java.sql.SQLException: No suitable driver

October 10th, 2007 Wytze 2 comments

After trying to set up Maven2 with Spring 2, Hibernate 3 (with annotations) I tried to configure the maven2 hibernate3 plugin so the schema could be exported for manual creation.

Then it hit me, a:

java.sql.SQLException: No suitable driver

I didn’t get this one before when using XDoclet2 to generate the hbm.xml files and schema-export for me. I did some research on the net and found out that the calling program is verifying the jdbc url against all available drivers. When none of the drivers accepts the jdbc url the above error is thrown. It’s good to know that the jdbc drivers are loaded dynamically.

Knowing this solved the puzzle: Either the jdbc url was not ok or the driver was not loaded at the point of the schema-export. The latter was the case. By adding the jdbc jar to the classpath I could luckily fix this issue. Maybe I should have done so in the first place. Well at least I learned something from all this and I hope you will too by reading this.

Here is a small snippet of my pom.xml for maven2. The db-properties are read from the META-INF/persistence.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<!-- Hibernate3 Plugin, used for creating SQL statements for table creation -->
<plugin>
	<groupid>org.codehaus.mojo</groupid>
	<artifactid>hibernate3-maven-plugin</artifactid>
	<version>2.0-SNAPSHOT</version>
	<configuration>
		<executions>
			<execution>
				<phase>process-classes</phase>
				<goals>
					<goal>hbm2ddl</goal>
				</goals>
			</execution>
		</executions>
		<components>
			<component>
				<name>hbm2ddl</name>
			</component>
		</components>
		<componentproperties>
			<implementation>jpaconfiguration</implementation>
			<!-- Output in pretty format -->
			<format>true</format>
			<!-- Do not create drop statements -->
			<drop>false</drop>
			<!-- Generate script -->
			<text>true</text>
			<!-- Do not output sql to screen -->
			<quiet>false</quiet>
			<!-- Do not export tables into database -->
			<export>false</export>
			<outputfilename>
				schema-export.sql
			</outputfilename>
		</componentproperties>
	</configuration>
	<dependencies>
		<!-- Necessary to be able to connect to db -->
		<dependency>
			<groupid>local</groupid>
			<artifactid>oracle-classes</artifactid>
			<version>1.2</version>
		</dependency>
	</dependencies>
</plugin>
Categories: Coding Tags:

Getting it up and running

September 10th, 2007 Wytze No comments

Last week I finally received my new hardware. Eager to get it all running I started with the base install of Debian (4.0).

First I installed the subversion server as follows:

  1. run ‘apt-get install subversion’
  2. run ‘svnadmin create /path/to/repository’
  3. update the config files in /path/to/repository/conf
  4. create a start script for the svnserve daemon, containing something like: ‘svnserve -d -r /path/to/repository/’
  5. wow it works! :P

Installing Apache2, PHP5 and MySQL 5 was also really easy.

  1. run ‘apt-get install apache2 php5 mysql-server-5.0 libapache2-mod-php5 php5-mysql’

Installing Tomcat was a bit harder. I first installed lynx so I could download jdk 1.5 from java.sun.com. After downloading it I installed it into /opt. After that I created a symbolic link from /opt/java pointing to the jdk 1.5 folder. After that setting the JAVA_HOME environment variable to it and exporting JAVA_HOME/bin to the path.

After that I also downloaded the Tomcat 5.5 tar.gz file and untarred it into the /opt directory on the harddrive. After that I created a similar symbolic link to /opt/tomcat referring to the tomcat install location. After that I tested the install and it worked ok. Because I didn’t want to run Tomcat as root I created a tomcat user and modified the startup script to run as the tomcat user using the ‘su -p tomcat’ command.

Categories: Coding, Hardware Tags:

Google-Guice

March 14th, 2007 Wytze No comments

Zo weer ‘s wat nieuws in AOP land. ;)

http://code.google.com/p/google-guice/

Binnenkort maar eens een java applicatie mee maken. Voorlopig klinkt het erg interessant. Ik ben alleen bang dat het niet zo uitgebreid is als het Spring framework. En dan waarschijnlijk net de onderdelen mist die ik in Spring zo ben gaan waarderen.

Categories: Coding Tags:

Maven 2 – Een korte introductie

February 26th, 2007 Wytze No comments

Follow this link for the list of quick guides:
Quick Guides

Ik heb een korte howto/inleiding/tutorial geschreven voor Maven 2. Ik zal hem hier neerzetten. Maven 2 – Een korte introductie

Categories: Coding Tags: