JDBC Introduction - Notes Lyst9545
JDBC Introduction - Notes Lyst9545
We have seen that, whenever we had to save output of a Java program in a file, we were using File
Handling. But even though file handling was a good approach, it had its own disadvantages when it
comes to storing data or storing databases since we cannot store databases inside a file and hence it had
the following disadvantages-
So, if we want to become a successful java developer, we should know how to establish a relation
between the java program and the RBDMS. To establish this relation/data flow, we should have a
connectivity called as JDBC.
Whenever we want to store or retrieve data, RDBMS plays a very important role. So, both standalone
applications and web applications require connectivity to RDBMS and the concept of JDBC plays a very
important role.
Program 1:
● RAM
● Hard disk
The database will be stored on the hard disk and this database will be managed by a software known as
Relational database Management System (RDBMS).
There is a variety of RDBMS in the market but we will be using MySQL database in this course.
Every process which will be executing on the RAM will have a unique identification number known as
Process ID and the process ID of MySQL is 3306.
1. Create a schema (database), by clicking on “Create schema” and set the name of the database as
“employee” and then click on “Apply”
2. Now a new schema(database) will be created.
3. Right-click on the schema and click on “Create Table”
4. Set the table name as “emp”
5. Add columns to emp table as shown below
6. After adding all the columns and constraints to it, click on “Apply”.
Inside the RAM, a java program is executing and it wants to access the database, but the java
program cannot directly access the data present inside the database as the data is in control of
MySQL. The only way the java program can access the data is through MySQL. But MySQL can’t
understand java and the java program can’t understand queries.
So, both of them can never directly interact with each other. To make the interaction possible we
require a translator and the software responsible for this interaction is known as “Driver”.
Since we are using MySQL, there is a MySQL Driver software available on the internet, which is
what we have downloaded and stored in the hard disk.
And if we want anything to be executed, it should be present on the RAM. In our case, it should
be present inside our Java program
To achieve this we have to make use of a method Class.forName( ) in our java program.
forName( ) is a static method of a class called “Class”. forName( ) accepts the name of the driver
as its parameter.
Now let’s see how to achieve this through our java program.
Step 1: Create a new java project and name the project as “jdbc”, and click on finish.
Step 2: Follow the steps provided in the PDF to download the jar file
Step 3: Right-click on the project “jdbc”, click on properties
Step 4: Click on ‘Java Build Path’, now click on the ‘Libraries’ tab
Step 5: Click on ‘Classpath’, (now all the buttons will be enabled).
Step 6: Click on the ‘Add External JARs’ button.
Step 7: Now select the ‘mysql connector’ file which you had previously downloaded and click on
‘open’.
Step 8: Click on ‘Apply’. And as soon as we click on ‘Apply’, a new folder “Referenced Libraries”
will be created.
Step 9: Click on ‘Apply and Close’.
To verify whether the driver class has been successfully loaded, we will add a print statement as shown
below-
Let’s execute the program now,
But unfortunately, we got an exception as the path which we had mentioned in forName( ) was wrong.
So we have to be careful whenever we add a path to avoid such problems.
Let us now give the correct path(“com.mysql.cj.jdbc.Driver”) and try executing the program again.
And as we can see, the driver was successfully loaded into the RAM.
Now that the driver is successfully loaded into the Java program, the Java program and MySQL can now
interact with each other. But to achieve this, we have to establish a connection between our java
program and MySQL.
We will call this connection ‘con’. Imagine this connection as a road through with information that can
be sent from the java program to MySQL and vice versa.
But even though the code and database are present in the same computer, we still need the URL to
establish the connection.
Let us provide the correct username and password in the code as shown below-
To establish the connection, we will give reference to the connection as ‘con’. Con is of type Connection.
Now that we have successfully established a connection, in the next class we try to understand how to
write queries to fetch the data from the java program.