This project is a basic Sqlite library in Java that uses JDBC.
The Sqlite wrapper is the class that executes all of the queries in the database. Without instatiating this class, you will not be able to use this library to communicate to the database.
public class UsingSqliteWrapper {
private SqliteWrapper wrapper;
public UsingSqliteWrapper() {
wrapper = new SqliteWrapper("path/to/sqlite/database.db");
}
}Once you have the SqliteWrapper instantiated (demonstrated above), you can pass a Query Object to one of the SqliteWrapper methods: insert, select, update, delete.
Inserting data is simple, just use the InsertQuery object to create a query and pass that object to the SqliteWrapper.insert() method.
public class UsingSqliteWrapper {
...
public void createUser(String name, String email, int age) throws SQLException {
InsertQuery query = new InsertQuery("users");
query.addValue("name", name)
.addValue("email", email)
.addValue("age", String.valueOf(age));
int id = wrapper.insert(query);
System.out.println("The id for user " + name + " is " + id);
}
}To select data from the database, similarly create a SelectQuery object and use it to build a query. Then pass that object to the SqliteWrapper.select() method. The SqliteWrapper.select() method will return a ResultSet.
public class UsingSqliteWrapper {
public void getUsers() throws SQLException {
SelectQuery query = new SelectQuery("users");
query.field("name")
.field("email)
.where("age > 32");
ResultSet results = wrapper.select(query);
while (rs.next()) {
System.out.println(
"Name: " + res.getString(1) + " " +
"Email: " + rs.getEmail(2));
}
}
}To update data that is already in the database, create an UpdateQuery object and use it to build a query. Then pass this object to the SqliteWrapper.update() method.
public class UsingSqliteWrapper {
...
public void updateUserEmail(int id, String newEmail) throws SQLException {
UpdateQuery query = new UpdateQuery("users");
query.set("email", email)
.where(String.format("id = %d", id);
SqliteWrapper.update(query);
}
}To delete data that is already in the database, create a DeleteQuery object and use it to build a query. Then pass this object to the SqliteWrapper.delete() method.
public class UsingSqliteWrapper {
...
public void deleteUser(int id) throws SQLException {
DeleteQuery query = new DeleteQuery("users");
query.where(String.format("id = %d", id);
SqliteWrapper.delete(query);
}
}- JUnit 5 - A popular unit testing framework for the Java programming language.
- Sqlite JDBC Driver - A library for accessing and creating SQLite database files in Java.