1 package net.jaekl.cfb.db.driver;
3 // Copyright (C) 2015 Christian Jaekl
5 import java.sql.Connection;
6 import java.sql.DriverManager;
7 import java.sql.SQLException;
8 import java.util.Properties;
10 import net.jaekl.cfb.db.Sequence;
11 import net.jaekl.cfb.db.Column.Type;
13 public class PostgresqlDriver extends DbDriver {
16 public void load() throws ClassNotFoundException {
17 // This should no longer be necessary, so long as we're using
18 // JDBC 4 (which came out with JDK 6) and an updated driver.
19 // But, it shouldn't hurt, either.
20 Class.forName("org.postgresql.Driver");
24 public Connection connect(String host, int port, String dbName, String user, String pass) throws SQLException {
25 String url = "jdbc:postgresql://" + host + ":" + port + "/" + dbName;
26 Properties props = new Properties();
27 props.setProperty("user", user);
28 props.setProperty("password", pass);
29 //props.setProperty("ssl", "true");
30 return DriverManager.getConnection(url, props);
34 public String nextValSql(Sequence seq)
36 return " SELECT NEXTVAL('" + seq.getName() + "') ";
40 protected String typeName(Type type) {
41 // Special case: TIMESTAMPTZ stored as INTEGER (milliseconds since the epoch)
42 // Reading a TIMESTAMPTZ back from the DB, and converting it to a java.util.Date,
43 // is fraught with peril. The best way around this is to store the dates in
44 // milliseconds-since-the-epoch (01.01.1970 00:00:00.000 UTC).
45 if (Type.TIMESTAMPTZ.equals(type)) {
49 return type.toString();