[Java][STS4] macでspringの環境構築

$ brew cask install java
-> エラーになるので
$ brew install cask
-> 更にエラーになるので
$ git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch –unshallow

$ brew install java
$ export JAVA_HOME=`/usr/libexec/java_home -v 15`
$ echo $JAVA_HOME
$ export PATH=$JAVA_HOME/bin:$PATH
$ echo $PATH
$ brew install maven
$ export M3_HOME=/usr/local/apache-maven-3.6.3
$ M3=$M3_HOME/bin
$ export PATH=$M3:$PATH
$ mvn –version

https://spring.io/tools

mavenのパスを通す

環境構築はこれでOKっぽい?

[jdk14.0.2] 基礎4

ArrayList

import java.util.*;

public class MyApp {

	public static void main(String[] args){

		List<Integer> sales = new ArrayList<>();

		sales.add(10);
		sales.add(20);
		sales.add(30);

		for (int i = 0; i < sales.size(); i++){
			System.out.println(sales.get(i));
		}

		sales.set(0, 100);
		sales.remove(2);

		for (Integer sale: sales){
			System.out.println(sale);
		}
	}
}

HashSet // 重複しない、順番が定まらない
L TreeSet, LinkedHashSet

public static void main(String[] args){

		// HashSet<Integer> = new HashSet<>();
		Set<Integer> sales = new HashSet<>();

		sales.add(10);
		sales.add(20);
		sales.add(30);
		sales.add(10);

		System.out.println(sales.size());
		for(Integer sale:sales){
			System.out.println(sale);
		}

		sales.remove(30);

		for(Integer sale:sales){
			System.out.println(sale);
		}
	}

HashMap

public class MyApp {

	public static void main(String[] args){
		// HashMap: key value
		// HashMap<String, Integer> sales = new HashMap<>();
		Map<String, Integer> sales = new HashMap<>();

		sales.put("tom", 10);
		sales.put("bob", 20);
		sales.put("steve", 30);

		System.out.println(sales.get("tom"));
		System.out.println(sales.size());

		for(Map.Entry<String, Integer> sale: sales.entrySet()){
			System.out.println(sale.getKey() + ":" + sale.getValue());
		}

		sales.put("tom", 100);
		sales.remove("steve");

		for(Map.Entry<String, Integer> sale: sales.entrySet()){
			System.out.println(sale.getKey() + ":" + sale.getValue());
		}
	}
}

StreamAPI

public static void main(String[] args){
		List<Integer> sales = new ArrayList<>(Arrays.asList(12, 30, 22, 4, 9));

		// for(Integer sale: sales){
		// 	System.out.println(sale);
		// }
		sales
			.stream()
			.filter(e-> e % 3 == 0)
			.map(e-> "(" + e + ")")
			.forEach(System.out::println);
		
	}

LocalDateTime class

import java.time.*;
import java.time.format.DateTimeFormatter;

public class MyApp {

	public static void main(String[] args){
		LocalDateTime d = LocalDateTime.now();

		System.out.println(d.getYear());
		System.out.println(d.getMonth().getValue());

		System.out.println(d.plusMonths(2).minusDays(3));

		DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy!MM!dd!");
		System.out.println(d.format(dtf));
	}
}

[jdk14.0.2] 基礎3

exception

class MyException extends Exception {
	public MyException(String s){
		super(s);
	}
}

public class MyApp {

	public static void div(int a, int b){
		try {
			if(b < 0){
				throw new MyException("not minus!");
			}
			System.out.println(a / b);
		} catch(ArithmeticException e){
			System.err.println(e.getMessage());
		} catch(MyException e){
			System.err.println(e.getMessage());
		} finally {
			System.out.println(" -- end -- ");
		}
	}

	public static void main(String[] args){
		div(3, 0);
		div(5, -2);
	}
}

wrapper class

public class MyApp {

	public static void main(String[] args){
		// Integer i = new Integer(32);
		// int n = i.intValue();
		Integer i = 32; // auto boxing 参照型
		int n = i; // auto unboxing
		System.out.println();
	}
}

generics 型の汎用化

class MyData<T> {
	public void getThree(T x){
		System.out.println(x);
		System.out.println(x);
		System.out.println(x);
	}
}

public class MyApp {

	public static void main(String[] args){
		MyData<Integer> i = new MyData<>(); //参照型
		i.getThree(32);

		MyData<String> s = new MyData<>(); //参照型
		s.getThree("What's up");
	}
}

Thread

class MyRunnable implements Runnable {
	@Override
	public void run(){
		for (int i = 0; i < 500; i++){
			System.out.print('*');
		}
	}
}

public class MyApp {

	public static void main(String[] args){
		MyRunnable r = new MyRunnable();

		Thread t = new Thread(r);
		t.start();

		for (int i = 0; i < 500; i++){
			System.out.print('.');
		}
	}
}

無名クラス

public class MyApp {

	public static void main(String[] args){
		new Thread(new Runnable() {
			@Override
			public void run(){
				for (int i = 0; i < 500; i++){
					System.out.print('*');
				}
			}
		}).start();

		for (int i = 0; i < 500; i++){
			System.out.print('.');
		}
	}
}

ラムダ式

new Thread(()-> {
			for (int i = 0; i < 500; i++){
				System.out.print('*');
			}
		}).start();

		for (int i = 0; i < 500; i++){
			System.out.print('.');
		}

String

public class MyApp {

	public static void main(String[] args){
		String s = "abcdef";
		System.out.println(s.length());
		System.out.println(s.substring(2, 5));
		System.out.println(s.replaceAll("ab", "AB"));

		String s1 = "ab";
		String s2 = "ab";

		if(s1.equals(s2)){
			System.out.println("same!");
		}

		if(s1 == s2){
			System.out.println("same!same!");
		}

		String ss1 = new String("abc");
		String ss2 = new String("abc");

		if(ss1 == ss2){
			System.out.println("same!same!same!");
		}
	}
}

printf

public class MyApp {

	public static void main(String[] args){

		int score = 50;
		double height = 165.8;
		String name ="hpscript";

		System.out.printf("name: %-10s, score: %10d, height: %5.2f\n", name, score, height);

		String s = String.format("name: %10s, score: %-10d, height: %5.2f\n", name, score, height);
		System.out.println(s);
	}
}

random

import java.util.Random;

public class MyApp {

	public static void main(String[] args){

		double d = 53.234;
		
		System.out.println(Math.ceil(d));
		System.out.println(Math.floor(d));
		System.out.println(Math.round(d));
		System.out.println(Math.PI);

		Random r = new Random();
		System.out.println(r.nextDouble()); // 0 - 1
		System.out.println(r.nextInt(100));
		System.out.println(r.nextBoolean());
	}
}

[jdk14.0.2] 基礎2

package hpscript.myapp;
import hpscript.myapp.model.User;
import hpscript.myapp.model.AdminUser;

public class MyApp {

	public static void main(String[] args){

		User tom;
		tom = new User("john");

		System.out.println(tom.name);
		tom.sayHi();

		AdminUser bob = new AdminUser("bob");
		System.out.println(bob.name);
		bob.sayHello();
	}
}

$ javac hpscript/myapp/MyApp.java
$ java hpscript.myapp.MyApp

getter, setter

class User {
	private String name;
	private int score;

	public User(String name, int score){
		this.name = name;
		this.score = score;
	}

	public int getScore(){
		return this.score;
	}

	public void setScore(int score){
		if (score > 0){
			this.score = score;
		}
	}
}



public class MyApp {

	public static void main(String[] args){

		User tom = new User("tom", 65);
		tom.setScore(85);
		tom.setScore(-22);
		System.out.println(tom.getScore());
	}
}

static

class User {
	private String name;
	private static int count = 0;

	public User(String name){
		this.name = name;
		User.count++;
	}

	public static void getInfo(){
		System.out.println("# of instances: " + User.count);
	}
}

public class MyApp {
	public static void main(String[] args){
		User.getInfo();
		User tom = new User("tom");
		User.getInfo();
		User bob = new User("bob");
		User.getInfo();
	}
}

initializer

class User {
	private String name;
	private static int count;

	static {
		User.count = 0;
		System.out.println("Static initializer");
	}

	{
		System.out.println("instance initializer");
	}

	public User(String name){
		this.name = name;
		User.count++;
		System.out.println("Constructor");
	}

	public static void getInfo(){
		System.out.println("# of instances: " + User.count);
	}
}

public class MyApp {
	public static void main(String[] args){
		User.getInfo();
		User tom = new User("tom");
		User.getInfo();
		User bob = new User("bob");
		User.getInfo();
	}
}

final

class User {
	protected String name;
	private static final double VERSION = 1.1;

	User(String name){
		this.name = name;
		User.VERSION = 1.2;
	}

	void sayHi(){
		System.out.println("Hi!" + this.name);
	}
}

class AdminUser extends User{
	AdminUser(String name){
		super(name);
	}

	void sayHi(){
		System.out.println("[admin] Hi!" + this.name);
	}

}

public class MyApp {
	public static void main(String[] args){
		User tom = new User("tom");
		User.sayHi();
		AdminUser bob = new AdminUser("bob");
		AdminUser.sayHi();
	}
}

abstract

abstract class User {
	public abstract void sayHi();
}

class JapaneseUser extends User {
	@Override
	public void sayHi(){
		System.out.println("こんにちは!");
	}
}

class AmericanUser extends User {
	@Override
	public void sayHi(){
		System.out.println("Hi");
	}
}


public class MyApp {
	public static void main(String[] args){
		AmericanUser tom = new AmericanUser();
		JapaneseUser aki = new JapaneseUser();
		tom.sayHi();
		aki.sayHi();
	}
}

interface

interface Printable {
	// 定数
	// 抽象メソッド default, static
	double VERSION = 1.2;
	void print();
	public default void getInfo(){
		System.out.println("I/F ver. " + Printable.VERSION);
	}
} 

class User implements Printable{
	@Override
	public void print(){
		System.out.println("Now printing user profile...");
	}	
}


public class MyApp {
	public static void main(String[] args){
		User tom = new User();
		tom.print();
		tom.getInfo();
	}
}

列挙型

enum Result {
	SUCCESS,
	ERROR,
}

public class MyApp {
	public static void main(String[] args){
		Result res;

		res = Result.ERROR;

		switch (res){
			case SUCCESS:
				System.out.println("OK!");
				System.out.println(res.ordinal());
				break;
			case ERROR:
				System.out.println("NG!");
				System.out.println(res.ordinal());
				break;
		}
	}
}

[jdk14.0.2] 基礎1

compile
$ javac MyApp.java
$ java MyApp

basic

public class MyApp {

	public static void main(String[] args){
		System.out.println("Hello world!");
	}
}

variable

String msg;
		msg = "Hello world!";
		System.out.println(msg);

variable
L primitiveなデータ型はメモリ領域を確保する
L String, arrayなど参照型はメモリの番地を確保して、番地を参照している

public static void main(String[] args){
		// String, char
		char a = 'a';
		// byte short int long
		int x = 10;
		long y = 55555555L;
		// 浮動小数点数 float double
		double d = 23423.334;
		float f = 32.33F;
		// 論理値
		boolean flag = true;
		// \n
		String msg = "Hello wo\nr\tld";
		System.out.println(msg);
	}
int i;
		i = 10 / 3;
		System.out.println(i);
		i = 10 % 3;
		System.out.println(i);
		int x = 5;
		x++;
		System.out.println(x);
		x--;
		System.out.println(x);

cast

double d = 52343.213;
int i = (int)d;

		int i = 10;
		double d = (double)i / 4;
		System.out.println(d); 

if

		int score = 85;
		if(score > 80){
			System.out.println("great");
		} else if (score > 60){
			System.out.println("good"); 
		} else {
			System.out.println("soso"); 
		}

		int score = 25;
		String msg = score > 80 ? "great!": "soso ...!";
		System.out.println("soso");

switch

		String signal = "red";
		switch(signal) {
			case "red":
				System.out.println("stop!");
				break;
			case "blue":
				System.out.println("go!");
				break;
			case "yellow":
				System.out.println("caution!");
				break;
			default:
				System.out.println("wrong signal!");
				break;
		}

while

		int i = 0;
		while(i < 10){
			System.out.println(i);
			i++;
		}

		do {
			System.out.println(i);
			i++;
		}while(i < 10);

for

for(int i = 0; i < 10; i++) {
			if (i == 5){
				continue;
			}
			System.out.println(i);
		}

配列

		int[] sales;
		sales = new int[3];

		sales[0] = 100;
		sales[1] = 200;
		sales[2] = 300;

		System.out.println(sales[1]);

		sales[1] = 1000;
		System.out.println(sales[1]);

		sales = new int[] {100, 200, 300};
		int[] sales = {100, 200, 300};

		int[] sales = {100, 200, 300};

		for(int i = 0; i < sales.length; i++){
			System.out.println(sales[i]);
		}

		for (int sale : sales){
			System.out.println(sale);
		}

基本データ型と参照型

		int[] a = {3, 5, 7};
		int[] b = a;
		b[1] = 8;
		System.out.println(a[1]);
		System.out.println(b[1]);

		// 文字列の場合は、別の領域にメモリを確保する
		String s = "hello";
		String t = s;
		t = "world";
		System.out.println(s);
		System.out.println(t);

method

	public static void sayHi(String name){
		System.out.println("Hi!" + name);
	}

	public static void main(String[] args){
		sayHi("Tom");
		sayHi("Bob");
	}

	public static String sayHi(String name){
		return "hi!" + name;
	}

	public static void main(String[] args){
		String msg = sayHi("Steve");
		System.out.println(msg);
	}

	// overload
	public static void sayHi(){
		System.out.println("Hi! Nobody!");
	}

	public static void main(String[] args){
		sayHi("Steve");
		sayHi();
	}

class

class User {
	String name = "Me!";

	void sayHi(){
		System.out.println("Hi!");
	}
}

public class MyApp {

	public static void main(String[] args){

		User tom;
		tom = new User();

		System.out.println(tom.name);
		tom.sayHi();
	}
}

constructor
L thisはclassのcontructorという意味

class User {
	String name;

	User(String name){
		this.name = name;
	}

	void sayHi(){
		System.out.println("Hi!" + this.name);
	}
}

public class MyApp {

	public static void main(String[] args){

		User tom;
		tom = new User("Tom");

		System.out.println(tom.name);
		tom.sayHi();
	}
}

継承

class AdminUser extends User{
	AdminUser(String name){
		super(name);
	}

	void sayHello(){
		System.out.println("Hello!" + this.name);
	}

}

public class MyApp {

	public static void main(String[] args){

		User tom;
		tom = new User("john");

		System.out.println(tom.name);
		tom.sayHi();

		AdminUser bob = new AdminUser("bob");
		System.out.println(bob.name);
		bob.sayHello();
	}
}

[openjdk-14] ubuntu20.04にopenjdkをinstallする

$ cat /etc/os-release
NAME=”Ubuntu”
VERSION=”20.04.1 LTS (Focal Fossa)”

$ sudo apt search openjdk-\(\.\)\+-jdk$
Sorting… Done
Full Text Search… Done
openjdk-11-jdk/focal-updates,focal-security 11.0.9+11-0ubuntu1~20.04 amd64
OpenJDK Development Kit (JDK)

openjdk-13-jdk/focal-updates 13.0.4+8-1~20.04 amd64
OpenJDK Development Kit (JDK)

openjdk-14-jdk/focal-updates 14.0.2+12-1~20.04 amd64
OpenJDK Development Kit (JDK)

openjdk-8-jdk/focal-updates,focal-security 8u272-b10-0ubuntu1~20.04 amd64
OpenJDK Development Kit (JDK)

$ sudo apt install openjdk-14-jdk
$ java -version
openjdk version “14.0.2” 2020-07-14
OpenJDK Runtime Environment (build 14.0.2+12-Ubuntu-120.04)
OpenJDK 64-Bit Server VM (build 14.0.2+12-Ubuntu-120.04, mixed mode, sharing)
$ javac -version
javac 14.0.2

JDBC

JDBCって何? MySQL接続する為のドライバだった記憶があるが。。

JDBC is, in word, “a standard Java API for accessing relational dattabases (and almost any tabular data). JDBC is said to be short for “Java Database Connectivity” but it is not actually specified in the JDBC specification.

Critical data in an enterprise is often stored in a relational database. As such, JDBC is one of the key APIs underlying Java-based enterprise applications.

Portable database applications can be built by using JDBC drives that absorb differences between databases and JDBC APIs that are standard APIs that do not depend on specific vendors. Not only for the platform of the execution environment, but also for the connected database, it is possible to realize WORA(Write Once, Run Anywhere), which is one of the outstanding features of Java, at a higher level.

JDBC can be used from various Java components such as:
– Regular Java classes and JavaBeans
– Java application that runs on the client
– Java Applet that runs on a web client(web browser)
– Servlets and JSPs that run on a web container(J2EE server)
– Session Bean or Entity Bean that runs on EJB container(J2EE server)

ああああああああ、Javaでアプリケーション作らないといけない、という課題が露呈してしまった。あかん。

Connection reset by peers

Connection reset by peers って何?
Oracleのページを覗く
https://docs.oracle.com/cd/E19455-01/806-2720/msgs-1171/index.html

ピアにより接続が強制的にクローズされました。通常は、タイムアウトや再起動によるリモートホスト接続の切断が原因です。

ピアって何??
AWTとピア
AWT(Abstract Window Toolkit)はJavaでGUI建築をサポートするパッケージ
GUI建築にはAWTの知識が必要
GUIのボタンやスクロールなどのオブジェクトをコンポーネントと呼ぶ
AWTはプラットフォーム依存のウィンドウ操作やAPIを意識することなく建築できる
AWTが提供するコンポーネント・クラス群のことをピアと呼ぶ
JavaプログラムがAWTコンポーネントを生成し、さらにそのコンポーネントがピアを生成する

Java.peerパッケージ
awtの各コンポーネントが、各ウィンドウシステムで実現される際に、システムレベルに対応したクラスに実装すべき機能を提供するインターフェイスの集まり

内部的に使われているのかーーーーーーーー
Java勉強しないとなー

EclipseのMavenとは何か?

Mavenは[méivən]と発音する。メイベンにちかい。

※話が脱線しますが、むかし「熱海」をネッカイとずっと読んでいて、はじめてアタミと聞いた時には、あまりにもかけ離れていて動揺しました。それ以来、発音を確認する時には熱海が頭をよぎります。

Mavenとは?

Mavenとはプロジェクトのビルド、テスト、ドキュメンテーション、成果物など、プロジェクトのライフサイクル全体を管理するもの。

これだけではよくわかりません。Javaをbuildするツールで、Gradleと同じようなものらしいです。Android StudioのGradleをイメージするとわかりやすいかもしれません。

-mvn deployでローカルからリモートリポジトリにデプロイできる。
-作成したjarファイルをリモートにデプロイするにはpom.xmlにリモートの情報を記載する必要がある