Java JDBC Tutorials

  • 16 August 2019
  • ADM

 

 

Java JDBC Tutorials - images/logos/jdbc.jpg

 

Java Database Connectivity (JDBC) is an application programming interface (API) for the programming language Java, which defines how a client may access a database. It is part of the Java Standard Edition platform, from Oracle Corporation. JDBC standardises how to connect to a database, how to execute queries, how to navigate the result on a query, and how to execute updates in the database.

Because the JDBC only defines the standard how to operate a database from Java, each database engine will provide a connector that implements that standard.

MySQL

Before starting with MySQL you need to download MySQL Connector from the download page and attach to your project or if you are using Maven structure you need to add it as a dependency.

<dependency>
     <groupId>mysql</groupId>
     <artifactId>mysql-connector-java</artifactId>
     <version>5.1.38</version>
 </dependency>

Oracle

For Oracle, you need to downloadits own driver and attach it to your project. For Maven users, add this dependency.

<dependency>
     <groupId>com.oracle.jdbc</groupId>
     <artifactId>ojdbc7</artifactId>
     <version>12.1.0.2</version>
 </dependency>

MSSQL

For Microsoft SQL Server you need this driver. For Maven users, you need to add it manually to the maven local repository and then to add it as a dependency.

To do this follow these steps:

  1. Download the driver.
  2. unzip all files to alocal directory.
  3. Go to command prompt navigate to folder where sqljdbc4.jar is located and execute thefollowing command:
    mvn install:install-file -Dfile=sqljdbc4.jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=6.0.7130.100 -Dpackaging=jar

    If you need more info on how to add libraries to local maven repository followHow to include library into maven local repositorytutorial.

  4. Now you are able to add it as dependency on your pom.xml file:
    <dependency>
         <groupId>com.microsoft.sqlserver</groupId>
         <artifactId>sqljdbc4</artifactId>
         <version>6.0.7130.100</version>
     </dependency>

PostgreSQL

Download the driver and attach it to the project, or for Maven users add the dependency.

<dependency>
     <groupId>postgresql</groupId>
     <artifactId>postgresql</artifactId>
     <version>9.1-901-1.jdbc4</version>
 </dependency>

 

MySQL JDBC Statement and PreparedStatement

MySQL Stored Procedure

MSSQL JDBC Statement and PreparedStatement

MSSQL Stored Procedure

Oracle JDBC Statement and PreparedStatement

Oracle Stored Procedure

PostgreSQL JDBC Statement and PreparedStatement

PostgreSQL Stored Procedure

 

References