From aab20c1c484989208760c5582b714c248e4205b0 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 5 Apr 2019 13:13:17 +0200 Subject: [PATCH] + Creating Database connection + POJOs for: - Address - Author - Book - Category - Customer - Publisher Signed-off-by: Johannes Theiner --- .gitignore | 3 +- config.json.example | 9 ++++ .../de/hsel/itech/config/Configuration.java | 39 +++++++++++++++++ .../java/de/hsel/itech/config/Database.java | 23 ++++++++++ src/main/java/de/hsel/itech/db/Database.java | 42 +++++++++++++++++++ .../java/de/hsel/itech/db/pojo/Address.java | 23 ++++++++++ .../java/de/hsel/itech/db/pojo/Author.java | 19 +++++++++ src/main/java/de/hsel/itech/db/pojo/Book.java | 34 +++++++++++++++ .../java/de/hsel/itech/db/pojo/Category.java | 19 +++++++++ .../java/de/hsel/itech/db/pojo/Customer.java | 21 ++++++++++ .../java/de/hsel/itech/db/pojo/Publisher.java | 18 ++++++++ 11 files changed, 249 insertions(+), 1 deletion(-) create mode 100644 config.json.example create mode 100644 src/main/java/de/hsel/itech/config/Configuration.java create mode 100644 src/main/java/de/hsel/itech/config/Database.java create mode 100644 src/main/java/de/hsel/itech/db/Database.java create mode 100644 src/main/java/de/hsel/itech/db/pojo/Address.java create mode 100644 src/main/java/de/hsel/itech/db/pojo/Author.java create mode 100644 src/main/java/de/hsel/itech/db/pojo/Book.java create mode 100644 src/main/java/de/hsel/itech/db/pojo/Category.java create mode 100644 src/main/java/de/hsel/itech/db/pojo/Customer.java create mode 100644 src/main/java/de/hsel/itech/db/pojo/Publisher.java diff --git a/.gitignore b/.gitignore index 358487f..881f473 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.iml /target -.idea \ No newline at end of file +.idea +config.json diff --git a/config.json.example b/config.json.example new file mode 100644 index 0000000..90f45b2 --- /dev/null +++ b/config.json.example @@ -0,0 +1,9 @@ +{ + "database": { + "hostname": "localhost", + "port": 3306, + "database": "itech", + "username": "itech", + "password": "123456" + } +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/config/Configuration.java b/src/main/java/de/hsel/itech/config/Configuration.java new file mode 100644 index 0000000..2f6f02e --- /dev/null +++ b/src/main/java/de/hsel/itech/config/Configuration.java @@ -0,0 +1,39 @@ +package de.hsel.itech.config; + +import com.google.gson.Gson; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.ToString; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + */ + +@Getter +@ToString +@EqualsAndHashCode +public class Configuration { + + private Database database; + + + public static Configuration get(String fileName) { + Gson gson = new Gson(); + + FileReader reader = null; + try { + reader = new FileReader(new File(fileName)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + assert reader != null; + return gson.fromJson(reader, Configuration.class); + } + +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/config/Database.java b/src/main/java/de/hsel/itech/config/Database.java new file mode 100644 index 0000000..2e78599 --- /dev/null +++ b/src/main/java/de/hsel/itech/config/Database.java @@ -0,0 +1,23 @@ +package de.hsel.itech.config; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.ToString; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + */ + +@Getter +@ToString +@EqualsAndHashCode +public class Database { + + String hostname; + int port; + String database; + String username; + String password; +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/Database.java b/src/main/java/de/hsel/itech/db/Database.java new file mode 100644 index 0000000..c42d288 --- /dev/null +++ b/src/main/java/de/hsel/itech/db/Database.java @@ -0,0 +1,42 @@ +package de.hsel.itech.db; + +import de.hsel.itech.config.Configuration; +import org.jetbrains.annotations.Nullable; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + */ +public class Database { + + public Database() { + try { + Class.forName("com.mariadb.jdbc.Driver"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + } + + @Nullable + private Connection getConnection() { + Configuration config = Configuration.get("config.json"); + try { + return DriverManager.getConnection("jdbc:mariadb://" + config.getDatabase().getHostname() + ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabase(), config.getDatabase().getUsername(), config.getDatabase().getPassword()); + + } catch (SQLException e) { + e.printStackTrace(); + } + return null; + } + + + + + +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/Address.java b/src/main/java/de/hsel/itech/db/pojo/Address.java new file mode 100644 index 0000000..f986fbf --- /dev/null +++ b/src/main/java/de/hsel/itech/db/pojo/Address.java @@ -0,0 +1,23 @@ +package de.hsel.itech.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + */ + +@Data +@AllArgsConstructor +public class Address { + + private Customer customer; + private long id; + private String name; + private String street; + private String number; + private int zipCode; + private String city; +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/Author.java b/src/main/java/de/hsel/itech/db/pojo/Author.java new file mode 100644 index 0000000..507893e --- /dev/null +++ b/src/main/java/de/hsel/itech/db/pojo/Author.java @@ -0,0 +1,19 @@ +package de.hsel.itech.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + */ + +@Data +@AllArgsConstructor +public class Author { + + private long id; + private String name; + +} diff --git a/src/main/java/de/hsel/itech/db/pojo/Book.java b/src/main/java/de/hsel/itech/db/pojo/Book.java new file mode 100644 index 0000000..79160e1 --- /dev/null +++ b/src/main/java/de/hsel/itech/db/pojo/Book.java @@ -0,0 +1,34 @@ +package de.hsel.itech.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.time.Year; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + */ + +@Data +@AllArgsConstructor +public class Book { + + private int isbn; + private Author author; + private Publisher publisher; + private Category category; + private String title; + private Year year; + + /** + * saved in cents + */ + private int price; + private String description; + private String image; + + + +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/Category.java b/src/main/java/de/hsel/itech/db/pojo/Category.java new file mode 100644 index 0000000..685ca42 --- /dev/null +++ b/src/main/java/de/hsel/itech/db/pojo/Category.java @@ -0,0 +1,19 @@ +package de.hsel.itech.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + */ + +@Data +@AllArgsConstructor +public class Category { + + private int id; + private String name; + private String image; +} diff --git a/src/main/java/de/hsel/itech/db/pojo/Customer.java b/src/main/java/de/hsel/itech/db/pojo/Customer.java new file mode 100644 index 0000000..b06ab30 --- /dev/null +++ b/src/main/java/de/hsel/itech/db/pojo/Customer.java @@ -0,0 +1,21 @@ +package de.hsel.itech.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + */ + +@Data +@AllArgsConstructor +public class Customer { + + private long id; + private String email; + private String name; + private String password; + private boolean admin; +} \ No newline at end of file diff --git a/src/main/java/de/hsel/itech/db/pojo/Publisher.java b/src/main/java/de/hsel/itech/db/pojo/Publisher.java new file mode 100644 index 0000000..c036d86 --- /dev/null +++ b/src/main/java/de/hsel/itech/db/pojo/Publisher.java @@ -0,0 +1,18 @@ +package de.hsel.itech.db.pojo; + +import lombok.AllArgsConstructor; +import lombok.Data; + +/** + * @author Johannes Theiner + * @version 0.1 + * @since 0.1 + */ + +@Data +@AllArgsConstructor +public class Publisher { + + private long id; + private String name; +} \ No newline at end of file