mikhail_dvorkin icon

Heroku example for 2018v

mikhail_dvorkin | PRO | 12/08/17 06:25:37 AM UTC | 0 ⭐ | 816 👁️ | Never ⏰ | []
Java |

4.46 KB

|

None

|

0 👍

/

0 👎

/*
 * Copyright 2002-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package com.example;
 
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
import javax.sql.DataSource;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Map;
import java.util.Random;
 
@Controller
@SpringBootApplication
public class Main {
 
  @Value("${spring.datasource.url}")
  private String dbUrl;
 
  @Autowired
  private DataSource dataSource;
 
  public static void main(String[] args) throws Exception {
    SpringApplication.run(Main.class, args);
  }
 
  @RequestMapping("/")
  String index() {
    return "index";
  }
 
@RequestMapping("/hello")
String hello(Map<String, Object> model) {
    model.put("class", "2018v (it is working!!!), v 0.2");
    return "hello";
}
 
  @RequestMapping("/db")
  String db(Map<String, Object> model) {
    try (Connection connection = dataSource.getConnection()) {
      Statement stmt = connection.createStatement();
      stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)");
      stmt.executeUpdate("INSERT INTO ticks VALUES (now())");
      ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks");
 
      ArrayList<String> output = new ArrayList<String>();
      while (rs.next()) {
        output.add("Read from DB: " + rs.getTimestamp("tick"));
      }
 
      model.put("records", output);
      return "db";
    } catch (Exception e) {
      model.put("message", e.getMessage());
      return "error";
    }
  }
 
//  public static void main(String[] args) {
//  new Main().db2(null);
//}
  
  @RequestMapping("/db2")
  String db2(Map<String, Object> model) {
    StringBuilder site = new StringBuilder();
    try {
        URL url = new URL("https://ya.ru/");
        BufferedReader br = new BufferedReader(
                new InputStreamReader(
                        url.openStream()));
        while (true) {
            String s = br.readLine();
            if (s == null) {
                break;
            }
            site.append(s);
        }
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    model.put("site", site.toString());
      
    try (Connection connection = dataSource.getConnection()) {
      Statement stmt = connection.createStatement();
      stmt.executeUpdate(
       "CREATE TABLE IF NOT EXISTS kitties (tick timestamp, name varchar(255))");
      String s = "";
      Random r = new Random();
      for (int i = 0; i < 6; i++) {
        s += (char) ('a' + r.nextInt(26));
      }
      stmt.executeUpdate("INSERT INTO kitties VALUES (now(), '" + s + "')");
      ResultSet rs = stmt.executeQuery("SELECT tick, name FROM kitties");
 
      ArrayList<String> output = new ArrayList<String>();
      while (rs.next()) {
        output.add(rs.getString("name") + " was born at " + rs.getTimestamp("tick"));
      }
 
      model.put("records", output);
      return "db";
    } catch (Exception e) {
      model.put("message", e.getMessage());
      return "error";
    }
  }
 
  @Bean
  public DataSource dataSource() throws SQLException {
    if (dbUrl == null || dbUrl.isEmpty()) {
      return new HikariDataSource();
    } else {
      HikariConfig config = new HikariConfig();
      config.setJdbcUrl(dbUrl);
      return new HikariDataSource(config);
    }
  }
 
}

Comments