Yank Example Code

Wait, there's even more!

In addition to the examples found here, there is a lot of great information about using Yank in the project’s README file. Additionally, there is working example and test code available at Github

Theory

Each table in your database has a corresponding Bean (a.k.a. Pojo or Plain Ol’ Java Object). The Bean has private fields with names and data types that match the column names of your table. In other words, the Bean is a one-to-one mapping of a single row in your table.

The next thing you need is a Data Access Object, or DAO. In this Class, you create methods for interacting with the table. In each method, with only a few lines of code, you create the specific SQL query you want and use org.knowm.yank.Yank to handle the query for you. You actually don’t need to have a DAO Class, but it helps to organize your project. You could always just call the Yank methods directly and forego a DAO Class.

The advantage of this is that your code for interacting with the database is all wrapped up in just two very simple classes for each table in your database – a Bean and a DAO. And because the actual nitty-gritty details of handling database connections and results sets in handled for you in org.knowm.yank.Yank, you don’t have to worry about coding that yourself. It saves time and prevents errors. In the following demo code you will see absolutely no low-level ResultSet, Connection, or exception handling code – only clean and organized business-domain code.

One advanced feature is also demonstrated here: storing database connection properties and SQL statements in Properties files. You do not have to do it this way; you could hardcode everything if you wanted to. There are several advantage however. First, if you want to switch database technologies, like from Oracle to MySQL, all you’d have to do is load a different Properties file with the new DB connection information. Same goes for the SQL Properties file. Since different database technologies use different SQL syntax, you could have two files – one for DB_X and one for DB_Y, and switching between the two is just a matter of loading a different Properties file. Some people also like to have all their SQL statements in one place and not scattered throughout several classes.

Note that in these examples that Yank is always setup at the beginning and then released at the end. In a real application you would not want to continuously setup and release before and after each database action, but instead setup and application startup and release at application shutdown, leaving Yank “open” for the lifetime of the application.

Setting up a Database

This demo uses MySQL but any JDBC-Compliant database will work. You just need to put the appropriate JDBC Connector jar on the classpath and tweak the SQL statements to match the specific SQL syntax for your database of choice!

Shell

1

2

3

4

5

6

7

8

//However you endup creatingadatabase,it must have the name"Yank"forthe rest of thisexample code towork.

//Ingeneral the database name must match the provided database name inthe jdbc url you provide Yank.

MYSQL_DB.properties

Shell

1

2

3

4

jdbcUrl=jdbc:mysql://localhost:3306/Yank

username=root

password=

maximumPoolSize=10

Yank comes bundled with the Hikari Connection Pool. When you setup Yank using the Yank.setupDataSource() method and pass it a Properties object, that object must at the very least contain jdbcUrl, username and password properties. Another common property is maximumPoolSize.To see a full list of available configuration properties along with their defaults, see Hikari’s main README.