Skip to content

Commit 61256d0

Browse files
author
Jon Mountjoy
committed
Merge pull request heroku#11 from heroku/jkutner/heroku-jdbc
Added heroku-jdbc for each database url parsing
2 parents b9a1f78 + 9f6db72 commit 61256d0

2 files changed

Lines changed: 9 additions & 16 deletions

File tree

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
<artifactId>postgresql</artifactId>
2323
<version>9.4-1201-jdbc4</version>
2424
</dependency>
25+
<dependency>
26+
<groupId>com.heroku.sdk</groupId>
27+
<artifactId>heroku-jdbc</artifactId>
28+
<version>0.1.0</version>
29+
</dependency>
2530
</dependencies>
2631
<build>
2732
<plugins>

src/main/java/Main.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import spark.ModelAndView;
1212
import static spark.Spark.get;
1313

14+
import com.heroku.sdk.jdbc.DatabaseUrl;
15+
1416
public class Main {
1517

1618
public static void main(String[] args) {
@@ -31,7 +33,7 @@ public static void main(String[] args) {
3133
Connection connection = null;
3234
Map<String, Object> attributes = new HashMap<>();
3335
try {
34-
connection = getConnection();
36+
connection = DatabaseUrl.extract().getConnection();
3537

3638
Statement stmt = connection.createStatement();
3739
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)");
@@ -51,22 +53,8 @@ public static void main(String[] args) {
5153
} finally {
5254
if (connection != null) try{connection.close();} catch(SQLException e){}
5355
}
54-
}, new FreeMarkerEngine());
55-
56-
}
57-
58-
private static Connection getConnection() throws URISyntaxException, SQLException {
59-
URI dbUri = new URI(System.getenv("DATABASE_URL"));
60-
int port = dbUri.getPort();
61-
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + ":" + port + dbUri.getPath();
56+
}, new FreeMarkerEngine());
6257

63-
if (dbUri.getUserInfo() != null) {
64-
String username = dbUri.getUserInfo().split(":")[0];
65-
String password = dbUri.getUserInfo().split(":")[1];
66-
return DriverManager.getConnection(dbUrl, username, password);
67-
} else {
68-
return DriverManager.getConnection(dbUrl);
69-
}
7058
}
7159

7260
}

0 commit comments

Comments
 (0)