Library Management System Project in Java: Step by Step guide

 public static void admin_menu() {JFrame f=new JFrame("Admin Functions"); //Give dialog box name as admin functions //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // JButton create_but=new JButton("Create/Reset");//creating instance of JButton to create or reset databasecreate_but.setBounds(450,60,120,25);//x axis, y axis, width, heightcreate_but.addActionListener(new ActionListener() { //Perform actionpublic void actionPerformed(ActionEvent e){create(); //Call create functionJOptionPane.showMessageDialog(null,"Database Created/Reset!"); //Open a dialog box and display the message}});JButton view_but=new JButton("View Books");//creating instance of JButton to view booksview_but.setBounds(20,20,120,25);//x axis, y axis, width, heightview_but.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e){ JFrame f = new JFrame("Books Available"); //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Connection connection = connect(); //connect to databaseString sql="select * from BOOKS"; //select all books try {Statement stmt = connection.createStatement(); stmt.executeUpdate("USE LIBRARY"); //use database stmt=connection.createStatement(); ResultSet rs=stmt.executeQuery(sql); JTable book_list= new JTable(); //view data in table format book_list.setModel(DbUtils.resultSetToTableModel(rs)); //mention scroll bar JScrollPane scrollPane = new JScrollPane(book_list); f.add(scrollPane); //add scrollpane f.setSize(800, 400); //set size for frame f.setVisible(true);f.setLocationRelativeTo(null); } catch (SQLException e1) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, e1); }}});JButton users_but=new JButton("View Users");//creating instance of JButton to view usersusers_but.setBounds(150,20,120,25);//x axis, y axis, width, heightusers_but.addActionListener(new ActionListener() { //Perform action on click buttonpublic void actionPerformed(ActionEvent e){ JFrame f = new JFrame("Users List"); //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Connection connection = connect();String sql="select * from users"; //retrieve all users try {Statement stmt = connection.createStatement(); stmt.executeUpdate("USE LIBRARY"); //use database stmt=connection.createStatement(); ResultSet rs=stmt.executeQuery(sql); JTable book_list= new JTable(); book_list.setModel(DbUtils.resultSetToTableModel(rs)); //mention scroll bar JScrollPane scrollPane = new JScrollPane(book_list); f.add(scrollPane); //add scrollpane f.setSize(800, 400); //set size for frame f.setVisible(true);f.setLocationRelativeTo(null); } catch (SQLException e1) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, e1); }}});JButton issued_but=new JButton("View Issued Books");//creating instance of JButton to view the issued booksissued_but.setBounds(280,20,160,25);//x axis, y axis, width, heightissued_but.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e){ JFrame f = new JFrame("Users List"); //f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);Connection connection = connect();String sql="select * from issued"; try {Statement stmt = connection.createStatement(); stmt.executeUpdate("USE LIBRARY"); stmt=connection.createStatement(); ResultSet rs=stmt.executeQuery(sql); JTable book_list= new JTable(); book_list.setModel(DbUtils.resultSetToTableModel(rs)); JScrollPane scrollPane = new JScrollPane(book_list); f.add(scrollPane); f.setSize(800, 400); f.setVisible(true);f.setLocationRelativeTo(null); } catch (SQLException e1) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, e1); }}});JButton add_user=new JButton("Add User"); //creating instance of JButton to add usersadd_user.setBounds(20,60,120,25); //set dimensions for buttonadd_user.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e){ JFrame g = new JFrame("Enter User Details"); //Frame to enter user details //g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create label JLabel l1,l2; l1=new JLabel("Username"); //label 1 for username l1.setBounds(30,15, 100,30); l2=new JLabel("Password"); //label 2 for password l2.setBounds(30,50, 100,30);//set text field for usernameJTextField F_user = new JTextField();F_user.setBounds(110, 15, 200, 30);//set text field for passwordJPasswordField F_pass=new JPasswordField();F_pass.setBounds(110, 50, 200, 30);//set radio button for adminJRadioButton a1 = new JRadioButton("Admin");a1.setBounds(55, 80, 200,30);//set radio button for userJRadioButton a2 = new JRadioButton("User");a2.setBounds(130, 80, 200,30);//add radio buttonsButtonGroup bg=new ButtonGroup();bg.add(a1);bg.add(a2);JButton create_but=new JButton("Create");//creating instance of JButton for Createcreate_but.setBounds(130,130,80,25);//x axis, y axis, width, heightcreate_but.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e){String username = F_user.getText();String password = F_pass.getText();Boolean admin = false;if(a1.isSelected()) {admin=true;}Connection connection = connect();try {Statement stmt = connection.createStatement(); stmt.executeUpdate("USE LIBRARY"); stmt.executeUpdate("INSERT INTO USERS(USERNAME,PASSWORD,ADMIN) VALUES ('"+username+"','"+password+"',"+admin+")"); JOptionPane.showMessageDialog(null,"User added!"); g.dispose();}catch (SQLException e1) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, e1); }}});g.add(create_but);g.add(a2);g.add(a1);g.add(l1);g.add(l2);g.add(F_user);g.add(F_pass);g.setSize(350,200);//400 width and 500 heightg.setLayout(null);//using no layout managersg.setVisible(true);//making the frame visibleg.setLocationRelativeTo(null);}});JButton add_book=new JButton("Add Book"); //creating instance of JButton for adding booksadd_book.setBounds(150,60,120,25);add_book.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e){//set frame wot enter book details JFrame g = new JFrame("Enter Book Details"); //g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set labels JLabel l1,l2,l3; l1=new JLabel("Book Name"); //lebel 1 for book name l1.setBounds(30,15, 100,30); l2=new JLabel("Genre"); //label 2 for genre l2.setBounds(30,53, 100,30); l3=new JLabel("Price"); //label 2 for price l3.setBounds(30,90, 100,30);//set text field for book nameJTextField F_bname = new JTextField();F_bname.setBounds(110, 15, 200, 30);//set text field for genreJTextField F_genre=new JTextField();F_genre.setBounds(110, 53, 200, 30);//set text field for priceJTextField F_price=new JTextField();F_price.setBounds(110, 90, 200, 30);JButton create_but=new JButton("Submit");//creating instance of JButton to submit detailscreate_but.setBounds(130,130,80,25);//x axis, y axis, width, heightcreate_but.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e){// assign the book name, genre, priceString bname = F_bname.getText();String genre = F_genre.getText();String price = F_price.getText();//convert price of integer to intint price_int = Integer.parseInt(price);Connection connection = connect();try {Statement stmt = connection.createStatement(); stmt.executeUpdate("USE LIBRARY"); stmt.executeUpdate("INSERT INTO BOOKS(BNAME,GENRE,PRICE) VALUES ('"+bname+"','"+genre+"',"+price_int+")"); JOptionPane.showMessageDialog(null,"Book added!"); g.dispose();}catch (SQLException e1) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, e1); }}});g.add(l3);g.add(create_but);g.add(l1);g.add(l2);g.add(F_bname);g.add(F_genre);g.add(F_price);g.setSize(350,200);//400 width and 500 heightg.setLayout(null);//using no layout managersg.setVisible(true);//making the frame visibleg.setLocationRelativeTo(null);}});JButton issue_book=new JButton("Issue Book"); //creating instance of JButton to issue booksissue_book.setBounds(450,20,120,25);issue_book.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e){//enter details JFrame g = new JFrame("Enter Details"); //g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //create labels JLabel l1,l2,l3,l4; l1=new JLabel("Book ID(BID)"); // Label 1 for Book ID l1.setBounds(30,15, 100,30); l2=new JLabel("
User ID(UID)"); //Label 2 for user ID l2.setBounds(30,53, 100,30); l3=new JLabel("Period(days)"); //Label 3 for period l3.setBounds(30,90, 100,30); l4=new JLabel("Issued Date(DD-MM-YYYY)"); //Label 4 for issue date l4.setBounds(30,127, 150,30);JTextField F_bid = new JTextField();F_bid.setBounds(110, 15, 200, 30);JTextField F_uid=new JTextField();F_uid.setBounds(110, 53, 200, 30);JTextField F_period=new JTextField();F_period.setBounds(110, 90, 200, 30);JTextField F_issue=new JTextField();F_issue.setBounds(180, 130, 130, 30);JButton create_but=new JButton("Submit");//creating instance of JButtoncreate_but.setBounds(130,170,80,25);//x axis, y axis, width, heightcreate_but.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e){String uid = F_uid.getText();String bid = F_bid.getText();String period = F_period.getText();String issued_date = F_issue.getText();int period_int = Integer.parseInt(period);Connection connection = connect();try {Statement stmt = connection.createStatement(); stmt.executeUpdate("USE LIBRARY"); stmt.executeUpdate("INSERT INTO ISSUED(UID,BID,ISSUED_DATE,PERIOD) VALUES ('"+uid+"','"+bid+"','"+issued_date+"',"+period_int+")"); JOptionPane.showMessageDialog(null,"Book Issued!"); g.dispose();}catch (SQLException e1) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, e1); }}});g.add(l3);g.add(l4);g.add(create_but);g.add(l1);g.add(l2);g.add(F_uid);g.add(F_bid);g.add(F_period);g.add(F_issue);g.setSize(350,250);//400 width and 500 heightg.setLayout(null);//using no layout managersg.setVisible(true);//making the frame visibleg.setLocationRelativeTo(null);}});JButton return_book=new JButton("Return Book"); //creating instance of JButton to return booksreturn_book.setBounds(280,60,160,25);return_book.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e){ JFrame g = new JFrame("Enter Details"); //g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set labels JLabel l1,l2,l3,l4; l1=new JLabel("Issue ID(IID)"); //Label 1 for Issue ID l1.setBounds(30,15, 100,30); l4=new JLabel("Return Date(DD-MM-YYYY)"); l4.setBounds(30,50, 150,30);JTextField F_iid = new JTextField();F_iid.setBounds(110, 15, 200, 30);JTextField F_return=new JTextField();F_return.setBounds(180, 50, 130, 30);JButton create_but=new JButton("Return");//creating instance of JButton to mention return date and calculcate finecreate_but.setBounds(130,170,80,25);//x axis, y axis, width, heightcreate_but.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e){String iid = F_iid.getText();String return_date = F_return.getText();Connection connection = connect();try {Statement stmt = connection.createStatement(); stmt.executeUpdate("USE LIBRARY"); //Intialize date1 with NULL value String date1=null; String date2=return_date; //Intialize date2 with return date //select issue date ResultSet rs = stmt.executeQuery("SELECT ISSUED_DATE FROM ISSUED WHERE IID="+iid); while (rs.next()) { date1 = rs.getString(1); } try {Date date_1=new SimpleDateFormat("dd-MM-yyyy").parse(date1);Date date_2=new SimpleDateFormat("dd-MM-yyyy").parse(date2);//subtract the dates and store in diff long diff = date_2.getTime() - date_1.getTime();//Convert diff from milliseconds to days ex.days=(int)(TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));} catch (ParseException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} //update return date stmt.executeUpdate("UPDATE ISSUED SET RETURN_DATE='"+return_date+"' WHERE IID="+iid); g.dispose(); Connection connection1 = connect(); Statement stmt1 = connection1.createStatement(); stmt1.executeUpdate("USE LIBRARY");ResultSet rs1 = stmt1.executeQuery("SELECT PERIOD FROM ISSUED WHERE IID="+iid); //set period String diff=null;while (rs1.next()) { diff = rs1.getString(1); }int diff_int = Integer.parseInt(diff);if(ex.days>diff_int) { //If number of days are more than the period then calculcate fine//System.out.println(ex.days);int fine = (ex.days-diff_int)*10; //fine for every day after the period is Rs 10.//update fine in the systemstmt1.executeUpdate("UPDATE ISSUED SET FINE="+fine+" WHERE IID="+iid);String fine_str = ("Fine: Rs. "+fine);JOptionPane.showMessageDialog(null,fine_str);} JOptionPane.showMessageDialog(null,"Book Returned!");}catch (SQLException e1) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, e1); }}});g.add(l4);g.add(create_but);g.add(l1);g.add(F_iid);g.add(F_return);g.setSize(350,250);//400 width and 500 heightg.setLayout(null);//using no layout managersg.setVisible(true);//making the frame visibleg.setLocationRelativeTo(null);}});f.add(create_but);f.add(return_book);f.add(issue_book);f.add(add_book);f.add(issued_but);f.add(users_but);f.add(view_but);f.add(add_user);f.setSize(600,200);//400 width and 500 heightf.setLayout(null);//using no layout managersf.setVisible(true);//making the frame visiblef.setLocationRelativeTo(null);} } 

Now that you have understood all the functions, let us execute our library management system project in Java and see the outputs.

Output:

Execute the application by clicking on the run button. Once, you execute you will see the below dialog box. In the below dialog box, mention username and password as {admin, admin}. Then click on the Login button.

Login Admin - Library Management System Project in Java - Edureka

Once you click on the Login button, you will see the below dialog box opening up.

Admin Functions - Library Management System Project in Java - Edureka

Here you have various options which you can explore. So, let us start with the first one:

View Books

Once, you click on View Books button, you will see the below frame displaying all the books present in the database, with their details.

Books Available - Library Management System Project in Java - Edureka

View Users

The View Users button is used to view the current users on the system. Since we just have only one user present i.e the admin, it will show you output as below:

Users List - Library Management System Project in Java - Edureka

Create/Reset

This functionality is used to create or reset a database. So, once you click on the button Create/Rest, you will see the below output:

Databa
se Created - Library Management System Project in Java - Edureka

Add User

To add a user, click on the option “Add User” and mention details such as username, password and choose the radio button user or admin. By default, it will be the user. Then, click on Create.

Login User - Library Management System Project in Java - Edureka

Once the user is created, you will see an output as below:

User Added - Library Management System Project in Java - Edureka

Now, again if you click on View Users button, you will see the below output:

User List - Library Management System Project in Java - Edureka

Alright, so now that we have added a user. Let us say, that particular user wants to issue books. To do that, the user has to choose the option of Issue Book.

Issue Book

Suppose, if you are the user, once you click on the Issue Book button, you have to mention the Book ID, User ID, Period(Number of days for issuing the book), and the Issue Date as follows:

Issue Books - Library Management System Project in Java - Edureka

Then click on Submit. Once, you click on Submit, you will see the below dialog box:

Books Issued - Library Management System Project in Java - Edureka

Now, if you want to see the issued books details, you can use the View Issued Books functionality.

View Issued Books

Once you click on this button, you will see the following output:

User Details - Library Management System Project in Java - Edureka

Alright, so, now if the user logs in to the system, using the login function, as below:

User - Library Management System Project in Java - EdurekaThen the user will see the below User Menu.

User Functions - Library Management System Project in Java - Edureka

Here, the user can view all the books in the database by using the View Books option and the books issued by the user in the My Books section as below:

Books - Library Management System Project in Java - Edureka

Now, if you wish to return the book, then you have to choose the option of Return Book.

Return Book

Once, you click on the Return Book, mention the Issue ID and the return date as below. Then click on Return.

Return Books - Library Management System Project in Java - EdurekaThen, you see a message box displaying the fine.

Calculate Fine - Library Management System Project in Java - Edureka

After that, you again see a dialog box, showing the message “Book Returned“. Refer below.

Book Returned - Library Management System Project in Java - Edureka

Now, if you click on the View Issued Books, you will see the below output:

Show User Details - Library Management System Project in Java - Edureka

Lastly, if you wish to add a book
, you can use the option of Add Book.

Add Book

Click on the Add Book button, and mention the book name, genre and price. Then, click on the Submit button. Refer below.

Add Books - Library Management System Project in Java - Edureka

You will see a dialog box displaying the below message:

Books Added - Library Management System Project in Java - Edureka

Apart from this, you can also, see the added books in the View Books section as below:

Books Available In Database - Library Management System Project in Java - Edureka

This brings us to the end of our article on Library Management System Project in Java. I hope you found this article informative and added value to your knowledge.

Check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

If you’re just beginning, then watch at this Java Tutorial to Understand the Fundamental Java Concepts.

Got a question for us? Please mention it in the comments section of this “Library Management System Project in Java” article and we will get back to you as soon as possible or you can join Java Training in UAE.

Check Also

Descargar Microsoft Word última versión en Español

excerpt

Leave a Reply

Your email address will not be published. Required fields are marked *