You are on page 1of 13

EX.NO.

11 MINI-PROJECT

AIM

To create a Mini project to implement MANAGEMENT OF BOOK RECORD using JAVA as


front-end and MYSQL as back-end.

DESIGN PLAN

The Design plan consists of the following:

 Project Plan
 Software requirement Analysis
 Implementation and Coding
 Software testing
 Software Debugging
 Conclusion

DESCRIPTION

We are very delighted and excited to finish this mini-project, as this is our first project involving
Databases. We have used MySql as Back-end and Java for Front-end work. We have used an
additional extension file named “rs2xml”. This project is for maintaining records of the books issued
by the librarian to the person. We have also added some features like Update and Delete, in case of
mistakes, while entering the details.

SOFTWARE REQUIREMENTS

 MySql Database
 Eclipse IDE
 Mysql connector jar file

TABLES

1) BOOK
create table book(id int(11) auto_increment , name varchar(255),edition int(15),price
int(15),primary key(id));

Fields Type Description

mysql> desc book ;


+---------+--------------+------+-----+---------+----------------
+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| edition | int | YES | | NULL | |
| price | int | YES | | NULL | |
+---------+--------------+------+-----+---------+----------------+
4 rows in set (0.04 sec)

STEPS TO CONNECT JAVA WITH MYSQL

1) Create a database in MYSQL.

2) Grant all privileges to user by providing password.

3) Import the packages: Requires that you include the packages containing the JDBC classes

needed for database programming. Most often using import java.sql.* will suffice.

4) Register the JDBC driver:- Requires that you initialize a driver so you can open a

communication channel with the database.

5) Open a connection: Requires using the DriverManager.getConnection() method to create a

Connection object, which represents a physical connection with the database.

6) Execute a Query: Requires using an object of type statement for building and submitting
an SQl

statement to the database.

7) Extract data from ResultSet: Requires that you use the appropriate ResultSet.getXXX()
method to retrieve the data from the ResultSet

STEPS TO IMPORT MYSQL CONNECTOR INTO YOUR PROJECT

1) Click project properties

2) Click on libraries and then ADD Library

3) Click on import,then you may many JARS available

4) Select the MYSQL JDBC driver and import it


SOURCE CODE

import java.awt.EventQueue;
import java.sql.*;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JoptionPane;

import net.proteanit.sql.DbUtils;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class JavaCrud {

private JFrame frame;


private JTextField txtbname;
private JTextField txtedition;
private JTextField txtprice;
private JTable table;
private JTextField txtbid;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
JavaCrud window = new JavaCrud();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the application.
*/
public JavaCrud() {
initialize();
Connect();
table_load();
}
Connection con;
PreparedStatement pst;
ResultSet rs;

public void Connect()


{
try {
Class.forName("com.mysql.jdbc.Driver");
con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/javacrud?
characterEncoding=latin1", "root","aravind");
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
catch (SQLException ex)
{
ex.printStackTrace();
}

public void table_load()


{
try
{
pst = con.prepareStatement("select * from book");
rs = pst.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
}
catch (SQLException e)
{
e.printStackTrace();
}
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1118, 594);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

JLabel lblNewLabel = new JLabel("BOOK SHOP");


lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 21));
lblNewLabel.setBounds(459, 28, 126, 26);
frame.getContentPane().add(lblNewLabel);

JPanel panel = new JPanel();


panel.setBorder(new TitledBorder(null, "REGISTRATION",
TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel.setBounds(25, 84, 484, 275);
frame.getContentPane().add(panel);
panel.setLayout(null);

JLabel lblNewLabel_1 = new JLabel("BOOK NAME");


lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 12));
lblNewLabel_1.setBounds(34, 67, 83, 14);
panel.add(lblNewLabel_1);

JLabel lblNewLabel_1_1 = new JLabel("EDITION");


lblNewLabel_1_1.setFont(new Font("Tahoma", Font.BOLD, 12));
lblNewLabel_1_1.setBounds(34, 119, 83, 14);
panel.add(lblNewLabel_1_1);

JLabel lblNewLabel_1_2 = new JLabel("PRICE ");


lblNewLabel_1_2.setFont(new Font("Tahoma", Font.BOLD, 12));
lblNewLabel_1_2.setBounds(34, 175, 83, 14);
panel.add(lblNewLabel_1_2);

txtbname = new JTextField();


txtbname.setBounds(127, 65, 226, 20);
panel.add(txtbname);
txtbname.setColumns(10);

txtedition = new JTextField();


txtedition.setColumns(10);
txtedition.setBounds(127, 117, 226, 20);
panel.add(txtedition);

txtprice = new JTextField();


txtprice.setColumns(10);
txtprice.setBounds(127, 173, 226, 20);
panel.add(txtprice);

JButton btnNewButton = new JButton("SAVE");


btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String bname,edition,price;
bname=txtbname.getText();
edition=txtedition.getText();
price=txtprice.getText();

try {
pst = con.prepareStatement("insert into
book(name,edition,price)values(?,?,?);");
pst.setString(1, bname);
pst.setString(2, edition);
pst.setString(3, price);
pst.executeUpdate();

JOptionPane.showMessageDialog(null,
"Record Added Successfully!!");

table_load();

txtbname.setText("");
txtedition.setText("");
txtprice.setText("");
txtbname.requestFocus();
}

catch (SQLException e1)


{
e1.printStackTrace();
}

}
});
btnNewButton.setBounds(25, 363, 89, 35);
frame.getContentPane().add(btnNewButton);

JButton btnExit = new JButton("EXIT ");


btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnExit.setBounds(146, 363, 89, 35);
frame.getContentPane().add(btnExit);

JButton btnClear = new JButton("CLEAR");


btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtbname.setText("");
txtedition.setText("");
txtprice.setText("");
txtbname.requestFocus();

}
});
btnClear.setBounds(271, 363, 89, 35);
frame.getContentPane().add(btnClear);

JScrollPane scrollPane = new JScrollPane();


scrollPane.setBounds(552, 83, 501, 275);
frame.getContentPane().add(scrollPane);

table = new JTable();


scrollPane.setViewportView(table);

JPanel panel_1 = new JPanel();


panel_1.setBorder(new TitledBorder(null, "SEARCH",
TitledBorder.LEADING, TitledBorder.TOP, null, null));
panel_1.setBounds(25, 431, 484, 83);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);

JLabel lblNewLabel_1_1_1 = new JLabel("BOOK ID");


lblNewLabel_1_1_1.setFont(new Font("Tahoma", Font.BOLD, 12));
lblNewLabel_1_1_1.setBounds(22, 36, 83, 14);
panel_1.add(lblNewLabel_1_1_1);

txtbid = new JTextField();


txtbid.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
try {

String id = txtbid.getText();

pst = con.prepareStatement("select
name,edition,price from book where id = ?");
pst.setString(1, id);
ResultSet rs = pst.executeQuery();

if(rs.next()==true)
{

String name = rs.getString(1);


String edition = rs.getString(2);
String price = rs.getString(3);

txtbname.setText(name);
txtedition.setText(edition);
txtprice.setText(price);

}
else
{
txtbname.setText("");
txtedition.setText("");
txtprice.setText("");

catch (SQLException ex) {

}
}
});
txtbid.setColumns(10);
txtbid.setBounds(122, 34, 226, 20);
panel_1.add(txtbid);

JButton btnUpdate = new JButton("UPDATE");


btnUpdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String bname,edition,price,bid;
bname = txtbname.getText();
edition = txtedition.getText();
price = txtprice.getText();
bid = txtbid.getText();

try {
pst = con.prepareStatement("update book set name
= ?,edition=?,price=? where id =?");
pst.setString(1, bname);
pst.setString(2, edition);
pst.setString(3, price);
pst.setString(4, bid);
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Record
Updated Successfully !!");

table_load();

txtbname.setText("");
txtedition.setText("");
txtprice.setText("");
txtbname.requestFocus();
}

catch (SQLException e1)


{

e1.printStackTrace();
}
}
});
btnUpdate.setBounds(638, 431, 89, 35);
frame.getContentPane().add(btnUpdate);

JButton btnDelete = new JButton("DELETE");


btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String bid;

bid = txtbid.getText();

try {
pst = con.prepareStatement("delete from book
where id = ?");

pst.setString(1, bid);
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Record
Deleted Sucessfully !!");

table_load();

txtbname.setText("");
txtedition.setText("");
txtprice.setText("");
txtbname.requestFocus();
}

catch (SQLException e1)


{

e1.printStackTrace();
}
}
});
btnDelete.setBounds(808, 431, 89, 35);
frame.getContentPane().add(btnDelete);
}
}

OUTPUT SCREEN SHOTS

INSERT
UPDATE
DELETE
RESULT:

Thus the front end and back end for the Book Record Management was designed and
executed successfully

You might also like