Skip to content

Commit

Permalink
db support
Browse files Browse the repository at this point in the history
  • Loading branch information
automan committed Jun 5, 2012
1 parent d26be4c commit 3d9211a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.0-801.jdbc4</version>
</dependency>
</dependencies>
<build>
<finalName>embeddedTomcatSample</finalName>
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/taobao/fario/server/db/test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
*
*/
package com.taobao.fario.server.db;

import java.net.URI;
import java.net.URISyntaxException;
import java.sql.*;

/**
* @author taichan
*
*/
public class test {
public static void main(String[] args) throws Exception {

Connection connection = getConnection();

Statement stmt = connection.createStatement();
stmt.executeUpdate("DROP TABLE IF EXISTS ticks");
stmt.executeUpdate("CREATE TABLE ticks (tick timestamp)");
stmt.executeUpdate("INSERT INTO ticks VALUES (now())");
ResultSet rs = stmt.executeQuery("SELECT name FROM \"user\"");
while (rs.next()) {
System.out.println("Read from DB: " + rs.getString("name"));
}
}

private static Connection getConnection() throws URISyntaxException,
SQLException {
URI dbUri = new URI(
"postgres://postgres:lee@localhost:5432/fario");

String username = dbUri.getUserInfo().split(":")[0];
String password = dbUri.getUserInfo().split(":")[1];
String dbUrl = "jdbc:postgresql://" + dbUri.getHost() + dbUri.getPath();

return DriverManager.getConnection(dbUrl, username, password);
}
}

0 comments on commit 3d9211a

Please sign in to comment.