Java Database Connectivity

Snehatiwari
3 min readJun 13, 2021

What is the JDBC(Java database connectivity)?

JDBC is a Java API(application programming interface) which is used to connect and execute query with the database.

here we can see in image below:-

In the image we can see that java application is connected to database through the JDBC driver. JDBC driver play a important role.

There are some steps which is used to connect database to java application:-

  1. Register driver object:-the first step which is used load the driver. forName() method of the Class is used to load the driver. The driver can be of any database just like oracle,mysql,etc. Before uploading driver you must be installed driver in you pc.

Syntax of forName() method:-

public static void forName(String className)throws ClassNotFoundException

driver load

sometimes when you load driver you got an exception ClassNotFoundException. In netbeans, you go to library->right click->add library->create ->create new library->add jar folder->driver path->add->ok

now when you complete these steps then again add exist library that you created and then load your driver.

2. Create Connection Object:- The getConnection() method of DriverManager class is used to establish connection with the database.

Syntax of getConnection() method:-

public static Connection getConnection(String url,String name,String password)throws SQLException

connection object

3. Create 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:-

public Statement createStatement()throws SQLException

statement object

4. Execute Query (ResultSet):-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:-

public ResultSet executeQuery(String sql)throws SQLException

5. Close Connection:- 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

public void close()throws SQLException

conn.close();

some information that we should know:

  1. Driver class: The driver class for the oracle database is oracle.jdbc.driver.OracleDriver.
  2. Connection URL: The connection URL for the oracle10G database is jdbc:oracle:thin:@localhost:1521:xe where jdbc is the API, oracle is the database, thin is the driver, localhost is the server name on which oracle is running, we may also use IP address, 1521 is the port number and XE is the Oracle service name. You may get all these information from the tnsnames.ora file.
  3. Username: The default username for the oracle database is system.
  4. Password: It is the password given by the user at the time of installing the oracle database

Example:

I hope this article will be helpful for you.

Sneha tiwari

--

--