-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathApplication.java
More file actions
92 lines (66 loc) · 2.63 KB
/
Application.java
File metadata and controls
92 lines (66 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.github.hackathon.advancedsecurityjava;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.github.hackathon.advancedsecurityjava.Models.Book;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@SpringBootApplication
public class Application {
public static String connectionString = "jdbc:sqlite:database.sqlite";
public static final Logger logger = LogManager.getLogger();
public static void main(String[] args) {
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException error) {
error.printStackTrace();
System.exit(1);
}
createDatabase();
List<Book> books = new ArrayList<Book>();
books.add(new Book("The Hobbit", "JRR Tolkien", "Fantasy", true));
books.add(new Book("The Fellowship of the Ring", "JRR Tolkien", "Fantasy", true));
books.add(new Book("The Eye of the World", "Robert Jordan", "Fantasy"));
books.add(new Book("A Game of Thrones", "George R. R. Martin", "Fantasy", true));
books.add(new Book("The Way of Kings", "Brandon Sanderson", "Fantasy"));
// Create database entries
createDatabaseEntries(books);
SpringApplication.run(Application.class, args);
}
public static void createDatabase() {
try (Connection connection = DriverManager.getConnection(connectionString);
Statement stmt = connection.createStatement()) {
// Create tables if they don't exist
stmt.execute(
"CREATE TABLE IF NOT EXISTS Books (id INTEGER PRIMARY KEY, name TEXT NOT NULL, author TEXT NOT NULL, read INTEGER, UNIQUE(name))");
} catch (SQLException error) {
error.printStackTrace();
System.exit(1);
}
}
public static void createDatabaseEntries(List<Book> books) {
try (Connection connection = DriverManager.getConnection(connectionString)) {
String query = "INSERT INTO Books (name, author, genre, read) VALUES(?, ?, ?, ?)";
for (Book book : books) {
try (PreparedStatement prepStmt = connection.prepareStatement(query);) {
prepStmt.setString(1, book.name);
prepStmt.setString(2, book.author);
prepStmt.setInt(3, book.read? 1 : 0);
prepStmt.setString(4, book.genre);
prepStmt.executeUpdate();
} catch (SQLException error) {
logger.warn("Failed to create book (already exists?) :: " + book.name);
}
}
} catch (SQLException error) {
error.printStackTrace();
System.exit(2);
}
}
}