2727import org .linktechtips .process .network .SystemInterface ;
2828import org .linktechtips .support .Support ;
2929import org .slf4j .Logger ;
30- import java . sql . Connection ;
31- import java .sql .DriverManager ;
30+
31+ import java .sql .* ;
3232
3333import java .io .File ;
3434import java .io .FileOutputStream ;
@@ -64,6 +64,7 @@ public class Main {
6464 private String whazzupjsonFile ;
6565 private String mysqlmode ;
6666 private static String sqlurl ;
67+ private static String sqltable ;
6768 private static String sqluser ;
6869 private static String sqlpassword ;
6970 private static Connection conn = null ;
@@ -433,6 +434,9 @@ private void configure() {
433434 if ((entry = system .getEntry ("sqlurl" )) != null ) {
434435 sqlurl = entry .getData ();
435436 }
437+ if ((entry = system .getEntry ("sqltable" )) != null ) {
438+ sqltable = entry .getData ();
439+ }
436440 if ((entry = system .getEntry ("sqluser" )) != null ) {
437441 sqluser = entry .getData ();
438442 }
@@ -448,11 +452,8 @@ private void configure() {
448452 whazzupjsonFile = entry .getData ();
449453 }
450454 }
451-
452455 configMyServer ();
453-
454456 readCert ();
455-
456457}
457458
458459 void handleCidLine (@ NotNull String line ) {
@@ -481,16 +482,14 @@ void handleCidLine(@NotNull String line) {
481482 }
482483 if (serverInterface != null ) serverInterface .sendCert ("*" , mode , tempcert , null );
483484 }
484- public static Connection getConn () {
485+ public static void getConn () {
485486 try {
486487 Class .forName ("com.mysql.cj.jdbc.Driver" );
487488 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" );
489+ } catch (ClassNotFoundException | SQLException e ) {
490+ LOGGER .info ("[BetterFSD]: Exception in get mysql conn" );
491491 e .printStackTrace ();
492492 }
493- return conn ;
494493 }
495494 void readCert () {
496495 if (Objects .equals (mysqlmode , "false" )) {
@@ -521,6 +520,25 @@ void readCert() {
521520 }
522521 } else if (Objects .equals (mysqlmode , "true" )) {
523522 getConn ();
523+ LOGGER .info ("[BetterFSD]: Reading certificates from database" );
524+ if (StringUtils .isEmpty (sqltable )) {
525+ LOGGER .error ("[BetterFSD]: sqltable is empty. Please check your config file" );
526+ } else if (sqltable != null ) try {
527+ String sql = "SELECT * FROM " + sqltable ;
528+ PreparedStatement preparedStatement = conn .prepareStatement (sql );
529+ ResultSet resultSet = preparedStatement .executeQuery ();
530+ while (resultSet .next ()) {
531+ Certificate tempcert = new Certificate (resultSet .getString ("cid" ), resultSet .getString ("password" ),
532+ resultSet .getInt ("level" ), Support .mgmtime (), Server .myServer .getIdent ());
533+ serverInterface .sendCert ("*" , ProtocolConstants .CERT_ADD , tempcert , null );
534+ }
535+ preparedStatement .close ();
536+ resultSet .close ();
537+ conn .close ();
538+ } catch (Exception e ) {
539+ LOGGER .error ("[BetterFSD]: N/A Exception on reading mysql" );
540+ e .printStackTrace ();
541+ }
524542 }
525543 }
526544
0 commit comments