|
| 1 | +package com.cloudcontrolled.sample.mysql; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.PrintWriter; |
| 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.sql.Statement; |
| 11 | + |
| 12 | +import javax.servlet.ServletException; |
| 13 | +import javax.servlet.http.*; |
| 14 | + |
| 15 | +import org.eclipse.jetty.server.Server; |
| 16 | +import org.eclipse.jetty.servlet.*; |
| 17 | + |
| 18 | +public class App { |
| 19 | + |
| 20 | + private static class Write extends HttpServlet { |
| 21 | + @Override |
| 22 | + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 23 | + Connection connection; |
| 24 | + try { |
| 25 | + connection = getConnection(); |
| 26 | + String insert = "INSERT INTO messages (message) VALUES(?)"; |
| 27 | + PreparedStatement stmt = connection.prepareStatement(insert); |
| 28 | + stmt.setString(1, req.getParameter("message")); |
| 29 | + stmt.executeUpdate(); |
| 30 | + connection.close(); |
| 31 | + } catch (SQLException e) { |
| 32 | + throw new ServletException(e); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + private static class Read extends HttpServlet { |
| 38 | + @Override |
| 39 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| 40 | + Connection connection; |
| 41 | + PrintWriter out = resp.getWriter(); |
| 42 | + try { |
| 43 | + connection = getConnection(); |
| 44 | + Statement stmt = connection.createStatement(); |
| 45 | + String select = "SELECT * FROM messages GROUP BY id DESC LIMIT 1"; |
| 46 | + ResultSet rs = stmt.executeQuery(select); |
| 47 | + if (rs.next()) { |
| 48 | + out.print(rs.getString("message")); |
| 49 | + } else { |
| 50 | + out.print("Sorry, no message for you!!!"); |
| 51 | + } |
| 52 | + out.flush(); |
| 53 | + out.close(); |
| 54 | + connection.close(); |
| 55 | + } catch (SQLException e) { |
| 56 | + throw new ServletException(e); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public static void main(String[] args) throws Exception { |
| 62 | + createSchema(); |
| 63 | + Server server = new Server(Integer.valueOf(System.getenv("PORT"))); |
| 64 | + ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); |
| 65 | + context.setContextPath("/"); |
| 66 | + server.setHandler(context); |
| 67 | + context.addServlet(new ServletHolder(new Write()), "/write"); |
| 68 | + context.addServlet(new ServletHolder(new Read()), "/read"); |
| 69 | + server.start(); |
| 70 | + server.join(); |
| 71 | + } |
| 72 | + |
| 73 | + private static void createSchema() throws SQLException { |
| 74 | + Connection connection = getConnection(); |
| 75 | + Statement stmt = connection.createStatement(); |
| 76 | + stmt.executeUpdate("DROP TABLE IF EXISTS messages"); |
| 77 | + stmt.executeUpdate("CREATE TABLE messages (id INT AUTO_INCREMENT, message VARCHAR(45), PRIMARY KEY (id))"); |
| 78 | + connection.close(); |
| 79 | + } |
| 80 | + |
| 81 | + protected static Connection getConnection() throws SQLException { |
| 82 | + String host = System.getenv("MYSQLS_HOSTNAME"); |
| 83 | + String port = System.getenv("MYSQLS_PORT"); |
| 84 | + String database = System.getenv("MYSQLS_DATABASE"); |
| 85 | + String username = System.getenv("MYSQLS_USERNAME"); |
| 86 | + String password = System.getenv("MYSQLS_PASSWORD"); |
| 87 | + String dbUrl = "jdbc:mysql://" + host + ":" + port + "/" +database; |
| 88 | + return DriverManager.getConnection(dbUrl, username, password); |
| 89 | + } |
| 90 | +} |
0 commit comments