27 July, 2009

JDBC: Postgres enums and prepared statements

There is simple way how to map Java enums to PostgreSQL enums via JDBC's prepared statements.

For example we have these PostgreSQL enum and table definitions:

CREATE TYPE log_level AS ENUM ('INFO', 'WARN', 'ERROR');

CREATE TABLE logs (
id SERIAL PRIMARY KEY,
message VARCHAR(255) NOT NULL,
level log_level NOT NULL,
);


And Java enum:

public enum LogLevel {
INFO, WARN, ERROR
}


This form of prepared statement should work:

LogLevel logLevel = LogLevel.INFO;
PreparedStatement sql = conn.prepareStatement("INSERT INTO logs (message, log_level) VALUES (?, ?::log_level)");
sql.setString(1, "Hello world");
//sql.setString(2, "INFO");
sql.setString(2, logLevel.name());

28 February, 2009

Get browser language via JavaScript

This is simple cross-browser solution:

var lang = (navigator.language || navigator.systemLanguage || navigator.userLanguage || 'en').substr(0, 2).toLowerCase();


And usage example:

var lang = (navigator.language || navigator.systemLanguage || navigator.userLanguage || 'en').substr(0, 2).toLowerCase();
if (lang == 'ru') {
    document.write('Specific Russian text');
} else {
    document.write('Common English text');
}

24 February, 2009

PHP-reference

I'm happy to introduce one more my project: PHP-reference.

This is the interactive engine for PHP documentation: the client side is implemented with ExtJS/JavaScript, and PHP for server side.
It uses original PHP documentation (processed via regexps and colored with CSS styles) as a data source.

I've tried to create it as usable as possible... any requests, ideas welcome!

18 February, 2009

TOAD connection problem under Windows XP x64

I've catch this problem with my installation of TOAD (and with PLSQL Developer) under Windows XP 64-bit edition:
"ORA-12154: TNS:could not resolve the connect identifier specified" (but under Windows XP 32-bit all OK).

And here is the simplest solution:
run TOAD via command line using short path: C:\PROGRA~2\QUESTS~1\TOAD\TOAD.exe
instead of long: C:\Program Files (x86)\Quest Software\TOAD\TOAD.exe

Unfortunately Windows doesn't allow to change desktop's shortcut from long mode to short (Windows always expands short paths).

Or... move TOAD from "Program Files (x86)" to "Program Files" directory.

19 January, 2009

Run java with non-system locale

java -Duser.language=en -Duser.region=US JavaClass