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.PreparedStatement;
8 import java.sql.ResultSet;
9 import java.sql.SQLException;
10 import java.util.Properties;
12 import net.jaekl.cfb.db.Sequence;
13 import net.jaekl.cfb.db.Column.Type;
15 public class PostgresqlDriver extends DbDriver {
18 public void load() throws ClassNotFoundException {
19 // This should no longer be necessary, so long as we're using
20 // JDBC 4 (which came out with JDK 6) and an updated driver.
21 // But, it shouldn't hurt, either.
22 Class.forName("org.postgresql.Driver");
26 public Connection connect(String host, int port, String dbName, String user, String pass) throws SQLException {
27 String url = "jdbc:postgresql://" + host + ":" + port + "/" + dbName;
28 Properties props = new Properties();
29 props.setProperty("user", user);
30 props.setProperty("password", pass);
31 //props.setProperty("ssl", "true");
32 return DriverManager.getConnection(url, props);
36 public long nextVal(Connection con, Sequence seq) throws SQLException
38 String sql = " SELECT NEXTVAL(?) ";
40 try (PreparedStatement ps = con.prepareStatement(sql))
42 ps.setString(1, seq.getName());
44 try (ResultSet rs = ps.executeQuery()) {
51 throw new SQLException("No value returned for sequence: " + sql);
56 protected String typeName(Type type) {
57 // Special case: TIMESTAMPTZ stored as INTEGER (milliseconds since the epoch)
58 // Reading a TIMESTAMPTZ back from the DB, and converting it to a java.util.Date,
59 // is fraught with peril. The best way around this is to store the dates in
60 // milliseconds-since-the-epoch (01.01.1970 00:00:00.000 UTC).
61 if (Type.TIMESTAMPTZ.equals(type)) {
65 return type.toString();