Java Database Connectivity
What is JDBC?
JDBC is nothing but Java DataBase Connectivity. As the name suggests, it deals with connecting you code business logic to a database.
Types of Connections
There are basically 4 types of drivers which you can use to connect to the database:
Type 1 : JDBC-ODBC bridge
Type 2 : Partial Java driver
Type 3 : pure Java driver for database middleware
Type 4 : pure Java driver for direct-to-database
The most widely used and supported by different vendors are the type 2 and type 4 drivers. They are easy to use and portable across various databases, and we will discuss the same here.
How to use it
1) Declaring a Connection reference variable which will store the connection details. You do this by
2) Then you search the Driver provided by the database designer/creator, and sometimes by Java, which will be used to connect to the database.Code:Connection variable_name;
The class is loaded by using Class class's forname method.
Code:Class.forname("Path_of_the_class");
3) Once the driver is loaded, it is time to connect to the database. But to connect to it, you need the username and password and the iport number where the database server listens.
This connection is referenced by the reference variable of the Connections class.Code:variable_name=DriverManager.getConnection ("type_of_connectivity:database://ip_address:port_number/db_name", "username", "password");
The various objects that are used for SQL are:
- PreparedStatement: Used when your application is going to reuse a statement multiple times. Once prepared, the you can specify the values at runtime.
- CallableStatement: A CallableStatement is used to call stored procedures that return values. It has methods for retrieving the return values of the stored procedure.
- ResultSet: Contains the results of a query. You can iterate through all the rows obtained by the various methods provided by the ResultSet class.
The Code
Note :: We are connecting to MySQL database. The database name is test and username and password is root.
OutputCode:package com.mycomputer.connectDB; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBConnection { Connection conn; public DBConnection() { try { Class.forName ("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/test", "root", "root"); System.out.println("\nConnected to the database"); } catch(ClassNotFoundException e) { System.out.println("\nClass not found. Check Path"); e.printStackTrace(); } catch (SQLException e) { System.out.println("Not connected"); e.printStackTrace(); } } public static void main(String[] args) { new DBConnection(); } }
Code:Connected to the database
![]()
So let's say you want to connect to the MySQL database in another class (one that might represent let's say the GUI of a login window). How would you do it then?
Is it creating a new reference variable of the type Connection and do:
Connection conn = new DBConnection();
And then use that reference variable to call some other methods for let's say querying the database...?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks