Introduction to JDBC

If you have developed business applications with Java, you have most likely used JDBC directly or indirectly(through an object-relational mapping framework) to connect and use relational databases. A relational database is a system for storing information in a tabular form; that is, in tables. There are many vendors offering free and commercial relational database management systems (RDBMS). Two of the most popular open source RDBMS are PostgreSQL and MySQL, while Oracle Database and Microsoft SQL Server are well-known options among the commercial ones. These systems understand the Structured Query Language (SQL), a declarative language used to perform tasks such as adding or deleting rows in a table.

When using a declarative language, you specify what you want the program to do. In contrast, when using an imperative language, such as the Java programming language, you specify how to do it.

Before we get started with the actual code, try compiling and running the example application located in the Data-centric-Applications-with-Vaadin-8/chapter-05 Maven module. Follow these steps:

  1. If you haven't done so, import the Data-centric-Applications-with-Vaadin-8 Maven project into your IDE.

 

  1. Create a running configuration for the packt.vaadin.datacentric.chapter05.jdbc.H2Server class and run it. This is your database server. It runs in a separate process in your computer. Alternatively, you can run the H2 server with Maven from the chapter-05 directory: mvn test exec:java -Dexec.mainClass="packt.vaadin.datacentric.chapter05.jdbc.H2Server".
  2. Create a running configuration for the packt.vaadin.datacentric.chapter05.jdbc.DatabaseInitialization class and run it. You should see an initialization succeeded message in the log. This initialization creates a new table (messages) in the database, and adds some demo rows to it. Alternatively, you can run the initialization application with Maven: mvn exec:java -Dexec.mainClass="packt.vaadin.datacentric.chapter05.jdbc.DatabaseInitialization".
  3. Create a running configuration for the Jetty Maven Plugin in the chapter-05 module.
  4. Point your browser to http://localhost:8080. You should see some demo data rendered by a Vaadin application.
  5. The H2 server we started in step 2 starts also a web application you can use to run SQL queries. Let's try it! Point your browser to http://localhost:8082 and connect using the following configuration:
  1. Insert a new row into the messages table by executing the following SQL statement: INSERT INTO messages VALUES('Welcome to JDBC!').
  2. Point your browser to (or reload) the Vaadin application. You should see the new message listed there:

If you want to, you can stop the Vaadin application and the H2 server, and run them again. You should see all the same data as before, including the newly inserted row. Just keep in mind that you need to run the H2 server first!

If you are curious, the location of the actual H2 database file is <home-directory>/h2-databases/demo.mv.db. You can delete this file and run the DatabaseInitialization application again if you want to recreate the initial state of the database.

The Java Database Connectivity (JDBC) API enables your applications to connect to an RDBMS and make SQL calls to it. Other technologies for SQL persistence are usually implemented on top of the JDBC. Understanding the key aspects of JDBC will make your life easier even if you are planning (or if you are already using) other persistence technologies.

Typically, your application should perform five steps in order to use a database with the JDBC API:

  1. Add a JDBC driver for your database.
  2. Establish a connection to the database.
  3. Create a statement to execute an SQL query.
  4. Get and process the result set.
  5. Close the connection.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset