Skip to content

Commit 00285b1

Browse files
committed
Added heroku-jdbc for each database url parsing
1 parent b9a1f78 commit 00285b1

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

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: 3 additions & 15 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)");
@@ -55,18 +57,4 @@ public static void main(String[] args) {
5557

5658
}
5759

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();
62-
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-
}
70-
}
71-
7260
}

0 commit comments

Comments
 (0)