I am working on a project in Java which includes a lot of database related tasks. For instance, I would have to insert, update, retrieve data so many times within the application. To use code reusability, I've just written this abstraction for my database class. I'm planing to call its methods where ever its needed to perform database related tasks within the code. Please review my code. Are there any bad practices?
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.teamincredibles.settings.Settings;
public class Database {
private Connection connection;
public void connect() {
try {
Class.forName(Settings.getDatabaseDriver());
connection = DriverManager.getConnection(Settings.getJdbcUrl(), Settings.getDBUserName(), Settings.getDBPassword());
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean prepareStatement(String query, String choice1) {
boolean flag = false;
try {
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, choice1);
ResultSet rs = ps.executeQuery();
flag = rs.next();
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
public boolean prepareStatement(String query, String choice1, String choice2) {
boolean flag = false;
try {
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, choice1);
ps.setString(2, choice2);
ResultSet rs = ps.executeQuery();
flag = rs.next();
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}
public void executeUpdate(String query) {
try {
Statement statement = connection.createStatement();
statement.executeUpdate(query);
} catch (SQLException e) {
e.printStackTrace();
}
}
public boolean execute(String query) {
boolean result = false;
try {
Statement statement = connection.createStatement();
result = statement.execute(query);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public ResultSet executeQuery(String query) {
ResultSet result = null;
try {
Statement statement = connection.createStatement();
result = statement.executeQuery(query);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public void disconnect() {
try {
if (connection != null) {
connection.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}