Package net.risingworld.api.database
Interface Database
- All Superinterfaces:
AutoCloseable
Database interface which represents a connection to an SQLite or MySQL database.
Example: Full example of how you may use an SQLite database
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this connection.void
Executes the given SQL statement, for example a CREATE or DELETE statement.executeQuery
(String sql) Executes the given SQL statement and returns aResultSet
object.
Remember to close the ResultSet once you're ready (or use a try-with-resources statement).void
executeUpdate
(String sql) Executes the given SQL statement, which may be an INSERT, UPDATE or DELETE statement.Gets the underlyingConnection
object, which represents the connection to the specific database.getType()
Gets the database type.
-
Method Details
-
getType
DatabaseType getType()Gets the database type.- Returns:
- the database type, SQLite or MySQL
-
getConnection
Connection getConnection()Gets the underlyingConnection
object, which represents the connection to the specific database. This provides full access to the database. Be careful when changing any settings of this connection;- Returns:
- a
Connection
object. - Example: Create a PreparedStatement and insert a value in an existing database
-
execute
Executes the given SQL statement, for example a CREATE or DELETE statement.- Parameters:
sql
- the SQL statement.- Example: Create a custom SQLite connection (creates new database if it does not exist)
-
executeUpdate
Executes the given SQL statement, which may be an INSERT, UPDATE or DELETE statement.- Parameters:
sql
- the SQL statement, INSERT, UPDATE or DELETE.- Example: Update an entry in an existing database
- Example: Create and insert or update an entry (UPSERT)
-
executeQuery
Executes the given SQL statement and returns aResultSet
object.
Remember to close the ResultSet once you're ready (or use a try-with-resources statement).- Parameters:
sql
- the SQL statement, typically a SELECT statement.- Returns:
- a ResultSet object containing the data produced by the given SQL statement.
- Throws:
SQLException
- If a database access error occurs.- See Also:
-
close
void close()Closes this connection. Call this method if the connection isn't needed anymore.- Specified by:
close
in interfaceAutoCloseable
-