#How can I add support for multiple databases?
1 messages ยท Page 1 of 1 (latest)
<@&987246584574140416> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Take note this is in Java.
I'd also like to use this kind of system for other databases SQL or not
If you need to ask something do not hesitate to ๐
You would create an interface for all database interactions, and write an implementation for each database you want to support
use Hibernate and just set up multiple database connection configs
then just select a different config by some condition
I've actually done that
is there a way to alternate between the connections?
Such as code examples, documentations, etc.
Here's the main interface that I use for my connections.
I uploaded your attachments as gist. That way, they are easier to read for everyone, especially mobile users ๐
And this is the one used by SQL connections such as H2, SQLite, etc.
USES HIKARICP
I uploaded your attachments as gist. That way, they are easier to read for everyone, especially mobile users ๐
Database db = switch (database) {
"h2" -> new H2Connector(),
"sqlite" -> new SQLiteConnector(),
"mongodb" -> new MongoDBConnector()
}```
something like this is what you'd use
to switch between implementations
(note that your code wouldn't work for nosql databases, even though you were talking about MongoDB in the first post, so that would at least need another layer of abstraction to work out ๐ )
So should I make like an abstract handler class that uses all these databases.. and if one is chosen from the YAML file, use that?
"public class AbstractDatabaseHandler<T>"?
And then T would connect to the database by creating the connection via interface
you'll basically need an interface with CRUD operations, and implement that interface per supported database type
as SQL shares a lot of common language, you might be able to abstract those into a subgroup (i.e. a simple select will likely be exactly the same in H2, SQLite, Mariadb, Postgres, OracleDB or MsSQL)
so start with java public interface Database<T> { void create(T entity); List<T> read(); void update(T entity); void delete(T entity); } of course, change the method signatures as you see fit, or add more methods where required
Detected code, here are some useful tools:
public interface Database<T> {
void create(T entity);
List<T> read();
void update(T entity);
void delete(T entity);
}