How to start fast working with JDBC?
Many people, at the beginning of their adventure with software development, are scared how many things and tools they have to learn. This is example how to start fast working with JDBC (for more information please go here). There is no need to install any database engine - we will use simple H2 database.
First step - download H2 database driver from here.
Second step - add this jar file to Your project.
Let’s write some code!
Create class JdbcExample.
Then define database configuration:
Create our main method to fire example after implementation
As You can see first step is to delete old database file - located in user home directory, named test.mkv. In next line, we initialize database connection. Next method is responsible for creating table - initializing schema. Then we can insert values into our database. In next step we would like to read/select inserted data. After all we need to close database connection.
Let’s see implementation of the first method:
First of all we need register JDBC Driver class name. After this done, we can try to get connection for our databse. We need to handle 2 exceptions and terminate our program when one of them occurse.
Second method is responsible for creating database schema. When we already have connection to database, we can execute query to create example Car table. After executing query we should close used statement. Handled SQLException is print to default system error output.
Third method insert data to our Car table. We create prepared statement based on SQL stored in insertValuesCommand. Question marks present query arguments. As we can see we set arguments in our statement - first argument of setInt or setString method is index of argument in our query - starting from 1.
When we set arguments we can execute query. Prepared statements can be reused- as in example - to execute multiple queries.
Now we can fetch inserted values. We create select query - remeber - using ‘*’ is bad practise, You should know which data You want to select/read (You can read more about this here). When we execute query prepared statement will return result set, which contains fetched data - it gives ous possibility to iterate over these values.
With “next” method we check, if there is some not read yet record. We can get specified data by column name (id, make) or column index in result set (model). We print fetched data to console.
At the end of our example we close connection to our database. Database engine has limited count of connection, so You should remember to close it, when You finish your work.
You can find source code in my Github repository how-to under jdbc directory.
Hope you enjoy this post about starting working with JDBC. If You have any questions or problems leave comment or mail to me.
See You soon!