Home » JDBC » A Complete Java JDBC Tutorial - Every Java Developer Should Read

A Complete Java JDBC Tutorial - Every Java Developer Should Read

JDBC: Java Database connectivity

The Java database connectivity will consist of the following topics:

1.1) Introduction to Advance Java
1.2) JDBC Introduction
1.3) Need for JDBC in Java
1.4) JDBC Architecture
1.5) JDBC API
1.6) What are Drivers
1.7) JDBC: Types of Drivers
1.8) Working with TYPE-1 Driver
1.9) Working with TYPE-2 Driver
1.10) Working with TYPE-3 Driver
1.11) Working with TYPE-4 Driver
1.12) Working with TYPE-5 Driver
1.13) 5 Steps to connect to the Database
1.14) INSERT operation in JDBC
1.15) UPDATE operation in JDBC
1.16) DELETE operation in JDBC
1.17) SELECT operation in JDBC

1.1) Introduction to Advance Java:

After covering the important topics in core java like collection, data file handling, etc. But by the help of core java, we can develop only stand-alone application.

Stand-alone application/Bachelor Application are those who are running on a single machine/computer. For example, the Calculator program, Hello World Application.

The Stand-alone application is divided into 2 parts

Java standalone application typesGUI Base Application: with GUI screen we are able to run this application like: Calculator, video game, paint. These are also called Desktop application.

CUI Based Application: Character User Interface application. Those application that are executing by the help of command Prompt that is called CUI Based application.

If we want to develop any web application there is minimal use of core java but to develop web application, we have to use Advance java in that case.

Web Application are those application which can provide service over web like Gmail, Facebook, yahoo, google javatutorials.com. To develop the certain web application, we have to learn the 3 topics in deep those are: JDBC, Servlet and JSP. By use of these 3 technologies we can easily develop a web application.

  • JSP is meant for presentation logics to display the content to the end-users
  • Servlet is meant for processing logics
  • JDBC now to communicate our website with the database of our website so JDBC is used

These 3 technologies are used to develop a complete website on the basic level. To improve your website or to make your website light-weighted we can study further for hibernate framework, spring frame work etc.

According to Sun Microsystem Java is based on total 3 editions

  • Java Standard Edition (J2SE/JSE)
  • Java Enterprise Edition(J2EE/JEE)
  • Java Micro Edition(J2ME/JME)

Under J2SE whole core Java and JDBC is covered on the other hand J2SE covers Servlets and JSP content. Under J2ME it covers micro application like mobile application, robot application etc.

Now coming to the topic called JDBC. The version we are using in JDBC is 4.2 version.

Now basically JDBC is used to communicate the java to a database. Understanding this scenario suppose there are two different regions with different languages where both the parties have to trade with each other but there is a language barrier between them, so in that case, the first thing, that they will require is the means of communication. If these both parties communicated well so the trading can be done easily and give and take system can be handled easily. For this communication, the parties need a translator for changing the system.

The work of the translator is to change the first party language to other parties so that they can understand that easily and vise-versa for the other party.

This is the overall concept of java database connectivity. Where one party is Java and other party is database. The translator act as drivers which can convert the java language to database specific calls and vise-versa.

Once the translator is ready there should be connectivity of both parties to translator so there should be some connectivity between both the parties. This is called Connections.

This is how the JDBC overall work. Further we will discuss in details about the Drivers, Connectives, ResultSet, Database and Java code.

1.2) JDBC Introduction:

In the last topic we discussed is the overall story of JDBC but now we study the technical terminologies for the JDBC, so these are as follows:

  1. Driver: This act as translator. This convert the java calls to database calls and database calls to java specific calls
  2. Connection: this is something like road which used as network between java application to database

3. Statement object: This is something like vehicle which is used to send the SQL query to database and to bring result from database to a Java application

4. ResultSet: it is used to hold the result of SQL query. Java application get the result of database from this ResultSet.

This is the four important term for working with JDBC. Now there are various standard step to go through JDBC coding.

These steps to develop JDBC application:

  1. Load and Register Driver
  2. Establish the connection between java application and database
  3. Creation of statement object
  4. Send and execute SQL query
  5. Process Result from  ResultSet
  6. Close the connection.

Now here is just simple program to show how this work through each and every thing will be explained in details afterward in this topic. Not to worry if anything is not explained.

These are the steps that used to connect the database with java. Now with the help of a program:

Java JDBC program

This is the Program where all the steps are described well all the steps will be explained in details.

Not to worry for that. But we have to connect to the database and make the connectivity by adding the external jar in the program.

All the steps will be explained in details and well explained.

Java JDBC program outputAs the above program was to add the new row in the database as shown in the program.

INSERT INTO `newdatabase`.`student`(`rollnumber`,`name`,`marks`)VALUES(106,'alka',15)

This is the script used in the program to add the row in the database.

Insert in Oracle Table using JDBC

This is the output for the above program which we get on console of the eclipse.

In this way the overall JDBC works in the programming. Further we will cover each and every line code and will be explained in the detailed way.

1.3) Need for JDBC in JAVA:

As we all came to know till now that from java application if we want to communicate with database we use JDBC.

JDBC is the part of java standard edition J2SE it is the group of guideline defined by java vendors or we can say JDBC is made by java vendors. But implementation is done by database vendors and this impletion is done by s/w called drivers.

Basically we need JDBC concept because:

Java JDBC Concept

  • It is used to communicate with database from java application
  • Connect your website with database so that anyone can register in your website or log in so JDBC concept is used for doing that
  • Two-way process to connect that two different languages.
  • Used in large application to store data dynamically
  • It is the standard API because for any kind of database code need not be changed for example same application can communicate with MySQL, Myphpadmin, Oracle database mongo dB etc.
  • JDBC is the database independent technology
  • Most of the database driver is developed in java only and now if driver is developed in Java and as the java is platform independent language so hence driver for JDBC is also platform independent.
  • CRUD operations are performed in the JDBC concept. But as CRUD operations are easily developed even difficult operation can also be done easily in JDBC.
  • A lot of vendors provide support for the JDBC technology if anyone wants to check the list of vendors so please click this link

https://www.oracle.com/technetwork/java/index-136695.html

IBM's Database 2 (DB2)
IBM's Informix Software Inc.
IDS Software
i-net software
Intersystem Corporation
iWay Software
JNetDirect

There are a lot of companies that provide JDBC support.

1.4) JDBC Architecture:

Let us suppose there is one Java Application and assume that we have one database called Oracle Database. Now we have to convert the calls from Java to database and vice versa so we required the Driver software. This conversation is done by Driver software of Oracle.

Assume the same scenario we have same java application but now we have the MySQL database and for connectivity we have MySQL Driver. Similarly, some other database and its driver.

If multiple driver is present so to manage all this Driver software one thing is required called Driver Manager. Driver manager is very important component in JDBC architecture.

Now Driver manager is present in java.sql package, so who provide driver manager in this concept.

JDBC API consist of the Driver manager. It is the class who is responsible to manage all the diver and it provide connection to the database. Java application only talk with driver manager.

Java application driver manager

Overall points for JDBC Architecture:

  • JDBC API provides Driver Manager to Java Application.
  • Java application can communicate with any database with the help of Driver manager and database specific driver software.

 Driver Manager

  • It is the key component in JDBC Architecture
  • It is the class present in Java SQL package.
  • It is responsible to manage all the database driver.
  • It is responsible to register and unregister Database drivers it is done by the help of DriverManager.registerDriver(driver) and DriverManager.unregisterDriver(driver)
  • It is responsible to establish connection to the database with the help of driver software Connection connection = DriverManager.getConnection(JDBC_url, user, password);

Database Driver

  • It is very important component of JDBC Architecture
  • Without help of Database Driver we are unable to connect to the database
  • It acts as the bridge between Java application and database and database specific calls into java calls and vice versa.

1.5) JDBC API:

JDBC API defines the several classes and interfaces which can be used for database communication. There are basically 2 kind of people who uses this one is Programmer to develop the JDBC program and the Driver software vendors uses this JDBC API to develop driver software.

JDBC API contains two packages:

java.sql package and javax.sql package

java.sql: It contains the basic classes and interface which can be used for database communication. Like Statement Result set, connection driver manager.

Javax.sql: it stands for Java extension this is the advanced concept for the JDBC. It contains more advance classes and interface. For example, Connection pooling, row Set, etc. there are several sub packages in it like javax.sql. rowset, javax.sql. rowset. Serial, javax.sql.rowset.spi

The Java Database Connectivity (JDBC) API provides universal data access from the Java programming language. Using the JDBC API, you can access virtually any data source, from relational databases to spreadsheets and flat files. JDBC technology also provides a common base on which tools and alternate interfaces can be built.

Driver Software: it is the group of classes. The classes which implement various interface of JDBC API.

Here in JDBC API some of the things should be kept in mind:

  • Programmers are not responsible to provide implementation for JDBC API interface
  • Most of the times database vendors is responsible to provide implementing as the part of Driver Software
  • Every Driver Software is a collection of classes implementing various interfaces of JDBC API, which can be used to communicate with a particular database.
  • For Example: Driver Software for Oracle means Collection of implementation classes of JDBC API, which can be used to communicate with Oracle Database.

JDBC Driver Software

1.6) What are DRIVERS:

In driver software there are multiple classes but among all there is special class called driver class. There is one type of driver called TYPE I driver this is provided by SUN MICROSYSTEM.

It is the part of JDK now in this there should be the driver class its name is sun.jdbc.odbc.JdbcOdbc.Driver this is the example of Driver class. We will discuss all these things in details this is just the basic idea in the driver class.

JDBC Driver functionalityIf java application wants to communicate with Database Diver software is required. This driver software is the group of classes. It is identified by the special class called the driver class.

Every Driver Software is identified with the special class which is called the driver class and this is the implementation of java SQL Driver.

Example: Thin Driver provided by the Oracle Corporation and corresponding driver class name is Oracle.jdbc.OracleDriver.

1.7) JDBC: Types of Drivers

As discussed above is what is drivers? Which is used to help java to contact with the database. There are thousands of Driver available.

But based on the Architecture and functionality the drivers are divided into four different type:

1)      TYPE- I Driver: JDBC-ODBC Bridge Driver

2)      TYPE-II Driver: Native API/Partly Java Driver

3)      TYPE-III Driver: All Java Net Protocol Driver

4)      TYPE-IV Driver: Pure Java Driver/Thin Driver

Now recently there is one software company named Progress Data Direct, this company people introduce the other driver which is considered to be as TYPE-V Driver. As of now there are only four types of driver.

While communicating with database, we have to concert java calls into database-specific calls and vice-versa for this Driver software is required.

In market many numbers of drivers are available but the main is those four kinds of drivers that are shown above. Now further we will discuss the each and every driver in Details.

1.8) Working with TYPE-I Driver:

This Driver is also known as the JDBC-ODBC Driver or Bridge Driver because it acts as the bridge between the JDBC application and the database.

This Driver was provided by the SUN Microsystem as the part of the JDK. Coming to the internal working of the driver will take the support of ODBC driver to communicate with the database

TYPE I driver is the driver which convert the JDBC calls into ODBC calls and ODBC Driver convert ODBC calls into database-specific calls due to this reason the TYPE-I driver acts as the bridge between the JDBC And ODBC.

TYPE-I driver is the oldest driver in the terms of JDBC its now out of date because the TYPE-I driver support is being finished in the JAVA 1.7 version. In Java 8 there is no support provided for the JDBC TYPE-I Driver or we can say that this driver age is over for Java.

Yet if you want some hands-on the TYPE-I driver we can install the Java 1.7 version and work on the TYPE-I driver.

Before jumping over to the advantages, we have to look at the architecture of the TYPE-I Driver.

Given below is the Basic architecture of the TYPE I Driver:

Architecture of TYPE I Driver

JDBC TYPE-I DriverThe java application consists of the code where the connectivity code is written in the application now In ODBC Driver the conversion work is done.

The TYPE I driver act as the bridge between the JDBC application and the ODBC Driver this is the reason TYPE I is also called bridge Driver. Its overall functionality of this driver is that it converts the JDBC calls into the ODBC calls and those ODBC calls are converted into DATABASE calls and same as vice-versa.

This is the functionality of the TYPE-I Driver.

Advantage of TYPE I Driver:

  • It is very easy to use and maintain
  • We are not required to install separately as it is the part of the JDK
  • TYPE I driver won’t communicate with the database directly Hence it is the database independent driver because of this migrating from one database to another database become easy.

Disadvantages of TYPE I Driver:

  • It is the slowest driver among all the drivers it is also called snail Driver because first it will convert JDBC calls to ODBC calls and ODBC driver converts ODBC calls into database-specific calls
  • The driver internally depends on the ODBC driver, which will work only on windows machine. Hence TYPE-I driver is platform dependent driver
  • No support form JDK 1.8 version onwards.

1.9) Working with TYPE-II Driver:

There is not much difference between TYPE I driver and TYPE II driver. But there is one difference between them it just provides the native libraries.

These native libraries are provided by the vendors. It is database specific libraries.

Here java application communicates with type 2 driver. This type 2 driver converts the JDBC calls into database-specific native libraries calls. These calls can be understood by database.

JDBC -> DB Native Calls -> DATABASE

This is exactly same as TYPE I driver but just native API drives is new arrival here.

What are native libraries?

The libraries that are develop in other language, mean that language that is not developed in the JAVA is called the Native libraries. This is provided by the vendors.

Java Application + TYPE II Driver + native Libraries all this comes under the client machine.

This is quite faster than the TYPE I driver. And also it is platform dependent language. Here we can take separate libraries for different-different Operating system.

Coming over to the Advantage and Disadvantages:

Advantage of TYPE I Driver:

  • When compared with the TYPE-I driver performance it is high because it required only one level connection from JDBC to native libraries.
  • There is no need for arranging ODBC driver.
  • When compared with TYPE I driver, its portability is more the TYPE I because TYPE I is applicable only for windows machine.

Disadvantages of TYPE II Driver:

  • It is database dependent driver because it internally uses database specific native libraries. Hence migrating from one database to another database will become difficult.
  • It is platform dependent driver.
  • We require to install native libraries on the client machine.
  • There is no guarantee that every database vendor will provide these drivers

Architecture of TYPE II Driver

Java JDBC Type 2 driver

1.10) Working with TYPE-III Driver:

It is also called Middleware driver. This Driver never communicate directly with database. There is the use of Middleware server / IDS is used to communicate with the Database. It is database independent and platform independent. Out of all the four driver TYPE III driver is the platform independent driver.

Now if we are working with multiple database it is highly recommended to work with TYPE III database.

Here Java application communicates with the middleware server by the help of TYPE III driver. This middleware server connects with the database by the help of TYPE I, TYPE II or TYPE IV driver.

In TYPE III driver client machine consist of the JDBC and TYPE III server and it also consist of middleware server called IDS server, which further use to connect with the database. It follows the three-tier architecture. For using the TYPE III driver, we need to install the IDS server.

This is most commonly used driver.

There are also several advantages and disadvantages for the TYPE III driver.

Advantage of TYPE III Driver:

  • This driver is server-based, so there is no need for any vendor database library to be present on client machines.
  • This driver is fully written in Java and hence Portable. It is suitable for the web.
  • There are many opportunities to optimize portability, performance, and scalability.
  • The net protocol can be designed to make the client JDBC driver very small and fast to load.
  • The type 3 driver typically provides support for features such as caching (connections, query results, and so on), load balancing, and advanced
    system administration such as logging and auditing.
  • This driver is very flexible allows access to multiple databases using one driver.
  • They are the most effective amongst all driver types

Disadvantage of TYPE III Driver:

  • It requires another server application to install and maintain. Traversing the record set may take longer, since the data comes through the backend server.
  • It is very slow as compared to the TYPE IV driver.
  • All the Driver are 2 tiers while TYPE III is the 3-tier architecture

Architecture of TYPE III Driver

Java JDBC Type 3 Driver1.11) Working with TYPE-IV Driver:

This driver is also called the Thin Driver. This don’t require any component for communication with database. It directly communicates directly with database by using the native protocol provided by the database vendor.

This Driver is also known as pure java driver and also known as native protocol driver. Internally it uses as the native protocol. It is very light weighted driver because it doesn’t require any extra component to communicated with DB.

As it can be seen in the architecture its very simple connectivity. It is platform independent driver.

In this client-side machine consist of only JDBC and TYPE IV driver. It is the fastest driver available. Here it requires only one tier conversation.

ODBC, Native middleware is not even required here to communicate. It is the database dependent driver.

Advantage of TYPE IV Driver:

  • The major benefit of using a type 4 JDBC drivers are that they are completely written in Java to achieve platform independence and eliminate deployment administration issues. It is most suitable for the web.
  • Number of translation layers is very less i.e. type 4 JDBC drivers don't have to translate database requests to ODBC or a native connectivity interface or to pass the request on to another server, performance is typically quite good.
  • You don’t need to install special software on the client or server. Further, these drivers can be downloaded dynamically.

Disadvantage of TYPE III Driver:

  • With type 4 drivers, the user needs a different driver for each database. This means it is

There are very limited disadvantages for TYPE IV driver.

Architecture of TYPE IV Driver

Java JDBC Type 4 Driver1.12) Working with TYPE-V Driver:

This is the driver that is not confirmed by the Sun Microsystem. This TYPE V Driver is made by some other company named Progress DataDirect. This company provide JDBC driver for various Databases.

There are various types of information we need to collect to use it in our code:

Driver class name: com. ddtek.jdbc.oracle.OracleDriver(Auto-loading of Driver class). We need the Jar file to use this but as it is the new company it will provide us trial version for only 30 days, after that we have to purchase it.

Now as we see in the Diagram:

  • The Java application first goes to the TYPE V driver then it will convert the call into database understandable call.
  • After that working on the Machine 2 it returns the call and return it to TYPE V
  • Again, conversion of the Database call to JDBC all is done and in this way the TYPE V driver used to work.

This is almost similar to TYPE IV Driver.

JDBC Type 5 DriverThis was all about the all the types of the driver that we used to learn in the JDBC concept.

After learning all the basic details of the JDBC and types of driver we need to start with the hands-on services provided.

For the hands-on practice you need to download some of the software for that.

HANDS-ON PART

1.13) 5 Steps to connect to the JDBC:

There no exact count the JDBC can be worked in 5 steps, we can break it into several other steps too. So, coming over to the JDBC Step-by-step so I have divided it into the 7 different steps:

1)      Import the Package

2)      Load and register the Driver

3)      Establish the connection

4)      Create the statement

5)      Execute the Query

6)      Process Result

7)      Close the connection

To remember this step there Is the short story:

Suppose in college you are calling a friend. So, you need to call so you should have phone or mobile (1. import the Package) now we need network in the phone or sim-card (2. Load and register the Driver). Now As we have phone and sim card so now, we have to establish the connection means we have to dial the number (3. Establish the connection). After that we will talk to that friend so we have to (4. Create the statement). Next, we have to speak to him so it is (5. Execute the Query). After that we will get the response from him so that is (6. Process Result) and after that we will cut the call or drop the call so (7. Close the connection).

In this way we can remember all the 7 steps of JDBC

Package we have to import is: java.sql. *;

Load the Driver: Every database have different driver and register for MySQL which is used in the below code is com.mysql.jdbc.Driver.

After the result get processed by the help of the Process Result, we have to close our connection by the help of Connection. Close(); function. In this way the overall working of the JDBC works.

The list of software that you require to work on the Hands-on are as follows:

  • Eclipse IDE
  • MySQL Workbench
  • Jar connector
  • Overall java set-up.

Below is the Screen shot for the code to insert the code.

JDBC Program screen-shot

This is how the JDBC 7 step process works.

1.14)  INSERT Operation in JDBC:

This the Operation in which we can insert the data in the Database by the help of Java code. Basically we use PreparedStatment.

PreparedStatement object for sending SQL statements to the database.  the PreparedStatement object contains not just a SQL statement, but a SQL statement that has been precompiled.  This means that when the PreparedStatement is executed, the DBMS can just run the PreparedStatement SQL statement without having to compile it first.

For example, there is the database present in the MySQL set-up named as Student. Where there are 3 column present named as roll number which is the PRIMARY KEY. I hope everyone is aware about the DBMS concepts, other is name and the last one is marks.

Now here are total 10 entries filled. Suppose if we want to add other one, we use INSERT SQL Script.

Here is the database picture for the database before running the program.

The main SQL script that we use is the INSERT:

INSERT INTO `newdatabase`.`student`,`rollnumber`,`name`,`marks`) VALUES(112,'tomamoto',15);

This is used to add the data into the database.

Inserted data screen-shot

Here is the program where we made the code in JDBC and added the entry into the database.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class ConnectDB {
static Connection connection = null;
static String databaseName ="";
static String url ="jdbc:mysql://localhost:3307" + databaseName;
static String username = "root";
static String password ="root";

public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection =DriverManager.getConnection(url,username,password);
PreparedStatement ps = connection.prepareStatement("INSERT INTO `newdatabase`.`student`"      + "(`rollnumber`,`name`,`marks`)VALUES(111,'tomamoto',15);");

int status = ps.executeUpdate();

if(status!= 0)
{
System.out.println("Database was Connection");

System.out.println("Record was inserted");

}
}
}

In this program we have used the PreparedStatement to insert into the database. Where it is creating the statement and after that the ExecuteUpdate is just executing the statement.

After running the program, we get the updated database. As shown here.

Databse Table screenshot

This is how we get the updated database in the MySQL workbench.

1.15)  UPDATE Operation in JDBC:

This the Operation in which we can Update the Database by the help of JDBC code. Any entity can be updated in the database by the help JDBC update. The Statement keyword is used for having Update command in the database.

It always Returns a Boolean value of true if a ResultSet object can be retrieved; otherwise, it returns false. It also Returns the number of rows affected by the execution of the SQL statement.

It can be used for general-purpose access to the database. It is useful when you are using static SQL statements at runtime.

Here is the database picture for the database before running the program.

The main SQL script that we use is the UPDATE:

UPDATE newdatabase.student set name = 'Mark1' where rollnumber= 107

Here is the program for the JDBC to Update the database in MySQL

package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class ConnectDB {
static Connection connection = null;
static String databaseName ="";
static String url ="jdbc:mysql://localhost:3307" + databaseName;
static String username = "root";
static String password ="root";

public static void main(String[] args) throws Exception {

Class.forName("com.mysql.jdbc.Driver").newInstance();

connection =DriverManager.getConnection(url,username,password);

Statement stmt = connection.createStatement();

System.out.println("Connected");

String sql = "UPDATE newdatabase.student set name = 'Mark1' where rollnumber= 107";

System.out.println("database updated");

stmt.executeQuery(sql);

}
}

In this program we have used the Statement to Update into the database. Where it is creating the statement and after that the ExecuteUpdate is just executing the statement.

1.16)  DELETE Operation in JDBC:

Now we will know how to delete the data from database by the help of Java code and which statement we have to write to remove the data from the database.

This is very similar to the UPDATE operation as shown above because the Statement interface is used in this case too. Which is explained earlier.

Delete from newdatabase.student where name = 'Mark1';

Here is the program for the JDBC to Delete the database in MySQL

package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;

public class ConnectDB {

static Connection connection = null;
static String databaseName ="";

static String url ="jdbc:mysql://localhost:3307" + databaseName;

static String username = "root";

static String password ="root";

public static void main(String[] args) throws Exception {

Class.forName("com.mysql.jdbc.Driver").newInstance();

connection =DriverManager.getConnection(url,username,password);

Statement stmt = connection.createStatement();

System.out.println("Connected");

String sql = "Delete from newdatabase.student where name = 'Mark1';";

System.out.println("database Deleted");

stmt.executeQuery(sql);

}
}

In this program we have used the Statement to Delete into the database. Where it is creating the statement and after that the ExecuteUpdate is just executing the statement.

1.17)  SELECT Operation in JDBC:

A ResultSet object is a table of data representing a database result set, which is usually generated by executing a statement that queries the database

You access the data in a ResultSet through a cursor. There are many types of Result set like based on Operation sell out, based on Hold ability etc.

TYPE_FORWARD_ONLY: The result set cannot be scrolled; its cursor moves forward only, from before the first row to after the last row. The rows contained in the result set depend on how the underlying database generates the results. That is, it contains the rows that satisfy the query at either the time the query is executed or as the rows are retrieved.

TYPE_SCROLL_INSENSITIVE: The result can be scrolled; its cursor can move both forward and backward relative to the current position, and it can move to an absolute position. The result set is insensitive to changes made to the underlying data source while it is open. It contains the rows that satisfy the query at either the time the query is executed or as the rows are retrieved.

TYPE_SCROLL_SENSITIVE: The result can be scrolled; its cursor can move both forward and backward relative to the current position, and it can move to an absolute position. The result set reflects changes made to the underlying data source while the result set remains open.

Here is the program which is the hand-on for the select Script of MySQL.

The main SQL script that we use is the SELECT:

SELECT * FROM newdatabase.student;

By the help of this we can get the whole data present in the Database because here * represents ALL. Here the program is for selecting only the rollnumber of the Students. Though in the program we are selecting everything but output is for only Rollnumber because we didn’t write the code to represent everything in the database.

Here is the database view of MySQL workbench which is showing the all the data present in the database.

Here is the program for the JDBC to Select the database in MySQL

package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class ConnectDB {
static Connection connection = null;

static String databaseName ="";

static String url ="jdbc:mysql://localhost:3307" + databaseName;

static String username = "root";

static String password ="root";

public static void main(String[] args) throws Exception {

Class.forName("com.mysql.jdbc.Driver").newInstance();

connection =DriverManager.getConnection(url,username,password);

Statement stmt = connection.createStatement();

System.out.println("Connected");

ResultSet rs = stmt.executeQuery("SELECT * FROM newdatabase.student;");

while(rs.next())

{

System.out.println(rs.getInt("rollnumber"));

}

System.out.println("database Selected");
}
}

This is the overall Output we are getting in the Consol.