Skip to content

Commit

Permalink
added executeUpdate method to connections
Browse files Browse the repository at this point in the history
  • Loading branch information
KDesp73 committed Sep 25, 2023
1 parent 51f68a4 commit 2ecb536
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ public interface DatabaseConnection {
public void connect(String url, String username, String password);

/**
* Executes the SQL query if it's valid
* Executes the SQL query if it's valid (For SELECT)
* @param query
* @return ResultSet
*/
ResultSet executeQuery(String query);

/**
* Executes the SQL query if it's valid (For INSERT, UPDATE, DELETE etc)
* @param query
* @return
*/
int executeUpdate(String query);

/**
* Close the connection with the database
*/
Expand Down
101 changes: 57 additions & 44 deletions src/main/java/kdesp73/databridge/connections/MSAccessConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,70 +7,83 @@
import java.sql.SQLException;

public class MSAccessConnection implements DatabaseConnection {
private Connection connection;
private Connection connection;

/**
* Creates the connection with the database
* @param url driver connector (jdbc:ucanaccess://) + path to database
*
* @param url driver connector (jdbc:ucanaccess://) + path to database
* @param username
* @param password
*/
@Override
public void connect(String url, String username, String password) {
if(!url.contains("jdbc:ucanaccess://")){
@Override
public void connect(String url, String username, String password) {
if (!url.contains("jdbc:ucanaccess://")) {
url = "jdbc:ucanaccess://" + url;
}

try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
throw new RuntimeException("Failed to connect to Microsoft Access database.");
}
}
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
throw new RuntimeException("Failed to connect to Microsoft Access database.");
}
}


/**
* Executes the SQL query if it's valid (For INSERT, UPDATE, DELETE etc)
*/
@Override
public int executeUpdate(String query) {
try (PreparedStatement statement = connection.prepareStatement(query)) {
if (query.toLowerCase().contains("insert") || query.toLowerCase().contains("update")
|| query.toLowerCase().contains("delete")) {
return statement.executeUpdate();
} else {
throw new IllegalArgumentException("Only INSERT, UPDATE, and DELETE statements are allowed.");
}
} catch (SQLException e) {
e.printStackTrace();
// You can handle SQLException differently based on your needs.
throw new RuntimeException("Error executing query: " + e.getMessage());
}
}

/**
* Executes the SQL query if it's valid
*
* @param query
* @return ResultSet
*/
@Override
public ResultSet executeQuery(String query) {
if(query.contains("INSERT") || query.contains("insert") || query.contains("UPDATE") || query.contains("update") || query.contains("DELETE") || query.contains("delete")){
try {
PreparedStatement statement = connection.prepareStatement(query);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Error executing query: " + e.getMessage());
@Override
public ResultSet executeQuery(String query) {
try (PreparedStatement statement = connection.prepareStatement(query)) {
if (query.toLowerCase().contains("select")) {
return statement.executeQuery();
} else {
throw new IllegalArgumentException("Only SELECT statements are allowed.");
}

return null;
} catch (SQLException e) {
e.printStackTrace();
// You can handle SQLException differently based on your needs.
throw new RuntimeException("Error executing query: " + e.getMessage());
}

try {
PreparedStatement statement = connection.prepareStatement(query);
return statement.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Error executing query: " + e.getMessage());
}
}
}

/**
* Close the connection with the database
*/
@Override
public void close() {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Error closing database connection: " + e.getMessage());
}
}
@Override
public void close() {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Error closing database connection: " + e.getMessage());
}
}
}

19 changes: 19 additions & 0 deletions src/main/java/kdesp73/databridge/connections/SQLiteConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ public void connect(String url, String username, String password) {
}
}

/**
* Executes the SQL query if it's valid (For INSERT, UPDATE, DELETE etc)
*/
@Override
public int executeUpdate(String query) {
try (PreparedStatement statement = connection.prepareStatement(query)) {
if (query.toLowerCase().contains("insert") || query.toLowerCase().contains("update")
|| query.toLowerCase().contains("delete")) {
return statement.executeUpdate();
} else {
throw new IllegalArgumentException("Only INSERT, UPDATE, and DELETE statements are allowed.");
}
} catch (SQLException e) {
e.printStackTrace();
// You can handle SQLException differently based on your needs.
throw new RuntimeException("Error executing query: " + e.getMessage());
}
}

/**
* Executes the SQL query if it's valid
* @param query
Expand Down

0 comments on commit 2ecb536

Please sign in to comment.