Here we will discuss Implementing Database Operations Using JDBC.

To begin with, first, have a look at the problem statement here.

https://www.programmingempire.com/a-mini-project-on-jdbc-operation/

https://www.programmingempire.com/jdbc-operations—insert-records-and-display/

Firstly, launch the Eclipse IDE. After that, create a new Java Project Exams, as shown below.

Create a New Project in Eclipse
Create a New Project in Eclipse

Since we are using Oracle 11g for the database, we need to include the corresponding jar file in the Reference Libraries in the project. For this purpose, right-click on the project name and select the option Build Path. Then select Configure Build Path as shown below.

Build Path Option in Eclipse
Build Path Option in Eclipse

Now click on the option Add External JARs and browse the ojdbc14.jar in your system as shown below.

Adding ojdbc14.jar file in the JDBC project
Adding ojdbc14.jar file in the JDBC project

Now add the following classes to the project. The classes that we will use in this project are given below along with the package names.

Class NamePackage Name
DBUtilcom.programmingempire.exams.util
ExamsDAOcom.programmingempire.exams.dao
UserBeancom.programmingempire.exams.bean
Exams.Maincom.programmingempire.exams.work
Classes and Packages Used in the Exams Projects

The following image shows the project structure after adding the classes.

The Project Structure for Implementing Database Operations Using JDBC
The Project Structure for Implementing Database Operations Using JDBC

Establishing the Connection for Implementing Database Operations Using JDBC

Once the required files are created, write the necessary code in each file. So, open the file DBUtil.java and write the code for establishing connection with the database. The following code shows how to establish the database connection.

package com.programmingempire.exams.util;
import java.sql.*;
public class DBUtil {
	public static Connection getDBConnection()
	{
		Connection con=null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "username", "password");
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
		return con;
	}
}

As can be seen in the above code, create an variable con of the Connection type. After that, load the corresponding database driver using the Class.forName() method. Then call the getConnection() method of the DriverManager class that returns the Connection object. Also, you need to provide the userame, and password for Oracle.

Secondly, create a UserBean class. The following code demonstrates it.

package com.programmingempire.exams.bean;

public class UserBean {
	private String userID;
	private String password;
	private String userType;
	private String userName;
	private int incorrectAttempts;
	private int lockStatus;
	public String getUserID() {
		return userID;
	}
	public void setUserID(String userID) {
		this.userID = userID;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUserType() {
		return userType;
	}
	public void setUserType(String userType) {
		this.userType = userType;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public int getIncorrectAttempts() {
		return incorrectAttempts;
	}
	public void setIncorrectAttempts(int incorrectAttempts) {
		this.incorrectAttempts = incorrectAttempts;
	}
	public int getLockStatus() {
		return lockStatus;
	}
	public void setLockStatus(int lockStatus) {
		this.lockStatus = lockStatus;
	}
}

Next we need to write code for database operations. For this purpose, we use the class ExamsDAO.


Further Reading

Understanding Enterprise Java Beans

Java Practice Exercise

Princites