Skip to content

Commit d67f396

Browse files
committed
Build MySQL link implementation
1 parent 24ce611 commit d67f396

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
<dependency>
5858
<groupId>ch.qos.logback</groupId>
5959
<artifactId>logback-classic</artifactId>
60-
<version>1.2.3</version>
60+
<version>1.4.4</version>
6161
</dependency>
6262
<dependency>
6363
<groupId>org.apache.commons</groupId>

src/main/java/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@
66
requires jdk.httpserver;
77
requires org.jetbrains.annotations;
88
requires slf4j.api;
9+
requires java.sql;
10+
requires mysql.connector.java;
911
}

src/main/java/org/linktechtips/Main.java

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.linktechtips.process.network.SystemInterface;
2828
import org.linktechtips.support.Support;
2929
import org.slf4j.Logger;
30+
import java.sql.Connection;
31+
import java.sql.DriverManager;
3032

3133
import java.io.File;
3234
import java.io.FileOutputStream;
@@ -61,9 +63,10 @@ public class Main {
6163
private String whazzupFile;
6264
private String whazzupjsonFile;
6365
private String mysqlmode;
64-
private String sqlurl;
65-
private String sqluser;
66-
private String sqlpassword;
66+
private static String sqlurl;
67+
private static String sqluser;
68+
private static String sqlpassword;
69+
private static Connection conn = null;
6770
private long timer;
6871
private long prevNotify;
6972
private long prevLagCheck;
@@ -140,21 +143,37 @@ public void doChecks() {
140143
ConfigEntry entry;
141144
ConfigGroup sysgroup = configManager.getGroup("system");
142145
if (sysgroup != null) {
143-
if ((entry = sysgroup.getEntry("certificates")) != null) {
144-
certFile = entry.getData().toUpperCase();
145-
Path file = Paths.get(certFile);
146-
if (Files.exists(file)) {
147-
try {
148-
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
149-
long fileLastModified = attr.lastModifiedTime().toMillis();
150-
prevCertCheck = now;
151-
if (certFileStat != fileLastModified) {
152-
certFileStat = fileLastModified;
146+
if (Objects.equals(mysqlmode, "false")) {
147+
if ((entry = sysgroup.getEntry("certificates")) != null) {
148+
certFile = entry.getData().toUpperCase();
149+
Path file = Paths.get(certFile);
150+
if (Files.exists(file)) {
151+
try {
152+
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
153+
long fileLastModified = attr.lastModifiedTime().toMillis();
154+
prevCertCheck = now;
155+
if (certFileStat != fileLastModified) {
156+
certFileStat = fileLastModified;
157+
readCert();
158+
}
159+
} catch (IOException e) {
160+
LOGGER.warn("[BetterFSD]: Read cert file info failed.", e);
161+
}
162+
}
163+
}
164+
} else if (Objects.equals(mysqlmode, "true")) {
165+
if ((sysgroup.getEntry("sqlurl")) != null) {
166+
if ((sysgroup.getEntry("sqluser")) != null){
167+
if (sysgroup.getEntry("sqlpassword") != null) {
153168
readCert();
169+
} else {
170+
LOGGER.error("[BetterFSD]: Read mysql password info failed");
154171
}
155-
} catch (IOException e) {
156-
LOGGER.warn("[BetterFSD]: Read cert file info failed.", e);
172+
} else {
173+
LOGGER.error("[BetterFSD]: Read mysql user info failed");
157174
}
175+
} else {
176+
LOGGER.error("[BetterFSD]: Read mysql url info failed");
158177
}
159178
}
160179
}
@@ -462,7 +481,17 @@ void handleCidLine(@NotNull String line) {
462481
}
463482
if (serverInterface != null) serverInterface.sendCert("*", mode, tempcert, null);
464483
}
465-
484+
public static Connection getConn() {
485+
try {
486+
Class.forName("com.mysql.cj.jdbc.Driver");
487+
conn = DriverManager.getConnection(sqlurl, sqluser, sqlpassword);
488+
LOGGER.info("[BetterFSD]: Database connection established");
489+
} catch (Exception e) {
490+
LOGGER.error("[BetterFSD]: N/A Exception on loading mysql");
491+
e.printStackTrace();
492+
}
493+
return conn;
494+
}
466495
void readCert() {
467496
if (Objects.equals(mysqlmode, "false")) {
468497
if (StringUtils.isBlank(certFile)) return;
@@ -491,7 +520,7 @@ void readCert() {
491520
}
492521
}
493522
} else if (Objects.equals(mysqlmode, "true")) {
494-
//TODO
523+
getConn();
495524
}
496525
}
497526

0 commit comments

Comments
 (0)