Archive for the ‘Databases’ Category
Hello Orange Bus
Last week I got a job in Newcastle
I will step into the position as “Head of development” at Orange Bus in November.
I really look forward to join the talented team at Orange Bus. Have a look at http://www.orangebus.co.uk where you can see some of their work.
Cheat sheets
When talking about cheat sheets, www.ilovejackdaniels.com has a list of a few good cheat sheets. I’ve actually printed some of them at work and taped them to the wall next to my computer.
Thanks to Dave Child for doing the work of putting together these sheets.
PostgreSQL cheat sheet
I often have a blank hole or two when it comes to the basic PostgreSQL commands, and have to do a look up in the documentation, Kitt Hodsden has posted a nice little cheat sheet with the most common basic postgreSQL commands.
My first introduction to java and databases
The other day I did a complete reconfiguration of java on my system. Tonight I started to play around with java and postgresql. I installed PostgreSQL 8.2 (yes I know 8.3 is out there), and downloaded the jdbc driver for PosgreSQL.
PostgreSQL
After installing PostgreSQL, you have to add a user to the authorization file pg_hba.conf located in /etc/postgresql/8.x/main on a ubuntu system, remember to restart the postgreSQL server after editing pg_hba.conf. When that is done add the same user to the postgreSQL server either by using postgreSQL’s createuser function or by logging into the postgreSQL server and doing a SQL CREATE USER, I did the latter one.
-
CREATE USER kristian WITH PASSWORD 'xxxxxxxx' CREATEDB;
This enables me to do a regular access of the database with:
-
kristian@Saturn:~$ psql -U kristian crawler
-
Welcome to psql 8.2.7, the PostgreSQL interactive terminal.</code>
-
-
Type: \copyright for distribution terms
-
\h for help with SQL commands
-
\? for help with psql commands
-
\g or terminate with semicolon to execute query
-
\q to quit
-
-
crawler=#
My table:
-
crawler=# \d document;
-
TABLE "public.document"
-
COLUMN | Type | Modifiers
-
—————+—————————–+————————
-
id | integer | NOT NULL DEFAULT NEXTVAL('document_id_seq'::regclass)
-
url | text | NOT NULL
-
document | text | NOT NULL
-
ts_downloaded | timestamp without time zone | DEFAULT now()
Data in the table:
-
crawler=# select * from document;
-
id | url | document | ts_downloaded
-
—-+———————–+———-+—————————-
-
1 | http://www.klunde.net | jalla | 2008-04-19 21:11:11.805482
Java
First thing to get the postgreSQL jdbc driver to work is to add it to the java classpath, and of course copy the actual jar file to its correct location, for instance /usr/lib/jvm/java-6-sun-1.6.0.03/jre/ext/lib/which is my java library path.
My classpath:
-
/usr/lib/jvm/java-6-sun-1.6.0.03/lib:/usr/lib/jvm/java-6-sun-1.6.0.03/jre/ext/lib/postgresql-8.3-603.jdbc4.jar:.
Now its about time to write some actual code:
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.PreparedStatement;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
import java.sql.Statement; </code>
-
-
public class DatabaseTest
-
{
-
private Connection connection = null;
-
-
private static final String USERNAME = "XXXXX";
-
private static final String PASSWORD = "XXXXX";
-
private static final String URL = "jdbc:postgresql:";
-
private static final String DATABASE = "crawler";
-
-
public DatabaseTest()
-
{
-
try
-
{
-
connection = DriverManager.getConnection(URL + DATABASE, USERNAME, PASSWORD);
-
ResultSet rs = doQuery("SELECT * FROM document");
-
-
rs.next();
-
System.out.println(rs.getString(2));
-
}
-
catch (SQLException e)
-
{
-
System.out.println("Database Exception!");
-
System.out.println(e.toString());
-
e.printStackTrace();
-
}
-
-
}
-
-
public ResultSet doQuery(String sql)
-
{
-
ResultSet rs;
-
try
-
{
-
Statement stmt = connection.createStatement();
-
rs = stmt.executeQuery(sql);
-
return rs;
-
}
-
catch (SQLException e) {
-
e.printStackTrace();
-
return null;
-
}
-
}
-
}
Trying to execute my script:
-
kristian@Saturn:~/workspace/crawler$ javac DatabaseTest.java
-
kristian@Saturn:~/workspace/crawler$ java -Djdbc.drivers=org.postgresql.Driver DatabaseTest
-
http://www.klunde.net
It works
The installation and initial setup went pretty much by the book, but as a PHP developer I must say that I’m a little bit disappointed, JDBC cannot compare itself to the PHP PDO extension yet. But then again, maybe I just need to get to know the JDBC library better. Anyhow it was fun writing this little java app and refreshing some of my java skills.