17 Dec 2005 orique   » (Observer)

Getting usergroup in PostgreSQL
I'm building with a classmate a console application (written in Java) which connects to a PostgreSQL database.

Eventually we wanted to know in which user group was the user trying to connect to the database. Looking for some information about this, we found there's a table called pg_group where for each group we have a field with users' id (grolist). Then the problem was to know where a concrete id was present. We thought grolist was a plain string, so we had to code a StringTokenizer to get the id's, and then test if it was the one wanted. That resulted in an ugly, unefficient code which I wanted to rewrite.

Fortunately, we misunderstood the type of data stored in the field grolist. It's an array of id's instead a string. So, and after visiting this page :P, I finally could solve our problem like follows:

private static void setGroup() throws SQLException {

ResultSet rs2;
String myGroup,query;

query = "SELECT groname FROM pg_group WHERE "+idUser+" = ANY(grolist);";

rs2 = DBConnection.execQuery(query);
rs2.next();

myGroup = rs2.getString(1);

...
}

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!