- Notifications
You must be signed in to change notification settings - Fork0
Simple and async java-library for mysql and mongodb database-connections
License
Heliumdioxid/database-api
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
a simple andasync java-library for mysql and mongodbdatabase-connections
It is an easy-to-use library that (until now) can connect to mysql and mongodb databases.
In addition, interactions with databases are simplified.Feel free to leave a ⭐ if you appreciate the work.
You can integrate the database-api into your project with Maven or Gradle.
Maven repository:
<repositories> <repository> <id>jitpack.io</id> <url>https://jitpack.io</url> </repository></repositories>
Maven dependencies:
<dependency> <groupId>com.github.Heliumdioxid.database-api</groupId> <artifactId>api</artifactId> <version>1.0.0-rc1</version></dependency><dependency> <groupId>com.github.Heliumdioxid.database-api</groupId> <artifactId>mysql</artifactId> <version>1.0.0-rc1</version></dependency><dependency> <groupId>com.github.Heliumdioxid.database-api</groupId> <artifactId>mongo</artifactId> <version>1.0.0-rc1</version></dependency>
Gradle repository:
maven { name'jitpack.io' url'https://jitpack.io'}
Gradle dependency:
dependencies { implementation'com.github.Heliumdioxid.database-api:api:1.0.0-rc1'}dependencies { implementation'com.github.Heliumdioxid.database-api:mysql:1.0.0-rc1'}dependencies { implementation'com.github.Heliumdioxid.database-api:mongo:1.0.0-rc1'}
First, you have to create aConnectionData
, either for a mongo or mysql connection.You need to provide a host, username, password, database-name and port (mostly 3306 for mysql and 27017 for mongodb)
ConnectionDataconnectionData =newConnectionData("127.0.0.1","username","password","database",3306);
🤖 The connection to mysql-databases should work properly.If there are any errors, it would be a great help to report them.
Moreover, you have to create some properties, which are then passed on during the connection:
MySQLConnectionConfigmySQLConnectionConfig =newMySQLConnectionConfig(connectionData);mySQLConnectionConfig.applyDefaultHikariConfig()// recommended setting for HikariCP
After that, the connection to the database can be established like this:
MySQLDatabaseConnectionmySQLDatabaseConnection =newMySQLDatabaseConnection(mySQLConnectionConfig);MySQLConnectionHandlermySQLConnectionHandler;// connect and check for a present value in the optional:mySQLConnectionHandler =mySQLDatabaseConnection.connect().join().get();// get the MySQLConnectionHandler with this method:mySQLConnectionHandler =mySQLDatabaseConnection.getConnectionHandler().get();
TheMySQLConnectionHandler
can now be used for communicating with the connected mysql-database:
UpdateResultupdateResult =mySQLConnectionHandler.executeUpdate("query");// execute an update// executes a query and applies the result to a functional interfacebooleanincluded =mySQLConnectionHandler.executeQuery(resultSet ->resultSet.next(),false,"query");
The connection can finally be closed by calling this method:
mySQLDatabaseConnection.disconnect();
🤖 The connection to mongo-databases was not testet yet.If you have tried it, I would be very grateful if you would contact me.
Moreover, you have to create some properties, which are then passed on during the connection:
MongoConnectionConfigmongoConnectionConfig =newMongoConnectionConfig(connectionData);mongoConnectionConfig.applyDefaultMongoClientSettings();// applies default properties like the uri
After that, the connection to the database can be established like this:
MongoDatabaseConnectionmongoDatabaseConnection =newMongoDatabaseConnection(mongoConnectionConfig);MongoConnectionHandlermongoConnectionHandler;// connect and check for a present value in the optional:mongoConnectionHandler =mongoDatabaseConnection.connect().join().get();// get the MongoConnectionHandler with this method:mongoConnectionHandler =mongoDatabaseConnection.getConnectionHandler().get();
TheMongoConnectionHandler
can now be used for communicating with the connected mongo-database (few examples):
Optional<MongoCollection<Document>>mongoCollection =mongoConnectionHandler.getCollection("collection").join();Optional<Document>document =mongoConnectionHandler.getDocument("collection","fieldName","value").join();
The connection can finally be closed by calling this method:
mongoDatabaseConnection.disconnect();
🤖 Please suggest features that we should include in the api.ReadContributing for details.
Before submitting issues and pull requests please read theCONTRIBUTING.md file.Please follow these guidelines to make life easier for all of us.
database-api is distributed under the terms of theMIT License
. A complete version of the license is available in theLICENSE.md in this repository. Any contribution made to this project will be licensed under theMIT License
.
All files are Copyright © 2023 Florian H. and all contributors.
You can contact me via Discord: Heliumdioxid#3963
About
Simple and async java-library for mysql and mongodb database-connections