· 8 years ago · Dec 08, 2017, 07:26 AM
1/*
2 * Copyright 2002-2014 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example;
18
19import com.zaxxer.hikari.HikariConfig;
20import com.zaxxer.hikari.HikariDataSource;
21
22import org.springframework.beans.factory.annotation.Autowired;
23import org.springframework.beans.factory.annotation.Value;
24import org.springframework.boot.SpringApplication;
25import org.springframework.boot.autoconfigure.SpringBootApplication;
26import org.springframework.context.annotation.Bean;
27import org.springframework.stereotype.Controller;
28import org.springframework.web.bind.annotation.RequestMapping;
29
30import javax.sql.DataSource;
31
32import java.io.BufferedReader;
33import java.io.IOException;
34import java.io.InputStreamReader;
35import java.net.MalformedURLException;
36import java.net.URL;
37import java.net.URLConnection;
38import java.sql.Connection;
39import java.sql.ResultSet;
40import java.sql.SQLException;
41import java.sql.Statement;
42import java.util.ArrayList;
43import java.util.Map;
44import java.util.Random;
45
46@Controller
47@SpringBootApplication
48public class Main {
49
50 @Value("${spring.datasource.url}")
51 private String dbUrl;
52
53 @Autowired
54 private DataSource dataSource;
55
56 public static void main(String[] args) throws Exception {
57 SpringApplication.run(Main.class, args);
58 }
59
60 @RequestMapping("/")
61 String index() {
62 return "index";
63 }
64
65@RequestMapping("/hello")
66String hello(Map<String, Object> model) {
67 model.put("class", "2018v (it is working!!!), v 0.2");
68 return "hello";
69}
70
71 @RequestMapping("/db")
72 String db(Map<String, Object> model) {
73 try (Connection connection = dataSource.getConnection()) {
74 Statement stmt = connection.createStatement();
75 stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)");
76 stmt.executeUpdate("INSERT INTO ticks VALUES (now())");
77 ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks");
78
79 ArrayList<String> output = new ArrayList<String>();
80 while (rs.next()) {
81 output.add("Read from DB: " + rs.getTimestamp("tick"));
82 }
83
84 model.put("records", output);
85 return "db";
86 } catch (Exception e) {
87 model.put("message", e.getMessage());
88 return "error";
89 }
90 }
91
92// public static void main(String[] args) {
93// new Main().db2(null);
94//}
95
96 @RequestMapping("/db2")
97 String db2(Map<String, Object> model) {
98 StringBuilder site = new StringBuilder();
99 try {
100 URL url = new URL("https://ya.ru/");
101 BufferedReader br = new BufferedReader(
102 new InputStreamReader(
103 url.openStream()));
104 while (true) {
105 String s = br.readLine();
106 if (s == null) {
107 break;
108 }
109 site.append(s);
110 }
111 } catch (IOException e1) {
112 e1.printStackTrace();
113 }
114 model.put("site", site.toString());
115
116 try (Connection connection = dataSource.getConnection()) {
117 Statement stmt = connection.createStatement();
118 stmt.executeUpdate(
119 "CREATE TABLE IF NOT EXISTS kitties (tick timestamp, name varchar(255))");
120 String s = "";
121 Random r = new Random();
122 for (int i = 0; i < 6; i++) {
123 s += (char) ('a' + r.nextInt(26));
124 }
125 stmt.executeUpdate("INSERT INTO kitties VALUES (now(), '" + s + "')");
126 ResultSet rs = stmt.executeQuery("SELECT tick, name FROM kitties");
127
128 ArrayList<String> output = new ArrayList<String>();
129 while (rs.next()) {
130 output.add(rs.getString("name") + " was born at " + rs.getTimestamp("tick"));
131 }
132
133 model.put("records", output);
134 return "db";
135 } catch (Exception e) {
136 model.put("message", e.getMessage());
137 return "error";
138 }
139 }
140
141 @Bean
142 public DataSource dataSource() throws SQLException {
143 if (dbUrl == null || dbUrl.isEmpty()) {
144 return new HikariDataSource();
145 } else {
146 HikariConfig config = new HikariConfig();
147 config.setJdbcUrl(dbUrl);
148 return new HikariDataSource(config);
149 }
150 }
151
152}