Blog of Java Programmer
Group for learn Java! Learning to Java developers
Sort digits with ArrayList:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws IOException {
// insert code here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList mainList = new ArrayList();
for (int i = 0; i < 20; i++) {
mainList.add(Integer.parseInt(reader.readLine()));
}
ArrayList multipleThree = new ArrayList();
ArrayList multipleTwo = new ArrayList();
ArrayList multipleThreeAndTwo = new ArrayList();
for (int i = 0; i < mainList.size(); i++) {
Integer figure = mainList.get(i);
if (figure % 3 == 0 || figure % 2 == 0) {
if (figure % 3 == 0) {
multipleThree.add(figure);
}
if (figure % 2 == 0) {
multipleTwo.add(figure);
}
}else {
multipleThreeAndTwo.add(figure);
}
}
29/07/2021
Java coding
18/07/2021
Оператор деления по модулю - оператор mod, обозначается символом %.
Этот оператор возвращает остаток от деления первого операнда на второй.
Оператор mod "%" в Java работает не только с целыми (такие как: byte/int/short/long),
но и с плавающей точкой (такие как: float/double) числами\типами.
03/03/2021
https://examples.javacodegeeks.com/enterprise-java/jpa/java-persistence-xml-example/
JPA persistence.xml Example | Examples Java Code Geeks - 2021 In this tutorial, we will show how to implement the Java Persistence API and define the JPA Persistence Units with EclipseLink and MySQL in Java.
01/03/2021
Pattern: Object Pool
public class PullObjects {
public static void main(String[] args) {
ObjectPool objectPool = new ObjectPool();
PooledObject pooledObject = objectPool.getPooledObject();
objectPool.releasePooledObject(pooledObject);
}
}
class PooledObject {
}
// басейн
class ObjectPool {
List free = new LinkedList();
List used = new LinkedList();
public PooledObject getPooledObject() {
if (free.isEmpty()) {
PooledObject pooledObject = new PooledObject();
free.add(pooledObject);
return pooledObject;
}else {
PooledObject pooledObject = free.get(0);
used.add(pooledObject);
free.remove(pooledObject);
return pooledObject;
}
}
public void releasePooledObject(PooledObject pooledObject) {
used.remove(pooledObject);
free.add(pooledObject);
}
}
Код для контроллера блога
package com.fara0n.blog.controllers;
import com.fara0n.blog.models.Post;
import com.fara0n.blog.repo.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.ArrayList;
import java.util.Optional;
public class BlogController {
private PostRepository postRepository;
("/blog")
public String blogMain(Model model) {
Iterable posts = postRepository.findAll();
model.addAttribute("posts", posts);
return "blog-main";
}
("/blog/add")
public String blogAdd(Model model) {
return "blog-add";
}
("/blog/add")
public String blogPostAdd( String title, String anons, String full_text, Model model) {
Post post = new Post(title, anons, full_text);
postRepository.save(post);
return "redirect:/blog";
}
("/blog/{id}")
public String blogDetails((value = "id") long id, Model model) {
if (!postRepository.existsById(id)) {
return "redirect:/blog";
}
Optional post = postRepository.findById(id);
ArrayList res = new ArrayList();
post.ifPresent(res::add);
model.addAttribute("post", res);
return "blog-details";
}
("/blog/{id}/edit")
public String blogEdit((value = "id") long id, Model model) {
if (!postRepository.existsById(id)) {
return "redirect:/blog";
}
Optional post = postRepository.findById(id);
ArrayList res = new ArrayList();
post.ifPresent(res::add);
model.addAttribute("post", res);
return "blog-edit";
}
("/blog/{id}/edit")
public String blogPostUpdate((value = "id") long id, String title, String anons, String full_text, Model model) {
Post post = postRepository.findById(id).orElseThrow();
post.setTitle(title);
post.setAnons(anons);
post.setFull_text(full_text);
postRepository.save(post);
return "redirect:/blog";
}
("/blog/{id}/remove")
public String blogPostDelete((value = "id") long id, Model model) {
Post post = postRepository.findById(id).orElseThrow();
postRepository.delete(post);
return "redirect:/blog";
}
}
Настройки подключения Spring к базе данных mySQL:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/Имя_базы_данных
spring.datasource.username=root
spring.datasource.password=root
(файл - application.properties)
pom зависимости:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/spring_web_blog
spring.datasource.username=root
spring.datasource.password=root
Эта страница будет временной площадкой для блога, основной сайт разрабатывается!
Let's continue to discuss about Spring and java Android development!
My repository is closed:
https://bitbucket.org/dashboard/overview
Contact with me for communication
Log in with Atlassian account Log in to Jira, Confluence, and all other Atlassian Cloud products here. Not an Atlassian user? Sign up for free.
05/11/2020
Prog life
Click here to claim your Sponsored Listing.
Category
Website
Address
Kyiv