Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package md.sw.util.sql;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- public class SqlConnector {
- private final String url;
- private final String user;
- private final String password;
- private Connection connection;
- private SqlConnector(final String url, final String user, final String password) {
- this.url = url;
- this.user = user;
- this.password = password;
- }
- public static SqlConnector build(String url, String user, String password) {
- return new SqlConnector(url, user, password);
- }
- public PreparedStatement preparedStatement(String sql) throws SQLException {
- return connection.prepareStatement(sql);
- }
- public int insert(String sql) throws SQLException {
- if(isConnected()) {
- return preparedStatement(sql).executeUpdate();
- }
- return 0;
- }
- public ResultSet select(String sql) throws SQLException {
- if(isConnected()) {
- return preparedStatement(sql).executeQuery();
- }
- return null;
- }
- public void open() throws SQLException {
- if(!isConnected()) {
- connection = DriverManager.getConnection(url, user, password);
- }
- }
- public void close() throws SQLException {
- if(isConnected()) {
- connection.close();
- }
- }
- public boolean isConnected() {
- return connection != null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement