Archive for October, 2009

MySQL and Java common problem

Have you ever received the following error while using Java and MySQL:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘????????????????’ at line 1

This is a common error when you first set up Java and MySQL to work together. To correct it simply add the following lines of code to your my.cnf in the section [mysqld]

collation_server=utf8_general_ci
character_set_server=utf8

Self Replicating Code

For a class recently the teacher challenged us to write a code snip-bit that would be self replicating. By that I mean write a program that when executed would write out its own source code. This is a bit harder than you’d initially think it to be. My first attempt tried to store the source code within the program when execute it would output what was stored within. However this creates a chick and egg problem. The source code contains the source code. Something that at first stumped me. Then I remembered Stringification in the C preprocessor and I came up with this:

#include “stdio.h”
#define M(Code) main(){FILE* fp = fopen(“source.c”, “w”);fprintf(fp,”#include \”stdio.h\”\n#define M(Code) %s\nM(%s)\n”,#Code,#Code);fclose(fp);}
M(main(){FILE* fp = fopen(“source.c”, “w”);fprintf(fp,”#include \”stdio.h\”\n#define M(Code) %s\nM(%s)\n”,#Code,#Code);fclose(fp);})

This is a pretty cute program if I can say so myself. When its ran it will create a file called source.c that will be exactly the same as the code above. You could even recompile source.c and it will create another source.c. You should attempt to do this on your own to see if you can come up with other cool ways of doing it.

Download: SelfRep.c

Dual booting Linux and Windows with linux already installed

I have always been told in order to dual boot Linux and Windows you must first install Windows then Linux. The idea being that Window’s boot loader does not play nicely with Linux like Grub or Lilo does. However it is often easier to install Linux then Windows; like with the up coming Windows 7 release, Linux users may want to try out Windows 7 without having to reinstall their Linux installs. I cam across a great tutorial on how to do just this.