Book Shop project in java with Data Base

 Book Shop Project in Java


Background:

This program was mainly created to make the management in Bookshop easier for the customers as well as the Book shop owner. The project has the ability to add new books, Upate its price and also delete sold books. The advantage of this project is the usage of database(Mysql) help us to prevent the lose of data.

Why we Choosed this topic

The old way of managing bookstores are old and lengthy process. Moreover it was very difficiult to handle all the datas. Therefore we thought we need a new system to make the management easy and transparent. By making the system transparent both the workers and owners can know the stats of the shop more clearly and help them develop trust among themselves.

CODE SNIPPET:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;




public class T extends JFrame {
	private JPanel contentPane;
	private JTextField txtBname;
	private JTextField txtEname;
	private JTextField txtPname;
	private JTable table;
	private JTextField textField;
	Connection c;
	static Object[][] D;
	static int q=0;
	private JTable table_1;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					T frame = new T();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public T() {
			try {
				c = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookshop","root","root");
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			setBounds(100, 100, 672, 405);
			contentPane = new JPanel();
			contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
			setContentPane(contentPane);
			contentPane.setLayout(null);
			
			JLabel lblNewLabel = new JLabel("BOOKSHOP");
			lblNewLabel.setForeground(Color.BLACK);
			lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 30));
			lblNewLabel.setBounds(201, 11, 185, 43);
			contentPane.add(lblNewLabel);
			
			JPanel panel = new JPanel();
			panel.setBorder(new TitledBorder(null, "Registration Form", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			panel.setBounds(26, 65, 271, 138);
			contentPane.add(panel);
			panel.setLayout(null);
			
			JLabel lblNewLabel_1 = new JLabel("Book Name");
			lblNewLabel_1.setFont(new Font("Times New Roman", Font.BOLD, 20));
			lblNewLabel_1.setBounds(10, 27, 113, 14);
			panel.add(lblNewLabel_1);
			
			JLabel lblNewLabel_1_1 = new JLabel("Edition");
			lblNewLabel_1_1.setFont(new Font("Times New Roman", Font.BOLD, 20));
			lblNewLabel_1_1.setBounds(10, 69, 113, 14);
			panel.add(lblNewLabel_1_1);
			
			JLabel lblNewLabel_1_1_1 = new JLabel("Price");
			lblNewLabel_1_1_1.setFont(new Font("Times New Roman", Font.BOLD, 20));
			lblNewLabel_1_1_1.setBounds(10, 107, 113, 14);
			panel.add(lblNewLabel_1_1_1);
			
			txtBname = new JTextField();
			txtBname.setBounds(131, 26, 113, 20);
			panel.add(txtBname);
			txtBname.setColumns(10);
			
			txtEname = new JTextField();
			txtEname.setColumns(10);
			txtEname.setBounds(133, 63, 113, 20);
			panel.add(txtEname);
			
			txtPname = new JTextField();
			txtPname.setColumns(10);
			txtPname.setBounds(131, 101, 113, 20);
			panel.add(txtPname);
			
			JButton btnNewButton = new JButton("Save");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						c = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookshop","root","root");
						PreparedStatement s = c.prepareStatement("insert into details(Bookname,Edition,Price) values(?,?,?)");
						s.setString(1,txtBname.getText());
						s.setString(2, 	txtEname.getText());
						s.setInt(3,Integer.valueOf(txtPname.getText()));
						s.execute();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try {
						Connection d = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookshop","root","root");
						Statement a = d.createStatement();
						ResultSet m = a.executeQuery("select Sl_no, Bookname, Edition, Price from details");	
						q=0;
						while(m.next()) {
							q++;
						}
						int i =0;
						D= new Object[q][4];
						ResultSet w = a.executeQuery("select Sl_no, Bookname, Edition, Price from details");
						while(w.next()) {
							D[i][0] = w.getInt("Sl_no");
							D[i][1]=w.getString("Bookname");
							D[i][2]=w.getString("Edition");
							D[i][3]=w.getInt("Price");
							i++;
						}
						
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					String s [] = {"SL.NO", "Book Name", "Edition", "Price(Nu)"};
					table_1.setModel(new DefaultTableModel(
							D,
							s
						));
				}				
			});
			btnNewButton.setBounds(36, 214, 80, 43);
			contentPane.add(btnNewButton);
			
			JButton btnExit = new JButton("Exit");
			btnExit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
			btnExit.setBounds(127, 214, 80, 43);
			contentPane.add(btnExit);
			
			JButton btnClear = new JButton("Clear");
			btnClear.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					 		txtBname.setText("");
					 		txtEname.setText("");
					 		txtPname.setText("");
					 		
					
				}
			});
			btnClear.setBounds(217, 214, 80, 43);
			contentPane.add(btnClear);
			
			JScrollPane scrollPane = new JScrollPane();
			scrollPane.setBounds(307, 65, 339, 195);
			contentPane.add(scrollPane);
		
			
			scrollPane.setViewportView(table);
			
			table_1 = new JTable();
			scrollPane.setViewportView(table_1);
			
			JPanel panel_1 = new JPanel();
			panel_1.setBounds(26, 270, 271, 85);
			contentPane.add(panel_1);
			panel_1.setLayout(null);
			
			JLabel SN = new JLabel("Book Id");
			SN.setFont(new Font("Times New Roman", Font.BOLD, 20));
			SN.setBounds(10, 28, 113, 14);
			panel_1.add(SN);
			
			textField = new JTextField();
			textField.setColumns(10);
			textField.setBounds(109, 27, 113, 20);
			panel_1.add(textField);
			
			JButton btnNewButton_1 = new JButton("Search");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						int x = Integer.valueOf(textField.getText());
						Connection d = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookshop","root","root");
						Statement a = d.createStatement();
						ResultSet st = a.executeQuery("Select Sl_no, Bookname, Edition, Price from details where Sl_no = "+x+";");
						Object[][] dat = new Object[1][4]; 
						while(st.next()) {
							dat[0][0] = st.getInt("Sl_No");
							dat[0][1] = st.getString("Bookname");
							dat[0][2] = st.getString("Edition");
							dat[0][3] = st.getInt("Price");
							
						}
						String j[] = {"SL.NO", "Book Name", "Edition", "Price(Nu)"};
						table_1.setModel(new DefaultTableModel(
								dat,
								j
							));
						
					}
					catch(SQLException e1)
					{
						
					}
					
				}
			});
			btnNewButton_1.setBounds(108, 58, 89, 23);
			panel_1.add(btnNewButton_1);
			
			JButton btnUpdate = new JButton("Update");
			btnUpdate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						Connection d = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookshop","root","root");
						Statement a = d.createStatement();
						ResultSet m = a.executeQuery("select Sl_no, Bookname, Edition, Price from details");
						q=0;
						while(m.next()) {
							q++;
						}
						int i =0;
						D= new Object[q][4];
						ResultSet w = a.executeQuery("select Sl_no, Bookname, Edition, Price from details");
						while(w.next()) {
							D[i][0] = w.getInt("Sl_no");
							D[i][1]=w.getString("Bookname");
							D[i][2]=w.getString("Edition");
							D[i][3]=w.getInt("Price");
							i++;
						}
						
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					String s [] = {"SL.NO", "Book Name", "Edition", "Price(Nu)"};
					table_1.setModel(new DefaultTableModel(
							D,
							s
						));
				}
			});
			btnUpdate.setBounds(388, 288, 80, 43);
			contentPane.add(btnUpdate);
			
			JButton btnDelete = new JButton("Delete");
			btnDelete.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int row = table_1.getSelectedRow();
					int column = table_1.getSelectedColumn();
					
					//String o= table_1.getName();
					//System.out.print(o);
					if(column==0 ) {
						int o = (int) table_1.getValueAt(row, column);
						
					
						try {
							Connection d = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookshop","root","root");
							Statement i = d.createStatement();
							i.execute("DELETE FROM details WHERE Sl_no = "+o+";");
							
						}
						catch(SQLException e1)
						{
							
						}
					}
					if(column==1)
					{
						String o = (String) table_1.getValueAt(row, column);
						try {
							Connection d = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookshop","root","root");
							Statement i = d.createStatement();
							i.execute("DELETE FROM details WHERE Bookname = '"+o+"';");
							
						}
						catch(SQLException e1)
						{
							
						}
					}
					if(column==2)
					{
						String o = (String) table_1.getValueAt(row, column);
						try {
							Connection d = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookshop","root","root");
							Statement i = d.createStatement();
							i.execute("DELETE FROM details WHERE Edition = '"+o+"';");
							
						}
						catch(SQLException e1)
						{
							
						}
					}
					if(column==3)
					{
						int o = (int) table_1.getValueAt(row, column);
						try {
							Connection d = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookshop","root","root");
							Statement i = d.createStatement();
							i.execute("DELETE FROM details WHERE Price = "+o+";");
							
						}
						catch(SQLException e1)
						{
							
						}
					}
						
				}
			});
			btnDelete.setBounds(478, 288, 80, 43);
			contentPane.add(btnDelete);
	}

}



5 comments: