Created
January 16, 2022 08:07
-
-
Save xfn14/78890928d40b37617f420585face0c98 to your computer and use it in GitHub Desktop.
Java SQL Connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SQLConnection { | |
private final Connection connection; | |
public SQLConnection(String url, String username, String password) throws SQLException { | |
this.connection = DriverManager.getConnection(url, username, password); | |
} | |
public ResultSet executeQuery(String query) throws SQLException { | |
Statement statement = this.connection.createStatement(); | |
return statement.executeQuery(query); | |
} | |
public int executeUpdate(String query) throws SQLException { | |
Statement statement = this.connection.createStatement(); | |
return statement.executeUpdate(query); | |
} | |
public boolean checkConnection() throws SQLException { | |
return this.connection != null && !this.connection.isClosed(); | |
} | |
public void close() throws SQLException { | |
if (this.connection == null || this.connection.isClosed()) | |
return; | |
this.connection.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment