Benny's Blog
4. März 2012

If you receive an error that is similar to this:

java.sql.SQLException: Value ’0000-00-00 00:00:00′ can not be represented as java.sql.Date

Then you should check your database connection. If your connection url looks like jdbc:mysql://localhost/my_database then you should try jdbc:mysql://localhost/my_database?zeroDateTimeBehavior=convertToNull.

Other options are:
jdbc:mysql://localhost/my_database?zeroDateTimeBehavior=round
jdbc:mysql://localhost/my_database?zeroDateTimeBehavior=exception

The explanation can be found in “handling DATETIME values“.

4. März 2012

In the beginning of my study time I’ve programmed a MySQL connection in Java for a music collection. After 2 years now I have made a remake of this code to show how to use the Java Database Connectivity (JDBC). All you need is mysql-connector-java-5.0.8-bin.jar.

I’ve written a small sample for the connection with a WordPress database.

…weiterlesen

4. März 2012

Code Sample:

public static void main(String[] args)
{
  String property = System.getProperty("my.text");
  System.out.println(property);
}

Execution:

java -Dmy.text="Hello World." -jar /home/user/application.jar

Note: With java -D you can set every property you like except the reserved ones, listed in Java System Properties.

You can set multiple properties with the following command:

java -Dmy.text="Hello World." -Dmy.ide="NetBeans IDE" -Dmy.browser="Mozilla Firefox" -jar /home/user/application.jar
3. März 2012

Code sample:

public class CharacterCounter
{
  public static int countOccurrences(String find, String string)
  {
    int count = 0;
    int indexOf = 0;
 
    while (indexOf > -1)
    {
      indexOf = string.indexOf(find, indexOf + 1);
      if (indexOf > -1)
        count++;
    }
 
    return count;
  }
}

Method call:

int occurrences = CharacterCounter.countOccurrences("l", "Hello World.");
System.out.println(occurrences); // 3

You can also count the occurrences of characters in a string by using the Apache commons lang library with the following one-liner:

int count = StringUtils.countMatches("a.b.c.d", ".");

If you are using the Sping Framework then you can do:

int occurance = StringUtils.countOccurrencesOf("a.b.c.d", ".");

If you want to be really smart (and don’t want to use a loop), then you can also use this one here:

int count = string.length() - string.replace(find, "").length();
1. März 2012

Code sample:

  @Test(expected = NoMatchFoundException.class)
  public void testGetRenamedFilePathWorstCase() throws NoMatchFoundException
  {
    String logEntry = "   A /my/folder/with/a/file.txt";
    RegExMatcher instance = new RegExMatcher();
    String actual = instance.getRenamedFilePath(logEntry);
    fail("Should have raised a NoMatchFoundException.");
  }
28. Februar 2012

How to install node.js (latest version)

apt-get install g++ curl libssl-dev apache2-utils git-core -y
cd /etc
git clone git://github.com/joyent/node
cd /etc/node
./configure
make
sudo make install

How to install node.js (stable version)

apt-get install g++ curl libssl-dev apache2-utils git-core -y
cd /tmp
wget http://nodejs.org/dist/v0.6.11/node-v0.6.11.tar.gz
tar xvf node-v0.6.11.tar.gz
mv /tmp/node-v0.6.11 /etc
cd /etc/node-v0.6.11
./configure
make
sudo make install
rm /tmp/node-v0.6.11.tar.gz

You can check the installed version with node --version.

node.js package manager

If you want to use a package manager for node.js (like npm), then you can get it with:

curl http://npmjs.org/install.sh | sh

To use the package manager, just go into your node.js webproject and execute something like:

npm install websocket.io

This command will load the desired dependencies (like websocket.io) into a directory called “node_modules” within your project’s folder.

Uninstalling node.js

To uninstall node.js, just go to the folder where you installed node.js (e.g. /tmp/node-v0.6.11) and use the following command:

make uninstall