Describe what you learned in Java Gold along with the Java SE8 Gold problem collection (Kuromoto).
Objects' hashCode method is there to improve search performance.
The meaning of the final modifier differs depending on the class, variable, and method.
Class: Non-inheritable
Variable: Non-reassignable, that is, a constant
Method: Cannot be overridden
** You can add final to the argument ** (I didn't know ...)
Code in static Initializer is executed when class is loaded
Features of Singleton class
Create your own instance in a private static field
Declare the constructor private
Prepare a method in public static that returns the instance set in the field
About rhombus inheritance. If the method is overwritten by multiple inheritance of the interface, a compile error will occur if the distance is the same, but if the closest distance is uniquely determined, the closest one will be adopted.
package tryAny.diamondExtends;
public interface A {
default void x() {
System.out.println("I am A.");
}
}
package tryAny.diamondExtends;
public interface B extends A {
}
package tryAny.diamondExtends;
public interface C extends A {
default void x() {
System.out.println("I am C.");
}
}
package tryAny.diamondExtends;
public class DiamondExtends implements B, C {
public static void main(String[] args) {
DiamondExtends de = new DiamondExtends();
de.x();// I am C.Output
}
}
There is a nested class. (In contrast to this, ordinary classes are called top-level classes)
static member class, inner class
The outer class is called the enclosing class. The enclosing class is not necessarily the top level class.
The usage of nested classes is as follows according to Perfect Java
When using an object only inside the enclosing class
If you want to hide the nested class implementation inside the enclosing class
package tryAny.inner;
public class InnerTest {
class InnerA {
void innerMethod() {
System.out.println("I am inner.");
}
}
private void execInner() {
InnerA ia = new InnerA();
ia.innerMethod();
}
public static void main(String[] args) {
InnerTest it = new InnerTest();
it.execInner();
}
}
package tryAny.inner;
public class Other {
public static void main(String[] args) {
InnerTest.InnerA ia = new InnerTest().new InnerA();
ia.innerMethod();
}
}
package tryAny.anonymous;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class AnonymousTest {
public static void main(String[] args) {
Data d1 = new Data(3, "aaa");
Data d2 = new Data(1, "bbb");
Data d3 = new Data(2, "ccc");
List<Data> list = new ArrayList<Data>();
list.add(d1);
list.add(d2);
list.add(d3);
list.stream().forEach(System.out::println);// 3,1,Displayed in order of 2
list.sort(new Comparator<Data>() {
public int compare(Data data1, Data data2) {
if (data1.num > data2.num) {
return 1;
} else {
return -1;
}
}
});
list.stream().forEach(System.out::println);// 1,2,Display in order of 3
}
static class Data {
int num;
String value;
Data(int num, String value) {
this.num = num;
this.value = value;
}
@Override
public String toString() {
return num + ":" + value;
}
}
}
package tryAny.enumTes;
public enum Gender {
MAN, WOMAN, OTHER;
}
class Try {
public static void main(String[] args) {
System.out.println(Gender.MAN.toString());// MAN
System.out.println(Gender.MAN.name());// MAN
for (Gender g : Gender.values()) {
System.out.println(g.name() + ":" + g.ordinal());
/**
* MAN:0 </br>
* WOMAN:1 </br>
* OTHER:2
*/
}
Gender g = Gender.MAN;
trans(g);//Man
}
public static void trans(Gender g) {
switch (g) {
case MAN:
System.out.println("Man");
break;
case WOMAN:
System.out.println("woman");
break;
case OTHER:
System.out.println("Other");
break;
default:
assert (false);
}
}
}
Type variable naming guidelines
Elements stored in the collection are ** E **
The keys and values stored in the map are ** K ** and ** V **, respectively.
General types other than the above are ** T **
** R ** for the return type
Primitive type cannot be specified in the type argument.
Variable declaration and instantiation type arguments must match. Therefore, from JavaSE7, it is possible to omit the type argument in instantiation as follows.
Foo<Integer> foo = new Foo<>();
package tryAny.Generic;
public class GenericTest<T> {
/**
*This can be compiled because T at the time of class declaration and T in the method below are different.
*/
static <T> T exec3(T t) {
return t;
}
/**The following does not compile
static T exec(T t) {
return t;
}
**/
}
//T is limited to the Number subtype.
class Foo<T extends Number> {}
package tryAny.Generic;
public class BoundedType {
public static void main(String[] args) {
Hoge<Foo, Bar> h1 = new Hoge<Foo, Bar>();//Compile through
Hoge<Foo, Foo> h2 = new Hoge<Foo, Foo>();//Compile through
// Hoge<Bar, Foo> h3 = new Hoge<Bar, Foo>();//Does not compile
}
}
abstract class Foo {
}
class Bar extends Foo {
}
class Hoge<T, U extends T> {
}
Wildcards **? ** Can be used for type arguments.
When inheriting a generic class, the type variable must be fixed.
Comparison of ArrayList and LinkedList
ArrayList has fast random access to elements, but the cost of adding elements to the list is high
LinkedList has slower random access than ArrayList, but costs less to add elements
ClassCastException is thrown if the element stored in the TreeSet is not of type Comparable
The variable that receives the return value of the constructor reference must be a functional interface (which is obvious, if you think about it).
package tryAny.lambda;
public interface Flyable {
AirPlane getAirPlane(String name);
}
package tryAny.lambda;
public class LambdaTest6 {
public static void main(String[] args) {
Flyable f = AirPlane::new;
AirPlane ap = f.getAirPlane("ana");
System.out.println(ap);
}
}
class AirPlane {
private String name;
public AirPlane(String name) {
this.name = name;
}
@Override
public String toString() {
return "constructor " + this.name;
}
}
package tryAny.lambda;
public class LambdaTest {
public static void main(String[] args) {
Runnable r1 = new Runnable() {
public void run() {
System.out.println("run1");
}
};
//the same
Runnable r2 = () -> System.out.println("run2");
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
t1.start();
t2.start();
}
}
package tryAny.lambda;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class LambdaTest2 {
public static void main(String[] args) {
Supplier<String> s = () -> "supplier";
Predicate<String> p = str -> "supplier".equals(str);
Consumer<String> c = str -> System.out.println(str);
Function<String, Integer> f = str -> str.length();
// "supplier"The number of characters of.
if (p.test(s.get())) {
c.accept(f.apply(s.get()).toString());
}
}
}
package tryAny.lambda;
import java.util.function.IntFunction;
import java.util.function.IntUnaryOperator;
import java.util.stream.IntStream;
public class LambdaTest3 {
public static void main(String[] args) {
IntStream s1 = IntStream.of(1, 2, 3);
IntFunction<IntUnaryOperator> func = x -> y -> x + y;
IntStream s2 = s1.map(func.apply(2));
s2.forEach(System.out::println);
}
}
package tryAny.stream;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamTest1 {
public static void main(String[] args) {
String[] strAry = { "a", "b", "c" };
Stream<String> strStream = Arrays.stream(strAry);
strStream.forEach(System.out::println);
int[] intAry = { 1, 2, 3, 4, 5 };
IntStream intStream = Arrays.stream(intAry);
intStream.forEach(System.out::println);
IntStream intStream2 = Arrays.stream(intAry, 2, 4);
intStream2.forEach(System.out::println);
/**
* 3</br>
* 4
*/
IntStream.range(0, 4).forEach(System.out::print);// 0123
IntStream.rangeClosed(0, 4).forEach(System.out::print);// 01234
}
}
package tryAny.stream;
import java.util.stream.Stream;
public class StreamTest2 {
public static void main(String[] args) {
Stream<Data> s = Stream.of(new StreamTest2().new Data("a", 1), new StreamTest2().new Data("b", 2));
// "a:1 b:2 "
s.forEach(System.out::print);
}
class Data {
String str;
int i;
public Data(String s, int i) {
this.str = s;
this.i = i;
}
@Override
public String toString() {
return this.str + ":" + this.i + " ";
}
}
}
package tryAny.stream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class StreamTest3 {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new FileReader("src/main/java/tryAny/stream/sample.txt"))) {
br.lines().forEach(System.out::println);
Files.lines(Paths.get("src/main/java/tryAny/stream/sample.txt")).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package tryAny.lambda;
import java.util.Arrays;
import java.util.stream.IntStream;
public class LambdaTest4 {
public static void main(String[] args) {
int[][] data = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
IntStream s1 = Arrays.stream(data).flatMapToInt(Arrays::stream);
s1.forEach(System.out::print);// 123456
}
}
package tryAny.stream;
import java.util.Arrays;
import java.util.List;
public class StreamTest5 {
public static void main(String[] args) {
List<List<String>> l = Arrays.asList(Arrays.asList("banana", "apple"), Arrays.asList("banana", "orange"));
l.stream().flatMap(li -> li.stream()).distinct().forEach(System.out::println);
List<List<List<String>>> l2 = Arrays.asList(Arrays.asList(Arrays.asList("pineapple", "grape")));
l2.stream().flatMap(li2 -> li2.stream()).flatMap(li3 -> li3.stream()).forEach(System.out::println);
}
}
package tryAny.stream;
import java.util.HashMap;
import java.util.Map;
public class StreamTest6 {
public static void main(String[] args) {
Map<String, Integer> m = new HashMap<>();
m.put("A", 1);
m.merge("A", 2, (v1, v2) -> v1 * v2);
m.merge("b", 1, (v1, v2) -> v1 * v2);
System.out.println(m);
// {A=2, b=1}
}
}
package tryAny.stream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamTest8 {
public static void main(String[] args) {
/**
* Collectors.toList()The Collector object created by</br>
*Accumulate input elements in List.
*/
List<String> l = Stream.of("paper", "book", "photo").collect(Collectors.toList());
l.stream().forEach(System.out::println);
// Collector<Integer,Map<String,Integer>,Map<String,List<Integer>>>
// collector
// = Collectors.groupingBy(Integer;]
//Group by number of characters
Map<Integer, List<String>> map = l.stream().collect(Collectors.groupingBy(e -> e.length()));
map.forEach((v1, v2) -> System.out.println(v1 + ":" + v2));
/**
* 4:[book]</br>
* 5:[paper, photo]
*/
//Group after the acronym
Map<Character, List<String>> map2 = l.stream().collect(Collectors.groupingBy(e -> e.charAt(0)));
map2.forEach((v1, v2) -> System.out.println(v1 + ":" + v2));
}
}
package tryAny.lambda;
import java.util.function.IntFunction;
import java.util.function.ToIntFunction;
public class LambdaTest7 {
public static void main(String[] args) {
//Returns the int taken as an argument as a character
IntFunction<String> intFunc = String::valueOf;
//Returns the number of String characters taken as an argument
ToIntFunction<String> toIntFunc = String::length;
System.out.println(intFunc.apply(11));
System.out.println(toIntFunc.applyAsInt("apple"));
}
}
package tryAny.exception;
public class ExceptionTest1 {
public static void main(String[] args) {
try {
x();
} catch (Throwable e) {
while (e != null) {
System.out.println(e.getMessage());
e = e.getCause();
}
/**
* from x</br>
* from y</br>
* from z
*/
}
}
static void x() throws Exception {
try {
y();
} catch (Exception e) {
throw new Exception("from x", e);
}
}
static void y() throws Exception {
try {
z();
} catch (Exception e) {
throw new Exception("from y", e);
}
}
static void z() throws Exception {
throw new Exception("from z");
}
}
package tryAny.exception;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class ExceptionTest2 {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("src/main/java/tryAny/stream/sample.txt"));
int a = Integer.parseInt("a");
} catch (IOException | NumberFormatException e) {
System.out.println(e);
}
}
}
package tryAny.exception;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ExceptionTest3 {
public static void main(String[] args) {
try (FileReader in = new FileReader("src/main/java/tryAny/stream/sample.txt");
FileWriter out = new FileWriter("src/main/java/tryAny/exception/sample.txt");
AutoCloseTest act = new AutoCloseTest();
/** AutoCloseTest2 act2 = new AutoCloseTest2();← Not implemented because AutoCloseable is not implemented**/ ) {
//AutoCloseTest close is executed.
} catch (IOException e) {
}
}
}
class AutoCloseTest implements AutoCloseable {
@Override
public void close() {
System.out.println("AutoCloseTest is closed.");
}
}
class AutoCloseTest2 {
public void close() {
System.out.println("AutoCloseTest is closed.");
}
}
package tryAny.exception;
public class ExceptionTest4 {
public static void main(String[] args) {
int x = 1;
assert (x == 2) : "Send an Error Message!";
/**
*In the VM argument-When I put ea, I get the following error.
*
* Exception in thread "main" java.lang.AssertionError:Send an Error Message!
* at tryAny.exception.ExceptionTest4.main(ExceptionTest4.java:7)
*
*/
}
}
package tryAny.dateTime;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
public class DateTest1 {
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
LocalDate ld = LocalDate.of(2018, Month.JANUARY, 1);
System.out.println(now + " " + ld);
}
}
package tryAny.dateTime;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class DateTest2 {
public static void main(String[] args) {
LocalDate ld1 = LocalDate.now();
LocalDate ld2 = ChronoUnit.YEARS.addTo(ld1, -1);
long l = ChronoUnit.DAYS.between(ld1, ld2);
System.out.println(ld1);//now
System.out.println(ld2);//One year ago
System.out.println(l);//If you do not consider leap years, etc.-365
}
}
package tryAny.dateTime;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.temporal.ChronoUnit;
public class DateTest3 {
public static void main(String[] args) {
LocalDateTime ldt1 = LocalDateTime.now();
LocalDateTime ldt2 = ChronoUnit.DAYS.addTo(ldt1, 31);
Duration d = Duration.between(ldt1, ldt2);
Period p = Period.between(ldt1.toLocalDate(), ldt2.toLocalDate());
System.out.println(d + " " + p);// PT744H P1M
}
}
package tryAny.dateTime;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class DateTest4 {
public static void main(String[] args) {
DateTimeFormatter dtf1 = DateTimeFormatter.BASIC_ISO_DATE;
DateTimeFormatter dtf2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
System.out.println(dtf1.format(LocalDate.now()));
System.out.println(dtf2.format(LocalDateTime.now()));
//Create your own format
DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("yyyy★MM★dd");
System.out.println(dtf3.format(LocalDate.now()));
}
}
package tryAny.io;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class IOTest1 {
public static void main(String[] args) {
try (PrintWriter pw = new PrintWriter("out.txt")) {
pw.println("Hello world");
//BufferedWriter cannot output primitive types such as boolean and double as they are.
pw.println(true);
pw.println(0.4);
} catch (FileNotFoundException e) {
System.out.println(e);
}
}
}
package tryAny.io;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class IOTest3 {
public static void main(String[] args) throws IOException {
//Stores serialized data.
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("iotest3.ser"))) {
String test = "iotest3";
oos.writeObject(test);//This is iotest3.You can ser.
oos.writeInt(111);
} catch (IOException e) {
throw new IOException(e);
}
//Get the serialized data and expand it to memory.
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("iotest3.ser"))) {
System.out.println(ois.readObject());
System.out.println(ois.readInt());
} catch (ClassNotFoundException | IOException e) {
throw new IOException(e);
}
}
}
Static variables and variables with the transient modifier are not subject to serialization.
Java SE 7 has changed the class that handles files from File to Path, which makes it possible to transparently use the platform's file system-specific mechanism.
The default behavior of the Files class is as follows. These behaviors can be changed by specifying options in the arguments.
An exception will be thrown if the destination file exists.
The attributes of the copy source file are not inherited.
When copying a directory, the files in the directory are not copied.
When copying a symbolic link, only the link destination is copied, not the link itself.
Classes that handle file attributes are in the java.nio.file.attribute package, and there are the following three.
BasicFileAttributes: File attributes common to both Windows and Linux
DosFileAttributes: Windows file attributes
PosixFileAttributes: Linux file attributes
There is an AttributeView as an interface that represents a set of file attributes. (** What does a set of file attributes mean ?? **)
If you want to get a single attribute of a file or directory, you can use Files.getAttribute.
package tryAny.io;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
public class IOTest5 {
public static void main(String[] args) throws IOException {
Path p = Paths.get("out.txt");
//Creation date and time
System.out.println((FileTime) Files.getAttribute(p, "creationTime"));
//size
System.out.println((long) Files.getAttribute(p, "size"));
//Whether it is a symbolic link
System.out.println((boolean) Files.getAttribute(p, "isSymbolicLink"));
}
}
package tryAny.io;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class IOTest6 {
public static void main(String[] args) throws IOException {
Path p = Paths.get("src");
//When a file appears, output it as standard.
Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes bfa) throws IOException {
System.out.println(path);
return FileVisitResult.CONTINUE;
}
});
//Output only the directory.
Files.walk(p).filter(path -> {
try {
return (boolean) Files.getAttribute(path, "isDirectory");
} catch (IOException e) {
System.out.println(e);
return false;
}
}).forEach(System.out::println);
}
}
Concurrency utilities are provided in java.util.concurrent.atomic and java.util.concurrent.locks. The mechanisms and functions provided here are as follows.
Thread pool: A mechanism to eliminate the overhead of thread creation by creating and storing threads in advance. Executor framework provides this functionality
Parallel collection: A collection that strikes a good balance between performance and concurrency
Atomic variable: A variable that guarantees the atomicity of processing, that is, a variable that guarantees that there can be no situation other than the choice of performing or not performing a certain function. Increment processing is the process of (1) reading the value → (2) changing the value → (3) writing the value, but it is guaranteed that the flow of (1) to (3) is either completed or untouched. (It is considered that other processing writes to the target variable between ① and ②)
Counting semaphore: ** Semaphore ** is a mechanism used for synchronization and interrupt control between processes and threads. There are ** binary semaphores ** and ** counting semaphores ** in semaphores. While the binary semaphore has only "possible" and "impossible" access control types for resources, the counting semaphore can arbitrarily set the number of accessible resources.
Atomic variables guarantee atomic operations.
package tryAny.concurrentUtility;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
public class ConcurrentTest5 {
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService es = Executors.newFixedThreadPool(8);
MyAtomCounter mac = new MyAtomCounter();
Future<?> ret1 = es.submit(new MyAtomWorker(mac));
Future<?> ret2 = es.submit(new MyAtomWorker(mac));
ret1.get();
ret2.get();
es.shutdown();
//It will definitely be 200,000.
mac.show();
}
}
class MyAtomCounter {
private AtomicInteger count = new AtomicInteger();
public void increment() {
count.incrementAndGet();
}
public void show() {
System.out.println("For AtomicInteger:" + count);
}
}
class MyAtomWorker implements Runnable {
private static final int NUM_LOOP = 100000;
private final MyAtomCounter counter;
MyAtomWorker(MyAtomCounter counter) {
this.counter = counter;
}
@Override
public void run() {
for (int i = 0; i < NUM_LOOP; i++) {
counter.increment();
}
}
}
package tryAny.concurrentUtility;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
public class ConcurrentTest9 {
public static void main(String[] args) {
Runnable runner = new Runner();
CyclicBarrier barrier = new CyclicBarrier(3, runner);
List<Worker> workers = new ArrayList<Worker>();
for (int i = 0; i < 3; i++) {
workers.add(new Worker(barrier));
}
workers.stream().parallel().forEach(Worker::run);
/**
* 1<br>
* 2<br>
* 3<br>
* All threads have run.
*
* 1,2,Part 3 may not be the case. 1,2,It's like 1.
*/
}
}
class Runner implements Runnable {
@Override
public void run() {
System.out.println("All threads have run.");
}
}
class Worker extends Thread {
private CyclicBarrier barrier;
private static int count;
public Worker(CyclicBarrier barrier) {
this.barrier = barrier;
}
@Override
public void run() {
try {
System.out.println(++count);
barrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
e.printStackTrace();
}
}
}
package tryAny.concurrentUtility;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
//Continues to generate random values every second.
public class ConcurrentTest6 {
public static void main(String[] args) {
ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(1);
ScheduledFuture<?> sf = stpe.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println(Math.random());
}
}, 1000, 1000, TimeUnit.MILLISECONDS);
while (true) {
if (sf.isCancelled() || sf.isDone()) {
stpe.shutdown();
break;
}
}
}
}
package tryAny.concurrentUtility;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;
public class ConcurrentTest7 {
public static void main(String[] args) throws InterruptedException {
ForkJoinPool jfp = new ForkJoinPool();
jfp.execute(new MyAction());
System.out.println("①");
Thread.sleep(3000);
System.out.println("②");
/**
* ①</br>
* ③</br>
* ②
*/
jfp.invoke(new MyAction());
System.out.println("①");
Thread.sleep(3000);
System.out.println("②");
/**
* ③</br>
* ①</br>
* ②
*/
}
}
class MyAction extends RecursiveAction {
@Override
protected void compute() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("③");
}
}
package tryAny.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcTest1 {
public static void main(String[] args) {
try (Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "test", "test");
Statement stmt = c.createStatement()) {
ResultSet rs = stmt.executeQuery("SELECT * FROM personal;");
if (rs.absolute(4)) {
System.out.println(rs.getString(2));
}
if (stmt.execute("SELECT * from personal")) {
ResultSet rs2 = stmt.getResultSet();
if (rs2.next()) {
System.out.println(rs2.getInt(1));
}
}
System.out.println(stmt.executeUpdate("insert into personal values(6,'ccc','dddd')"));
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Class.forName("com.mysql.jdbc.Driver");
package tryAny.locale;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class LocaleTest1 {
public static void main(String[] args) {
try (InputStream is = new FileInputStream("test.properties");
InputStream is2 = new FileInputStream("test2.properties")) {
Properties p = new Properties();
p.load(is);
p.list(System.out);
System.out.println("★" + p.getProperty("fruit") + "★");
//The load here is not an overwrite, but an addition.
p.loadFromXML(is2);
p.forEach((k, v) -> System.out.println(k + "=" + v));
System.out.println("★" + p.getProperty("hoge") + "★");
/**
* -- listing properties --</br>
* fruit=apple</br>
* vegitable=carot</br>
* ★apple★</br>
* vegitable=carot</br>
* hoge=Hoge</br>
* fuga=Fuga</br>
* fruit=apple</br>
* piyo=Piyo</br>
*★ Hoge ★</br>
*
*/
} catch (IOException e) {
e.printStackTrace();
}
}
}
package tryAny.locale;
import java.util.Locale;
import java.util.ResourceBundle;
public class LocaleTest2 {
public static void main(String[] args) {
// test_ja_JP.fruit in properties=banana
ResourceBundle rb1 = ResourceBundle.getBundle("test");
System.out.println(rb1.getString("fruit"));// banana
// test_en_US.fruit in properties=apple
ResourceBundle rb2 = ResourceBundle.getBundle("test", Locale.US);
System.out.println(rb2.getString("fruit"));// apple
}
}
`Locale l = Locale.JAPAN;`
Locale.Builder b = new Locale.Builder();
b.setLanguage("cat");
b.setRegion("ES");
Locale l = b.build();
Recommended Posts