@@ -25,6 +25,7 @@ public class DManager {
2525 public DManager (GSitMain GPluginMain ) { GPM = GPluginMain ; }
2626
2727 public boolean connect () {
28+ if (isConnected ()) return true ;
2829 File dataFile = new File (GPM .getDataFolder (), "data/data.yml" );
2930 if (!dataFile .exists ()) GPM .saveResource ("data/data.yml" , false );
3031 FileConfiguration dataConfig = YamlConfiguration .loadConfiguration (dataFile );
@@ -37,6 +38,13 @@ public boolean connect() {
3738 return reconnect ();
3839 }
3940
41+ public boolean isConnected () {
42+ try {
43+ if (connection != null && !connection .isClosed ()) return true ;
44+ } catch (SQLException ignored ) { }
45+ return false ;
46+ }
47+
4048 private boolean reconnect () {
4149 try {
4250 if (type .equals ("sqlite" )) Class .forName ("org.sqlite.JDBC" );
@@ -67,22 +75,26 @@ private Connection getConnection(boolean WithDatabase) throws SQLException {
6775 return null ;
6876 }
6977
70- public boolean execute (String Query , Object ... Data ) throws SQLException {
71- if ( connection == null ) throw new SQLException ( "missing " + type + " database connection" );
72- if ( connection .isClosed () && ! reconnect ()) return false ;
73- PreparedStatement preparedStatement = connection . prepareStatement ( Query );
74- for ( int i = 1 ; i <= Data . length ; i ++) preparedStatement .setObject ( i , Data [ i - 1 ] );
75- return preparedStatement . execute ();
78+ public void execute (String Query , Object ... Data ) throws SQLException {
79+ ensureConnection ( );
80+ try ( PreparedStatement preparedStatement = connection .prepareStatement ( Query )) {
81+ for ( int i = 1 ; i <= Data . length ; i ++) preparedStatement . setObject ( i , Data [ i - 1 ] );
82+ preparedStatement .executeUpdate ( );
83+ }
7684 }
7785
7886 public ResultSet executeAndGet (String Query , Object ... Data ) throws SQLException {
79- if (connection == null ) throw new SQLException ("missing " + type + " database connection" );
80- if (connection .isClosed () && !reconnect ()) return null ;
87+ ensureConnection ();
8188 PreparedStatement preparedStatement = connection .prepareStatement (Query );
8289 for (int i = 1 ; i <= Data .length ; i ++) preparedStatement .setObject (i , Data [i - 1 ]);
8390 return preparedStatement .executeQuery ();
8491 }
8592
86- public void close () { try { if (connection != null ) connection .close (); } catch (SQLException ignored ) { } }
93+ private void ensureConnection () throws SQLException {
94+ if (isConnected ()) return ;
95+ if (!reconnect ()) throw new SQLException ("Failed to reconnect to the " + type + " database." );
96+ }
97+
98+ public void close () { try { if (connection != null && !connection .isClosed ()) connection .close (); } catch (SQLException ignored ) { } }
8799
88100}
0 commit comments