-
Notifications
You must be signed in to change notification settings - Fork 2
How to use
Begin by creating a MySQL object and connecting to a database. You will need this object for every query.
MySQL mysql = new MySQL();
mysql.connect(String host, String port, String username, String password, String database);For every query you can use the Query class. The Query class will handle opening and closing the connection. Simply create a Query object like this:
Query query = new Query(MySQL mysql, String sql);It will automatically create a PreparedStatement. You can set parameters using the setParameter method:
query.setParameter(int index, Object value);Executing the query is simple, just call executeUpdate() or executeQuery() depending on the SQL. You can also use the asynchronous methods and (optionally) provide a callback:
query.executeUpdateAsync(Callback<Integer, SQLException> callback);To use a callback, make a class that implements the Callback interface. Making a callback for executeUpdateAsync() is optional, but is required for executeQueryAsync() (because it would be pointless to not have a callback). If you want to make a callback for executeUpdateAsync(), make it like the following:
private class MyCallback implements Callback<Integer, SQLException> {
public void call(Integer rowsChanged, SQLException e) {
}
}If an exception was thrown, rowsChanged will be zero.
To make a callback for executeQueryAsync(), make it like the following:
private class MyCallback implements Callback<ResultSet, SQLException> {
public void call(ResultSet resultSet, SQLException e) {
}
}If an exception was thrown resultSet will be null.
If you prefer to do so, you can also use anonymous classes:
executeQueryAsync(new Callback<ResultSet, SQLException>() {
public void call(ResultSet resultSet, SQLException e){
}
});