Java Database Connectivity with 5 Steps
There are 5 steps to connect any java application with the database using JDBC. These steps are as follows:
|
1) Register the driver class
The forName() method of Class class is used to register the driver class. This method is used to dynamically load the driver class. |
Syntax of forName() method
Example to register the OracleDriver class
Here, Java program is loading oracle driver to esteblish database connection.
2) Create the connection object
The getConnection() method of DriverManager class is used to establish connection with the database. |
Syntax of getConnection() method
Example to establish connection with the Oracle database
3) Create the Statement object
The createStatement() method of Connection interface is used to create statement. The object of statement is responsible to execute queries with the database. |
Syntax of createStatement() method
Example to create the statement object
4) Execute the query
The executeQuery() method of Statement interface is used to execute queries to the database. This method returns the object of ResultSet that can be used to get all the records of a table. |
Syntax of executeQuery() method
Example to execute query
5) Close the connection object
By closing connection object statement and ResultSet will be closed automatically. The close() method of Connection interface is used to close the connection. |
Syntax of close() method
Example to close connection
Java Database Connectivity with Oracle
To connect java application with the oracle database, we need to follow 5 following steps. In this example, we are using Oracle 10g as the database. So we need to know following information for the oracle database:
|
Create a TableBefore establishing connection, let's first create a table in oracle database. Following is the SQL query to create a table. |
Example to Connect Java Application with Oracle database
In this example, we are connecting to an Oracle database and getting data from emp table. Here, system and oracle are the username and password of the Oracle database.
To connect java application with the Oracle database ojdbc14.jar file is required to be loaded.
Two ways to load the jar file:
- paste the ojdbc14.jar file in jre/lib/ext folder
- set classpath
1) paste the ojdbc14.jar file in JRE/lib/ext folder:
Firstly, search the ojdbc14.jar file then go to JRE/lib/ext folder and paste the jar file here. |
2) set classpath:
There are two ways to set the classpath:
|
How to set the temporary classpath:
Firstly, search the ojdbc14.jar file then open command prompt and write: |
How to set the permanent classpath:
Go to environment variable then click on new tab. In variable name write classpath and in variable value paste the path to ojdbc14.jar by appending ojdbc14.jar;.; as C:\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jar;.;
Java Database Connectivity with MySQL
To connect Java application with the MySQL database, we need to follow 5 following steps.
In this example we are using MySql as the database. So we need to know following informations for the mysql database:
- Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.
- Connection URL: The connection URL for the mysql database is jdbc:mysql://localhost:3306/sonoo where jdbc is the API, mysql is the database, localhost is the server name on which mysql is running, we may also use IP address, 3306 is the port number and sonoo is the database name. We may use any database, in such case, we need to replace the sonoo with our database name.
- Username: The default username for the mysql database is root.
- Password: It is the password given by the user at the time of installing the mysql database. In this example, we are going to use root as the password.
Let's first create a table in the mysql database, but before creating table, we need to create database first.
Example to Connect Java Application with mysql database
In this example, sonoo is the database name, root is the username and password both.
The above example will fetch all the records of emp table.
To connect java application with the mysql database, mysqlconnector.jar file is required to be loaded.
Two ways to load the jar file:
- Paste the mysqlconnector.jar file in jre/lib/ext folder
- Set classpath
1) Paste the mysqlconnector.jar file in JRE/lib/ext folder:
Download the mysqlconnector.jar file. Go to jre/lib/ext folder and paste the jar file here. |
2) Set classpath:
There are two ways to set the classpath:
|
How to set the temporary classpath
open command prompt and write: |
How to set the permanent classpath
Go to environment variable then click on new tab. In variable name write classpath and in variable value paste the path to the mysqlconnector.jar file by appending mysqlconnector.jar;.; as C:\folder\mysql-connector-java-5.0.8-bin.jar;.;
Connectivity with Access without DSN
There are two ways to connect java application with the access database.
- Without DSN (Data Source Name)
- With DSN
Java is mostly used with Oracle, mysql, or DB2 database. So you can learn this topic only for knowledge.
Example to Connect Java Application with access without DSN
In this example, we are going to connect the java program with the access database. In such case, we have created the login table in the access database. There is only one column in the table named name. Let's get all the name of the login table.
Example to Connect Java Application with access with DSN
Connectivity with type1 driver is not considered good. To connect java application with type1 driver, create DSN first, here we are assuming your dsn name is mydsn.
Comments
Post a Comment