Everything is documented and with examples (auto generated). Add with maven and download sources to view docs inside IDE.
Create an SQLite database
Code (Java):
Database database
= SQLib.
createDatabase
(
new
File
(
"database.db"
)
)
;
Create a MySQL database
Code (Java):
Database database
= SQLib.
createDatabase
(
"database",
"localhost",
"3306",
"username",
"password"
)
;
To get a DataSource for SQLite or MySQL use SQLib#createDataSource with the same parameters as above (You most likely don't need this)
Code (Java):
DataSource dataSource
= SQLib.
createDataSource
(
new
File
(
"database.db"
)
)
;
Create a DatabaseStatement
Code (Java):
DatabaseStatement stmt
= database.
createStatement
(
"INSERT INTO players (id, name) VALUES (?, ?);"
)
;
Add a PreparedStatement parameter
Code (Text):
stmt.withParameter(1);
Add a PreparedStatement option
Code (Text):
stmt.addOption(pStmt -> pStmt.setString(2, "Mark"))
Create a DataTable
Code (Java):
DataTable dataTable
= database.
tableOf
(
"players"
)
;
Alter table
Code (Java):
dataTable.
create
(
String...
columns
)
;
dataTable.
createIfNotExists
(
String...
columns
)
;
dataTable.
drop
(
)
;
// removes the table from the database
dataTable.
rename
(
String newName
)
;
dataTable.
clear
(
)
;
dataTable.
addColumn
(
String columnDefinition
)
;
dataTable.
renameColumn
(
String oldName,
String newName
)
;
Update table
Code (Java):
//Warning: some of these statements require you to set parameters before execution. See PreparedStatement jdbc.
dataTable.
insert
(
String clause
).
executeUpdate
(
)
;
dataTable.
insertInto
(
String...
columns
)
%SET_PARAMETERS
%.
executeUpdate
(
)
;
dataTable.
insertAll
(
String...
values
).
executeUpdate
(
)
;
dataTable.
insertAll
(
int parameters
)
%SET_PARAMETERS
%.
executeUpdate
(
)
;
dataTable.
set
(
String clause
).
executeUpdate
(
)
;
dataTable.
delete
(
String clause
).
executeUpdate
(
)
;
Query table
Code (Java):
//Selects all columns for the rows included in the clause
dataTable.
select
(
String clause
).
executeQuery
(resultSet
->
{
}
)
;
//Selects the specified columns for the rows included in the clause
dataTable.
select
(
String clause,
String...
columns
).
executeQuery
(resultSet
->
{
}
)
;
//Selects all rows and all columns
dataTable.
selectAll
(
).
executeQuery
(resultSet
->
{
}
)
;
//Selects the specified columns for all rows
dataTable.
selectAll
(
String...
columns
).
executeQuery
(resultSet
->
{
}
)
;
//Executes once for the first row if found
dataTable.
selectAll
(
).
fetchRow
(resultSet
->
{
}
)
;
//Executes once for each row found
dataTable.
selectAll
(
).
fetchAllRows
(resultSet
->
{
}
)
;
If you're curious of implementation
Code (Java):
DataSource dataSource
;
//Obtain your dataSource
Database database
=
new SimpleDatabase
(
"databaseName", dataSource
)
;
DataTable dataTable
=
new GenericDataTable
(
"tableName", database
)
;
DatabaseStatement stmt
=
new GenericDatabaseStatement
(database,
"sql"
)
;