Now we focus on JDBC Operations – Insert Records and Display.

Before continuing, go through this earlier post to create the database table.

Database Table Structure for JDBC Operations - Insert Records and Display
Database Table Structure for JDBC Operations – Insert Records and Display

Detailed Description of JDBC Operations – Insert Records and Display

Here we insert records in the Exam table using a JavaBean. For this purpose, define a method addUser_1() with the following signature.

String addUser_1(UserBean userBean);

Also, perform the following tasks.

(i) Create a Java class UserBean with the following private variables – userId, password, userName, userType, incorrectAttempts, and lockStatus. Also, define getters, and setters for each variable.

(ii) Obtain the Connection object using the getDBConnection() method. After that, insert a record using the above method that returns a String. When the record is successfully inserted, the return value should be “Success”. Otherwise, the return value should be “fail”.

(iii) Call the addUser_1() method from the main() method. Check the inserted value using SqlPlus at the command line.

Similarly, create a method addUser_2(), with precisely the same parameters as taken by the addUser_1() method. Also, it should perform the same tasks. However, it should insert a record only when the value of lockStatus is 0.

Further, create a method getUsers() with the following signature.

ArrayList<UserBean> getUsers(String userType);

This method should perform the following tasks.

(i) Obtain the Connection object using the getDBConnetion() method and retrieve all records with the specified user_type.

(ii) Add all these records to an ArrayList and return that ArrayList.

(iii) Within the main() method, retrieve all the specified records and display these records.

Likewise, create a method storeAllRecords() that returns an ArrayList containing all records from the Exam table.

Another method getNames() need to be created with the following signatures:

String[] getNames();

This method should return an array of strings containing user names. Display the user names by calling this method from the main() method.


Further Reading

Understanding Enterprise Java Beans

Java Practice Exercise

Princites