Java Jdbc Tutorial with H2

JDBC is built for the purpose of communication with relational databases with the help of Java Program.
The Jdbc Driver is component that comes like mediator that takes to and fro data from java to database and vice versa.
Today We are going to perform save ,delete, display operations on H2 database from java program.
Before we kick start our implementation I wish to introduce you with some core classes we will use throughout the tutorial.
  • Class.forName(driverClass)
  • DriverManager
  • Connection
  • PrepareStament
  • ResultSet
  • Class.forName(driverClass):It is used for loading the database specific drivers. This driver gives the power to communicate with database from java program.
  • DriverManager:This class mainly is loaded when you load driver using Class.forName.DriverManager helps you find and load suitable jdbc driver using classloader.
              Once Drivermanager loaded you can use static method                     DriverManager.getConnection(url,username,password) to get Connection.
              DriverManager attempts to establish the connection with database using the getConnection() once it establishes connection it gives you Connection instance in return.
  • Connection: Connection class is responsible for giving the database related information. It also gives Statement object.
  • PrepareStatement: Used for performing the insert/update/delete operations on the database.

  • Statement: Statement is used mainly for getting list of records from table.

Steps For Implementation
  • Step1:Get H2 database jar from H2 Database Jar.
  • Step2:File->Create Project->Java->give some name to project.
  • Place this H2 jar in classpath.Right click project ->Conigure build path->Add external jar->select jar->click Ok
  • Step3:Create in src folder of your project.
  • Step4:Add the below code in your file.
See complete example.

How to solve Access denied for user in mysql.

If you re working with mysql you need to grant previledges to you user.

april is database name,test is username at localhost with password identified by  test.

Congrats ! you just learned database connectivity with jdbc java api. If you have come this far it means you have enjoyed reading this article. Please comment your feedback.
Please support me by sharing this article with your friends and colleagues. Still have some doubts please comment I will surely solve your doubts.

Leave a Reply

Your email address will not be published. Required fields are marked *