Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package helpers;
- import exception.JDBCConnectionException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.util.ResourceBundle;
- public class JdbcConnector {
- Connection connection;
- String url;
- String driver;
- String user;
- String password;
- public JdbcConnector() {
- ResourceBundle resourceBundle = ResourceBundle.getBundle("database");
- url = resourceBundle.getString("url");
- driver = resourceBundle.getString("driver");
- user = resourceBundle.getString("user");
- password = resourceBundle.getString("password");
- }
- public void startConnection() throws JDBCConnectionException {
- try {
- Class.forName(driver).newInstance();
- connection = DriverManager.getConnection(url, user, password);
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- throw new JDBCConnectionException("Can't load database driver!", e);
- } catch (SQLException e) {
- throw new JDBCConnectionException("Can't connect to database!", e);
- }
- if (connection == null)
- throw new JDBCConnectionException("Driver type is not correct in URL " +
- url + "!");
- }
- public Connection getConnection() {
- return connection;
- }
- public void closeConnection() throws JDBCConnectionException {
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException e) {
- throw new JDBCConnectionException("Can't close connection!", e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement