Jood is object oriented sql-database library written in Java.
implementation 'com.nikialeksey:jood:x.y.z'
<dependency>
<groupId>com.nikialeksey</groupId>
<artifactId>jood</artifactId>
<version>x.y.z</version>
</dependency>
Where x.y.z
actual version from
Connection connection = DriverManager.getConnection(...);
Db db = new JdDb(() -> connection);
For example, connect to sqlite
in-memory:
Connection connection = DriverManager.getConnection(
"jdbc:sqlite::memory:"
);
Db db = new JdDb(() -> connection);
DataSource ds = ...;
Db db = new JdDb(ds);
For example, connect to h2
in-memory through the c3p0
pooled data source:
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setJdbcUrl("jdbc:h2:mem:db_name");
Db db = new JdDb(ds);
Db db = ...;
db.write(new JdSql("CREATE TABLE a (n INTEGER NOT NULL)"));
db.write(new JdSql("INSERT INTO a(n) VALUES(5)"));
try (
QueryResult qr = db.read(new JdSql("SELECT * FROM a"))
) {
ResultSet rs = qr.rs();
while (rs.next()) {
String n = rs.getString("n");
}
}
db.write(
new JdSql(
"INSERT INTO a(n) VALUES(?)",
new IntArg(5)
)
);
Db db = ...;
db.run(() -> { // all changes inside will be commited after successfull execution
db.run(() -> { // transactions could be inner
for (int i = 0; i < 1000; i++) {
db.write(new JdSql("INSERT INTO a(n) VALUES(1)"));
}
});
for (int i = 0; i < 3000; i++) {
db.write(new JdSql("INSERT INTO a(n) VALUES(2)"));
}
});
Db origin = ...;
Db db = new MigrationsDb(
origin,
new JdMigrations(
new Migration() {
@Override
public int number() {
return 0; // migration index
}
@Override
public void execute(final Db db) throws JbException {
db.write(
new JdSql(
"CREATE TABLE user (name TEXT NOT NULL)"
)
);
}
},
new Migration() {
@Override
public int number() {
return 1; // migration index
}
@Override
public void execute(final Db db) throws JbException {
db.write(
new JdSql(
"ALTER TABLE user " +
"ADD lastname TEXT NOT NULL DEFAULT ''"
)
);
}
}
),
2 // db version number
)