+ Creating Database connection

+ POJOs for:
- Address
- Author
- Book
- Category
- Customer
- Publisher

Signed-off-by: Johannes Theiner <j.theiner@live.de>
This commit is contained in:
Johannes Theiner 2019-04-05 13:13:17 +02:00
parent 1ca0791cde
commit aab20c1c48
11 changed files with 249 additions and 1 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.iml
/target
.idea
.idea
config.json

9
config.json.example Normal file
View File

@ -0,0 +1,9 @@
{
"database": {
"hostname": "localhost",
"port": 3306,
"database": "itech",
"username": "itech",
"password": "123456"
}
}

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}