| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f48dea1 commit 481fe97
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,6 @@ | |||
| 1 | 1 | # Postgre SQL sample for Google App Engine J8 | |
| 2 | 2 | ||
| 3 | - <a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=appengine-java8/postgres/README.md"> | ||
| 3 | + <a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=appengine-java8/cloudsql-postgres/README.md"> | ||
| 4 | 4 | <img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a> | |
| 5 | 5 | ||
| 6 | 6 | This sample demonstrates how to use [PostgreSql](https://cloud.google.com/sql/) on Google App | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,7 +48,6 @@ | |||
| 48 | 48 | <!-- [END properties] --> | |
| 49 | 49 | ||
| 50 | 50 | <dependencies> | |
| 51 | - <!-- Parent POM defines ${appengine.sdk.version} (updates frequently). --> | ||
| 52 | 51 | <dependency> | |
| 53 | 52 | <groupId>com.google.appengine</groupId> | |
| 54 | 53 | <artifactId>appengine-api-1.0-sdk</artifactId> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,12 +17,8 @@ | |||
| 17 | 17 | package com.example.appengine.postgresql; | |
| 18 | 18 | ||
| 19 | 19 | import com.google.common.base.Stopwatch; | |
| 20 | - | ||
| 21 | 20 | import java.io.IOException; | |
| 22 | 21 | import java.io.PrintWriter; | |
| 23 | - import java.net.Inet4Address; | ||
| 24 | - import java.net.Inet6Address; | ||
| 25 | - import java.net.InetAddress; | ||
| 26 | 22 | import java.sql.Connection; | |
| 27 | 23 | import java.sql.DriverManager; | |
| 28 | 24 | import java.sql.PreparedStatement; | |
@@ -31,7 +27,6 @@ | |||
| 31 | 27 | import java.sql.Timestamp; | |
| 32 | 28 | import java.util.Date; | |
| 33 | 29 | import java.util.concurrent.TimeUnit; | |
| 34 | - | ||
| 35 | 30 | import javax.servlet.ServletException; | |
| 36 | 31 | import javax.servlet.annotation.WebServlet; | |
| 37 | 32 | import javax.servlet.http.HttpServlet; | |
@@ -42,7 +37,7 @@ | |||
| 42 | 37 | @SuppressWarnings("serial") | |
| 43 | 38 | // With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required. | |
| 44 | 39 | @WebServlet(name = "PostgreSQL", | |
| 45 | - description = "PostgreSQL: Write low order IP address to PostgreSQL", | ||
| 40 | + description = "PostgreSQL: Write timestamps of visitors to PostgreSQL", | ||
| 46 | 41 | urlPatterns = "/postgresql") | |
| 47 | 42 | public class PostgreSqlServlet extends HttpServlet { | |
| 48 | 43 | ||
@@ -52,11 +47,11 @@ public class PostgreSqlServlet extends HttpServlet { | |||
| 52 | 47 | public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, | |
| 53 | 48 | ServletException { | |
| 54 | 49 | ||
| 55 | - final String createTableSql = "CREATE TABLE IF NOT EXISTS visits ( visit_id SERIAL NOT NULL, " | ||
| 56 | - + "user_ip VARCHAR(46) NOT NULL, ts timestamp NOT NULL, " | ||
| 50 | + final String createTableSql = "CREATE TABLE IF NOT EXISTS visits ( " | ||
| 51 | + + "visit_id SERIAL NOT NULL, ts timestamp NOT NULL, " | ||
| 57 | 52 | + "PRIMARY KEY (visit_id) );"; | |
| 58 | - final String createVisitSql = "INSERT INTO visits (user_ip, ts) VALUES (?, ?);"; | ||
| 59 | - final String selectSql = "SELECT user_ip, ts FROM visits ORDER BY ts DESC " | ||
| 53 | + final String createVisitSql = "INSERT INTO visits (ts) VALUES (?);"; | ||
| 54 | + final String selectSql = "SELECT ts FROM visits ORDER BY ts DESC " | ||
| 60 | 55 | + "LIMIT 10;"; | |
| 61 | 56 | ||
| 62 | 57 | String path = req.getRequestURI(); | |
@@ -67,55 +62,34 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc | |||
| 67 | 62 | PrintWriter out = resp.getWriter(); | |
| 68 | 63 | resp.setContentType("text/plain"); | |
| 69 | 64 | ||
| 70 | - // store only the first two octets of a users ip address | ||
| 71 | - String userIp = req.getRemoteAddr(); | ||
| 72 | - InetAddress address = InetAddress.getByName(userIp); | ||
| 73 | - if (address instanceof Inet6Address) { | ||
| 74 | - // nest indexOf calls to find the second occurrence of a character in a string | ||
| 75 | - // an alternative is to use Apache Commons Lang: StringUtils.ordinalIndexOf() | ||
| 76 | - userIp = userIp.substring(0, userIp.indexOf(":", userIp.indexOf(":") + 1)) + ":*:*:*:*:*:*"; | ||
| 77 | - } else if (address instanceof Inet4Address) { | ||
| 78 | - userIp = userIp.substring(0, userIp.indexOf(".", userIp.indexOf(".") + 1)) + ".*.*"; | ||
| 79 | - } | ||
| 80 | - | ||
| 81 | 65 | Stopwatch stopwatch = Stopwatch.createStarted(); | |
| 82 | 66 | try (PreparedStatement statementCreateVisit = conn.prepareStatement(createVisitSql)) { | |
| 83 | 67 | conn.createStatement().executeUpdate(createTableSql); | |
| 84 | - statementCreateVisit.setString(1, userIp); | ||
| 85 | - statementCreateVisit.setTimestamp(2, new Timestamp(new Date().getTime())); | ||
| 68 | + statementCreateVisit.setTimestamp(1, new Timestamp(new Date().getTime())); | ||
| 86 | 69 | statementCreateVisit.executeUpdate(); | |
| 87 | 70 | ||
| 88 | 71 | try (ResultSet rs = conn.prepareStatement(selectSql).executeQuery()) { | |
| 89 | 72 | stopwatch.stop(); | |
| 90 | 73 | out.print("Last 10 visits:\n"); | |
| 91 | 74 | while (rs.next()) { | |
| 92 | - String savedIp = rs.getString("user_ip"); | ||
| 93 | 75 | String timeStamp = rs.getString("ts"); | |
| 94 | - out.println("Time: " + timeStamp + " Addr: " + savedIp); | ||
| 76 | + out.println("Visited at time: " + timeStamp); | ||
| 95 | 77 | } | |
| 96 | - out.println("Elapsed: " + stopwatch.elapsed(TimeUnit.MILLISECONDS)); | ||
| 97 | 78 | } | |
| 98 | 79 | } catch (SQLException e) { | |
| 99 | 80 | throw new ServletException("SQL error", e); | |
| 100 | 81 | } | |
| 82 | + out.println("Query time (ms):" + stopwatch.elapsed(TimeUnit.MILLISECONDS)); | ||
| 101 | 83 | } | |
| 102 | 84 | ||
| 103 | 85 | @Override | |
| 104 | 86 | public void init() throws ServletException { | |
| 87 | + String url = System.getProperty("postgresql"); | ||
| 88 | + log("connecting to: " + url); | ||
| 105 | 89 | try { | |
| 106 | - String url = System.getProperty("postgresql"); | ||
| 107 | - log("connecting to: " + url); | ||
| 108 | - try { | ||
| 109 | - Class.forName("org.postgresql.Driver"); | ||
| 110 | - conn = DriverManager.getConnection(url); | ||
| 111 | - } catch (ClassNotFoundException e) { | ||
| 112 | - throw new ServletException("Error loading JDBC Driver", e); | ||
| 113 | - } catch (SQLException e) { | ||
| 114 | - throw new ServletException("Unable to connect to PostGre", e); | ||
| 115 | - } | ||
| 116 | - | ||
| 117 | - } finally { | ||
| 118 | - // Nothing really to do here. | ||
| 90 | + conn = DriverManager.getConnection(url); | ||
| 91 | + } catch (SQLException e) { | ||
| 92 | + throw new ServletException("Unable to connect to PostgreSQL", e); | ||
| 119 | 93 | } | |
| 120 | 94 | } | |
| 121 | 95 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -48,7 +48,6 @@ | |||
| 48 | 48 | <!-- [END properties] --> | |
| 49 | 49 | ||
| 50 | 50 | <dependencies> | |
| 51 | - <!-- Parent POM defines ${appengine.sdk.version} (updates frequently). --> | ||
| 52 | 51 | <dependency> | |
| 53 | 52 | <groupId>com.google.appengine</groupId> | |
| 54 | 53 | <artifactId>appengine-api-1.0-sdk</artifactId> | |
@@ -78,8 +77,9 @@ | |||
| 78 | 77 | </dependency> | |
| 79 | 78 | <dependency> | |
| 80 | 79 | <groupId>com.google.cloud.sql</groupId> | |
| 81 | - <artifactId>mysql-socket-factory</artifactId> <!-- mysql-socket-factory-connector-j-6 if using 6.x.x --> | ||
| 82 | - <version>1.0.4</version> | ||
| 80 | + <!-- If using MySQL 6.x driver, use mysql-socket-factory-connector-j-6 instead --> | ||
| 81 | + <artifactId>mysql-socket-factory</artifactId> | ||
| 82 | + <version>1.0.5</version> | ||
| 83 | 83 | </dependency> | |
| 84 | 84 | <!-- [END dependencies] --> | |
| 85 | 85 | </dependencies> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,20 +44,22 @@ | |||
| 44 | 44 | // [START example] | |
| 45 | 45 | @SuppressWarnings("serial") | |
| 46 | 46 | // With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required. | |
| 47 | - @WebServlet(name = "CloudSQL", description = "CloudSQL: Write low order IP address to Cloud SQL", | ||
| 47 | + @WebServlet(name = "CloudSQL", | ||
| 48 | + description = "CloudSQL: Write timestamps of visitors to Cloud SQL", | ||
| 48 | 49 | urlPatterns = "/cloudsql") | |
| 49 | 50 | public class CloudSqlServlet extends HttpServlet { | |
| 50 | 51 | Connection conn; | |
| 51 | 52 | ||
| 52 | 53 | @Override | |
| 53 | 54 | public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, | |
| 54 | 55 | ServletException { | |
| 55 | - final String createTableSql = "CREATE TABLE IF NOT EXISTS visits ( visit_id INT NOT NULL " | ||
| 56 | - + "AUTO_INCREMENT, user_ip VARCHAR(46) NOT NULL, timestamp DATETIME NOT NULL, " | ||
| 57 | - + "PRIMARY KEY (visit_id) )"; | ||
| 58 | - final String createVisitSql = "INSERT INTO visits (user_ip, timestamp) VALUES (?, ?)"; | ||
| 59 | - final String selectSql = "SELECT user_ip, timestamp FROM visits ORDER BY timestamp DESC " | ||
| 60 | - + "LIMIT 10"; | ||
| 56 | + | ||
| 57 | + final String createTableSql = "CREATE TABLE IF NOT EXISTS visits ( " | ||
| 58 | + + "visit_id SERIAL NOT NULL, ts timestamp NOT NULL, " | ||
| 59 | + + "PRIMARY KEY (visit_id) );"; | ||
| 60 | + final String createVisitSql = "INSERT INTO visits (ts) VALUES (?);"; | ||
| 61 | + final String selectSql = "SELECT ts FROM visits ORDER BY ts DESC " | ||
| 62 | + + "LIMIT 10;"; | ||
| 61 | 63 | ||
| 62 | 64 | String path = req.getRequestURI(); | |
| 63 | 65 | if (path.startsWith("/favicon.ico")) { | |
@@ -67,37 +69,24 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOExc | |||
| 67 | 69 | PrintWriter out = resp.getWriter(); | |
| 68 | 70 | resp.setContentType("text/plain"); | |
| 69 | 71 | ||
| 70 | - // store only the first two octets of a users ip address | ||
| 71 | - String userIp = req.getRemoteAddr(); | ||
| 72 | - InetAddress address = InetAddress.getByName(userIp); | ||
| 73 | - if (address instanceof Inet6Address) { | ||
| 74 | - // nest indexOf calls to find the second occurrence of a character in a string | ||
| 75 | - // an alternative is to use Apache Commons Lang: StringUtils.ordinalIndexOf() | ||
| 76 | - userIp = userIp.substring(0, userIp.indexOf(":", userIp.indexOf(":") + 1)) + ":*:*:*:*:*:*"; | ||
| 77 | - } else if (address instanceof Inet4Address) { | ||
| 78 | - userIp = userIp.substring(0, userIp.indexOf(".", userIp.indexOf(".") + 1)) + ".*.*"; | ||
| 79 | - } | ||
| 80 | - | ||
| 81 | 72 | Stopwatch stopwatch = Stopwatch.createStarted(); | |
| 82 | 73 | try (PreparedStatement statementCreateVisit = conn.prepareStatement(createVisitSql)) { | |
| 83 | 74 | conn.createStatement().executeUpdate(createTableSql); | |
| 84 | - statementCreateVisit.setString(1, userIp); | ||
| 85 | - statementCreateVisit.setTimestamp(2, new Timestamp(new Date().getTime())); | ||
| 75 | + statementCreateVisit.setTimestamp(1, new Timestamp(new Date().getTime())); | ||
| 86 | 76 | statementCreateVisit.executeUpdate(); | |
| 87 | 77 | ||
| 88 | 78 | try (ResultSet rs = conn.prepareStatement(selectSql).executeQuery()) { | |
| 89 | 79 | stopwatch.stop(); | |
| 90 | 80 | out.print("Last 10 visits:\n"); | |
| 91 | 81 | while (rs.next()) { | |
| 92 | - String savedIp = rs.getString("user_ip"); | ||
| 93 | - String timeStamp = rs.getString("timestamp"); | ||
| 94 | - out.print("Time: " + timeStamp + " Addr: " + savedIp + "\n"); | ||
| 82 | + String timeStamp = rs.getString("ts"); | ||
| 83 | + out.println("Visited at time: " + timeStamp); | ||
| 95 | 84 | } | |
| 96 | - out.println("Elapsed: " + stopwatch.elapsed(TimeUnit.MILLISECONDS)); | ||
| 97 | 85 | } | |
| 98 | 86 | } catch (SQLException e) { | |
| 99 | 87 | throw new ServletException("SQL error", e); | |
| 100 | 88 | } | |
| 89 | + out.println("Query time (ms):" + stopwatch.elapsed(TimeUnit.MILLISECONDS)); | ||
| 101 | 90 | } | |
| 102 | 91 | ||
| 103 | 92 | @Override | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,6 +41,7 @@ | |||
| 41 | 41 | <module>appidentity</module> | |
| 42 | 42 | <module>bigtable</module> | |
| 43 | 43 | <module>cloudsql</module> | |
| 44 | + <module>cloudsql-postgres</module> | ||
| 44 | 45 | <module>datastore</module> | |
| 45 | 46 | <module>datastore-indexes</module> | |
| 46 | 47 | <module>datastore-indexes-exploding</module> | |
@@ -62,7 +63,6 @@ | |||
| 62 | 63 | <module>metadata</module> | |
| 63 | 64 | <module>multitenancy</module> | |
| 64 | 65 | <module>oauth2</module> | |
| 65 | - <module>postgres</module> | ||
| 66 | 66 | <module>pubsub</module> | |
| 67 | 67 | <module>requests</module> | |
| 68 | 68 | <module>remote-client</module> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments