Skip to content

Instantly share code, notes, and snippets.

@xfn14
Created January 16, 2022 08:07
Show Gist options
  • Save xfn14/78890928d40b37617f420585face0c98 to your computer and use it in GitHub Desktop.
Save xfn14/78890928d40b37617f420585face0c98 to your computer and use it in GitHub Desktop.
Java SQL Connection
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