Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package databaseConnection;
- import java.sql.*;
- /**
- * Created by pawel on 25.03.2017.
- */
- abstract class DatabaseQuery {
- public static final String JDBC_URL = "jdbc:hsqldb:file:data/rozkazer.db";
- public static final String USER = "SA";
- public static final String PASSWORD = "";
- public static final String DRIVER_CLASSNAME = "org.hsqldb.jdbcDriver";
- private String sql;
- private Connection connection;
- private Statement statement;
- protected ResultSet resultSet;
- public DatabaseQuery(String sql){
- this.sql = sql;
- }
- public void execute(){
- try {
- connectToDatabase();
- executeSql();
- process();
- close();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private void close() throws SQLException {
- resultSet.close();
- statement.execute("SHUTDOWN");
- statement.close();
- connection.close();
- }
- abstract protected void process() throws SQLException;
- private void executeSql() throws SQLException {
- statement = connection.createStatement();
- resultSet = statement.executeQuery(sql);
- }
- private void connectToDatabase() throws ClassNotFoundException, SQLException {
- Class.forName(DRIVER_CLASSNAME);
- connection = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
- }
- }
Add Comment
Please, Sign In to add comment