From a37af0c5337af51e0631056ee226d3016c2aad2f Mon Sep 17 00:00:00 2001 From: Eirik Steira <eirsteir@stud.ntnu.no> Date: Mon, 9 Mar 2020 11:57:22 +0100 Subject: [PATCH 1/2] Setup initial database connection with connection pool with HikariCP --- .gitignore | 1 + ImageApplication.iml | 17 +++++++-- pom.xml | 10 +++++ src/main/java/NTNU/IDATT1002/App.java | 8 +++- .../NTNU/IDATT1002/database/Database.java | 37 +++++++++++++++++++ 5 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 src/main/java/NTNU/IDATT1002/database/Database.java diff --git a/.gitignore b/.gitignore index 14e974d2..e48e2dfd 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ target/ .idea/ +*.properties # User-specific stuff .idea/**/workspace.xml diff --git a/ImageApplication.iml b/ImageApplication.iml index 17c32733..ef51d496 100644 --- a/ImageApplication.iml +++ b/ImageApplication.iml @@ -1,5 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> + <component name="ExternalSystem" externalSystem="Maven" /> <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11"> <output url="file://$MODULE_DIR$/target/classes" /> <output-test url="file://$MODULE_DIR$/target/test-classes" /> @@ -10,13 +11,21 @@ </content> <orderEntry type="inheritedJdk" /> <orderEntry type="sourceFolder" forTests="false" /> - <orderEntry type="library" name="Maven: org.openjfx:javafx-controls:13" level="project" /> <orderEntry type="library" name="Maven: org.openjfx:javafx-controls:mac:13" level="project" /> - <orderEntry type="library" name="Maven: org.openjfx:javafx-graphics:13" level="project" /> <orderEntry type="library" name="Maven: org.openjfx:javafx-graphics:mac:13" level="project" /> - <orderEntry type="library" name="Maven: org.openjfx:javafx-base:13" level="project" /> <orderEntry type="library" name="Maven: org.openjfx:javafx-base:mac:13" level="project" /> - <orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:13" level="project" /> <orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:mac:13" level="project" /> + <orderEntry type="library" name="Maven: org.openjfx:javafx-controls:13" level="project" /> + <orderEntry type="library" name="Maven: org.openjfx:javafx-controls:win:13" level="project" /> + <orderEntry type="library" name="Maven: org.openjfx:javafx-graphics:13" level="project" /> + <orderEntry type="library" name="Maven: org.openjfx:javafx-graphics:win:13" level="project" /> + <orderEntry type="library" name="Maven: org.openjfx:javafx-base:13" level="project" /> + <orderEntry type="library" name="Maven: org.openjfx:javafx-base:win:13" level="project" /> + <orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.19" level="project" /> + <orderEntry type="library" name="Maven: com.google.protobuf:protobuf-java:3.6.1" level="project" /> + <orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:13" level="project" /> + <orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:win:13" level="project" /> + <orderEntry type="library" name="Maven: com.zaxxer:HikariCP:3.4.2" level="project" /> + <orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" /> </component> </module> \ No newline at end of file diff --git a/pom.xml b/pom.xml index fba6fe0e..e0520b1a 100644 --- a/pom.xml +++ b/pom.xml @@ -15,11 +15,21 @@ <artifactId>javafx-controls</artifactId> <version>13</version> </dependency> + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <version>8.0.19</version> + </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-fxml</artifactId> <version>13</version> </dependency> + <dependency> + <groupId>com.zaxxer</groupId> + <artifactId>HikariCP</artifactId> + <version>3.4.2</version> + </dependency> </dependencies> <build> <plugins> diff --git a/src/main/java/NTNU/IDATT1002/App.java b/src/main/java/NTNU/IDATT1002/App.java index 1edc0bd5..942250f2 100644 --- a/src/main/java/NTNU/IDATT1002/App.java +++ b/src/main/java/NTNU/IDATT1002/App.java @@ -1,5 +1,6 @@ package NTNU.IDATT1002; +import NTNU.IDATT1002.database.Database; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; @@ -7,6 +8,8 @@ import javafx.scene.Scene; import javafx.stage.Stage; import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; public class App extends Application { @@ -14,10 +17,13 @@ public class App extends Application { private static Scene scene; @Override - public void start(Stage stage) throws IOException { + public void start(Stage stage) throws IOException, SQLException { scene = new Scene(loadFXML("login")); stage.setScene(scene); stage.show(); + + Connection connection = Database.getConnection(); + System.out.println(connection.isValid(1000)); } static void setRoot(String fxml) throws IOException { diff --git a/src/main/java/NTNU/IDATT1002/database/Database.java b/src/main/java/NTNU/IDATT1002/database/Database.java new file mode 100644 index 00000000..e8ff1c43 --- /dev/null +++ b/src/main/java/NTNU/IDATT1002/database/Database.java @@ -0,0 +1,37 @@ +package NTNU.IDATT1002.database; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; + +import java.sql.Connection; +import java.sql.SQLException; +import java.util.logging.Logger; + +/** + * TODO: + * java.sql.SQLException: Access denied for user 'eirsteir_root'@'dhcp-10-22-12-209.wlan.ntnu.no' (using password: YES) + */ +public class Database { + + private static final Logger logger = Logger.getLogger(Database.class.getName()); + private static HikariConfig config; + private static HikariDataSource dataSource; + + /** + * Load configuration and setup the data source + */ + static{ + config = new HikariConfig("datasource.properties" );; + dataSource = new HikariDataSource(config); + } + + /** + * Gets a connection from the HikariCP connection pool + * @return Connection to the database + * @throws SQLException + */ + public static Connection getConnection() throws SQLException { + return dataSource.getConnection(); + } + +} \ No newline at end of file -- GitLab From c3b51943c5bac0e1f01caf3e7c29705cdc32d078 Mon Sep 17 00:00:00 2001 From: Eirik Steira <eirsteir@stud.ntnu.no> Date: Tue, 10 Mar 2020 14:33:06 +0100 Subject: [PATCH 2/2] Add logging, refactor DBConnection, add javadoc Remove logger from DBConnection Remove unused code in DBConnection Update readme Get Started instructions Fix typo in docstrings DBConnection --- ImageApplication.iml | 3 ++- README.md | 2 +- pom.xml | 5 +++++ src/main/java/NTNU/IDATT1002/App.java | 10 ++-------- .../{Database.java => DBConnection.java} | 16 ++++++---------- 5 files changed, 16 insertions(+), 20 deletions(-) rename src/main/java/NTNU/IDATT1002/database/{Database.java => DBConnection.java} (56%) diff --git a/ImageApplication.iml b/ImageApplication.iml index ef51d496..e89efabf 100644 --- a/ImageApplication.iml +++ b/ImageApplication.iml @@ -15,6 +15,8 @@ <orderEntry type="library" name="Maven: org.openjfx:javafx-graphics:mac:13" level="project" /> <orderEntry type="library" name="Maven: org.openjfx:javafx-base:mac:13" level="project" /> <orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:mac:13" level="project" /> + <orderEntry type="library" name="Maven: org.slf4j:slf4j-simple:1.7.30" level="project" /> + <orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.30" level="project" /> <orderEntry type="library" name="Maven: org.openjfx:javafx-controls:13" level="project" /> <orderEntry type="library" name="Maven: org.openjfx:javafx-controls:win:13" level="project" /> <orderEntry type="library" name="Maven: org.openjfx:javafx-graphics:13" level="project" /> @@ -26,6 +28,5 @@ <orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:13" level="project" /> <orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:win:13" level="project" /> <orderEntry type="library" name="Maven: com.zaxxer:HikariCP:3.4.2" level="project" /> - <orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" /> </component> </module> \ No newline at end of file diff --git a/README.md b/README.md index 0350b632..f9fbb2ae 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ #### IntelliJ: *Follow these steps to run the project:* -- Open the Maven Projects window in `View -> Tool Windows -> Maven` and click on `ImageApplication -> Plugins -> javafx -> javafx:run` to execute the project +- Open the Maven Projects window in `View -> Tool Windows -> Maven` and click on `ImageApplication -> Plugins -> javafx -> javafx:compile` and then `javafx:run` to execute the project ## Questions or need help *Link to [our wiki](https://gitlab.stud.idi.ntnu.no/eirsteir/team-14-software-engineering/-/wikis/home) page, maybe add contact info* diff --git a/pom.xml b/pom.xml index e0520b1a..7520166c 100644 --- a/pom.xml +++ b/pom.xml @@ -10,6 +10,11 @@ <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> + <dependency> + <groupId>org.slf4j</groupId> + <artifactId>slf4j-simple</artifactId> + <version>1.7.30</version> + </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> diff --git a/src/main/java/NTNU/IDATT1002/App.java b/src/main/java/NTNU/IDATT1002/App.java index 942250f2..0e62956a 100644 --- a/src/main/java/NTNU/IDATT1002/App.java +++ b/src/main/java/NTNU/IDATT1002/App.java @@ -1,6 +1,6 @@ package NTNU.IDATT1002; -import NTNU.IDATT1002.database.Database; +import NTNU.IDATT1002.database.DBConnection; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; @@ -8,22 +8,16 @@ import javafx.scene.Scene; import javafx.stage.Stage; import java.io.IOException; -import java.sql.Connection; -import java.sql.SQLException; - public class App extends Application { private static Scene scene; @Override - public void start(Stage stage) throws IOException, SQLException { + public void start(Stage stage) throws IOException { scene = new Scene(loadFXML("login")); stage.setScene(scene); stage.show(); - - Connection connection = Database.getConnection(); - System.out.println(connection.isValid(1000)); } static void setRoot(String fxml) throws IOException { diff --git a/src/main/java/NTNU/IDATT1002/database/Database.java b/src/main/java/NTNU/IDATT1002/database/DBConnection.java similarity index 56% rename from src/main/java/NTNU/IDATT1002/database/Database.java rename to src/main/java/NTNU/IDATT1002/database/DBConnection.java index e8ff1c43..294643e1 100644 --- a/src/main/java/NTNU/IDATT1002/database/Database.java +++ b/src/main/java/NTNU/IDATT1002/database/DBConnection.java @@ -5,33 +5,29 @@ import com.zaxxer.hikari.HikariDataSource; import java.sql.Connection; import java.sql.SQLException; -import java.util.logging.Logger; /** - * TODO: - * java.sql.SQLException: Access denied for user 'eirsteir_root'@'dhcp-10-22-12-209.wlan.ntnu.no' (using password: YES) - */ -public class Database { + * Class for connecting to the database. This will load configurations and create a connection pool. +*/ +public class DBConnection { - private static final Logger logger = Logger.getLogger(Database.class.getName()); - private static HikariConfig config; private static HikariDataSource dataSource; /** * Load configuration and setup the data source */ static{ - config = new HikariConfig("datasource.properties" );; + HikariConfig config = new HikariConfig("datasource.properties" ); dataSource = new HikariDataSource(config); } /** - * Gets a connection from the HikariCP connection pool + * Establish a connection pool to the database + * * @return Connection to the database * @throws SQLException */ public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } - } \ No newline at end of file -- GitLab