Struts 2 database integration Introduction

Database Acess

In this section of struts we will discuss about access a database using Struts 2 in simple and easy to do steps. Struts is a MVC framework and not a database framework but it provides excellent support for Hibernate integration. We can also do the database access by using jdbc and POJO. We shall use plain old JDBC to access the database.

The first step in this chapter is to setup and prime our database. I am using MySQL as my database for this example. I have MySQL installed on my machine and I have created a new database called "struts_tutorial". I have created a table called login and populated it with some values. Below is the script I used to create and populate the table.

Introduction to JDBC

JDBC stands for Java Database Connectivity, it is the database API written in java which is a standard Java API for database-independent connectivity between the Java programming language and a wide range of databases needs to be access by the java APIs. It plays the important role of the communicator and transactions manager the java

The JDBC library includes APIs for each of the tasks commonly associated with database usage:

  • Making a connection to a database.
  • Creating SQL or MySQL statements.
  • Executing that SQL or MySQL queries in the database.
  • Viewing & Modifying the resulting records.
  • Managing and maintaining connection transactions over the databases.

Simply in other words we can say, JDBC is a specification that provides a complete set of interfaces and classes that allows for portable access to an underlying database. Java can be used to write different types of executables, such as:

  • Java Applications with different databases
  • Java Applets API with databases
  • Java Servlets API and multiple databases
  • Java ServerPages (JSPs) and databases
  • Enterprise JavaBeans (EJBs) and databases

All of these java APIs are able to use a JDBC driver to access a database and take advantage of the stored data over the databases.

JDBC provides the samea as ODBC, allowing Java programs to contain database-independent code.

Database Acess

Access a database using Struts 2 in simple and easy to do steps. Struts is a MVC framework and not a database framework but it provides excellent support for Hibernate integration. We can also do the database access by using jdbc and POJO. We shall use plain old JDBC to access the database.

The first step in all this tutorial is to setup and primary requirement to our database.We are going to use MySQL as my database for this example.

I have MySQL installed on my machine and I have created a new database called "struts_tutorial". I have created a table called login and populated it with some values. Below is the script I used to create and populate the table.