Skip to content
This repository was archived by the owner on Jun 15, 2024. It is now read-only.

Commit d872b84

Browse files
committed
+added log4j2, +changed Databaseconnection to Singleton pattern, +moved default.properties to src\main\resources\default.properties
1 parent b0a1500 commit d872b84

File tree

9 files changed

+42
-26
lines changed

9 files changed

+42
-26
lines changed

Diff for: .idea/modules/PIC-DB.main.iml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: .idea/modules/PIC-DB.test.iml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: build.gradle

+3-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group 'at.technikum'
9-
version '0.0.1-alpha'
9+
version '0.0.2-alpha'
1010

1111
sourceCompatibility = 14
1212

@@ -18,6 +18,8 @@ dependencies {
1818
testCompile group: 'junit', name: 'junit', version: '4.13'
1919
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
2020
implementation group: 'mysql', name: 'mysql-connector-java', version: '5.1.13'
21+
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.1'
22+
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.1'
2123
}
2224

2325
javafx {
@@ -38,18 +40,6 @@ sourceSets.main {
3840
}
3941
}
4042

41-
/*
42-
jar {
43-
manifest {
44-
attributes(
45-
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
46-
'Main-Class': mainClassName
47-
)
48-
}
49-
}
50-
51-
*/
52-
5343
// resolved Fat Jar Issue !!
5444
// https://stackoverflow.com/questions/52569724/javafx-11-create-a-jar-file-with-gradle/52571719#52571719
5545
mainClassName='at.technikum.Main'

Diff for: src/main/java/at/technikum/ApplicationFX.java

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
package at.technikum;
22

33
import at.technikum.Business.BusinessLayerImpl;
4-
import at.technikum.DataAccessLayer.Database;
5-
import at.technikum.Model.PictureModelImpl;
64
import at.technikum.interfaces.BusinessLayer;
75
import at.technikum.interfaces.models.PictureModel;
86
import javafx.application.Application;
97
import javafx.fxml.FXMLLoader;
108
import javafx.scene.Parent;
119
import javafx.scene.Scene;
1210
import javafx.stage.Stage;
11+
import org.apache.logging.log4j.LogManager;
12+
import org.apache.logging.log4j.Logger;
1313

1414
import java.io.IOException;
1515

1616
public class ApplicationFX extends Application {
1717

18+
private static final Logger logger = LogManager.getLogger(ApplicationFX.class);
19+
1820
public static void main(String[] args) throws Exception {
19-
Database db = new Database();
20-
db.connect();
21-
/*BusinessLayer businessLayer = new BusinessLayerImpl();
21+
22+
logger.debug("Hello from Log4j 2");
23+
24+
BusinessLayer businessLayer = new BusinessLayerImpl();
2225
PictureModel pictureModel = businessLayer.getPicture(2);
2326

2427
System.out.println("ID: " + pictureModel.getID() + " , name: "
25-
+ pictureModel.getFileName());*/
28+
+ pictureModel.getFileName());
29+
2630
launch(args);
2731
}
2832

Diff for: src/main/java/at/technikum/DataAccessLayer/DALFactory.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ public static DataAccessLayer getDAL() {
1616
if(useMock) {
1717
return new DALMockImpl();
1818
} else {
19-
Database db = new Database();
20-
Connection connection = db.connect();
19+
Connection connection = Database.getConnection();
2120
return new DALImpl(connection);
2221
}
2322
}

Diff for: src/main/java/at/technikum/DataAccessLayer/Database.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@
88
import java.util.Properties;
99

1010
public class Database {
11+
12+
static Connection connection = null;
13+
1114
/**
1215
* Connect to the MySQL database
1316
*
1417
* @return a Connection object
1518
*/
16-
public Connection connect() {
17-
19+
public static Connection getConnection() {
20+
if (connection != null) return connection;
21+
1822
Properties properties = Configurations.readProperties();
1923

2024
String url = properties.getProperty("db.url");
2125
String user = properties.getProperty("db.user");
2226
String passwd = properties.getProperty("db.passwd");
2327

24-
Connection connection = null;
25-
2628
try {
2729
Class.forName("com.mysql.jdbc.Driver");
2830
connection = DriverManager.getConnection(url, user, passwd);

Diff for: src/main/java/at/technikum/Utils/Configurations.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public class Configurations {
1616
public static Properties readProperties() {
1717
// create and load default properties Properties
1818
java.util.Properties defaultProps = new java.util.Properties();
19-
Path defaultPropsPath = Paths.get("default.properties");
19+
Path defaultPropsPath = Paths.get(System.getProperty("user.dir") +
20+
"\\src\\main\\resources\\default.properties");
2021

2122
try {
2223
BufferedReader bf = Files.newBufferedReader(defaultPropsPath, StandardCharsets.UTF_8);
File renamed without changes.

Diff for: src/main/resources/log4j2.xml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="WARN">
3+
<Appenders>
4+
<Console name="LogToConsole" target="SYSTEM_OUT">
5+
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
6+
</Console>
7+
</Appenders>
8+
<Loggers>
9+
<Logger name="at.technikum" level="debug" additivity="false">
10+
<AppenderRef ref="LogToConsole"/>
11+
</Logger>
12+
<Root level="error">
13+
<AppenderRef ref="LogToConsole"/>
14+
</Root>
15+
</Loggers>
16+
</Configuration>

0 commit comments

Comments
 (0)