Skip to content

Commit 42f1a5c

Browse files
committed
Initial commit
0 parents  commit 42f1a5c

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed

Procfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: java -cp target/classes:target/dependency/* com.cloudcontrolled.sample.mysql.App

pom.xml

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
4+
http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>com.cloudcontrolled.sample.mysql</groupId>
7+
<artifactId>java-mysql-example-app</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
<dependencies>
11+
<dependency>
12+
<groupId>mysql</groupId>
13+
<artifactId>mysql-connector-java</artifactId>
14+
<version>5.1.22</version>
15+
</dependency>
16+
<dependency>
17+
<groupId>org.eclipse.jetty</groupId>
18+
<artifactId>jetty-servlet</artifactId>
19+
<version>7.6.0.v20120127</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>javax.servlet</groupId>
23+
<artifactId>servlet-api</artifactId>
24+
<version>2.5</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>junit</groupId>
28+
<artifactId>junit</artifactId>
29+
<version>3.8.1</version>
30+
<scope>test</scope>
31+
</dependency>
32+
</dependencies>
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-dependency-plugin</artifactId>
38+
<version>2.4</version>
39+
<executions>
40+
<execution>
41+
<id>copy-dependencies</id>
42+
<phase>package</phase>
43+
<goals><goal>copy-dependencies</goal></goals>
44+
</execution>
45+
</executions>
46+
</plugin>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-compiler-plugin</artifactId>
50+
<version>2.3.2</version>
51+
<configuration>
52+
<source>1.6</source>
53+
<target>1.6</target>
54+
</configuration>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
</project>
59+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.cloudcontrolled.sample.mysql;
2+
3+
import junit.framework.Test;
4+
import junit.framework.TestCase;
5+
import junit.framework.TestSuite;
6+
7+
/**
8+
* Unit test for simple App.
9+
*/
10+
public class AppTest
11+
extends TestCase
12+
{
13+
/**
14+
* Create the test case
15+
*
16+
* @param testName name of the test case
17+
*/
18+
public AppTest( String testName )
19+
{
20+
super( testName );
21+
}
22+
23+
/**
24+
* @return the suite of tests being tested
25+
*/
26+
public static Test suite()
27+
{
28+
return new TestSuite( AppTest.class );
29+
}
30+
31+
/**
32+
* Rigourous Test :-)
33+
*/
34+
public void testApp()
35+
{
36+
assertTrue( true );
37+
}
38+
}

0 commit comments

Comments
 (0)