Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bookDatabase;
- public class Book {
- String bookName; // Create Variable for the book name//
- String authorName;
- String price;
- //We now create the setters and getters
- public Book( String bookName, String authorName, String price){
- this.bookName = bookName;
- this.authorName = authorName;
- this.price = price;
- }
- public void setbookName (String bookName ){
- this.bookName = bookName;
- }
- public void setauthorName (String authorName){
- this.authorName = authorName;
- }
- public void setprice (String price){
- this.price = price;
- }
- public String getbookName(){
- return bookName;
- }
- public String getauthorName (){
- return authorName;
- }
- public String getprice (){
- return price;
- }
- @Override
- public String toString(){
- return bookName + authorName + price ;
- }
- }
- package bookDatabase;
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.SQLException;
- public class guiBook extends JFrame{
- private String bookName;
- private String authorName;
- private String price;
- Book b1 = new Book(bookName, authorName, price);
- BookQuerie q1 = new BookQuerie(bookName, authorName, price);
- private JTabbedPane pane = new JTabbedPane();
- private JPanel panel1 = new JPanel();
- private JTextArea areaShow= new JTextArea("List of All Books");
- private JTable selectArea = new JTable();
- private JPanel panel2 = new JPanel();
- private JLabel update1 = new JLabel ("Insert Name of Book", JLabel.LEFT);
- private JTextField updateField1 = new JTextField(20);
- private JLabel update2 = new JLabel ("Enter the new Price", JLabel.LEFT);
- private JTextField updateField2 = new JTextField(20);
- private JButton updateButon = new JButton("Update price");
- private JPanel panel3 = new JPanel();
- private JTextField firstInsert = new JTextField(20);
- private JTextField secondInsert = new JTextField(20);
- private JTextField thirdInsert = new JTextField(20);
- private JButton insertButton = new JButton("Insert New Book");
- private JLabel labelInsert1 = new JLabel("Enter Book Name", JLabel.LEFT);
- private JLabel labelInsert2 = new JLabel("Enter Author Name", JLabel.LEFT);
- private JLabel labelInsert3 = new JLabel("Enter Price");
- private JPanel panel4 = new JPanel();
- private JTextField firstDelete = new JTextField(20);
- private JButton buttonDelete = new JButton("Delete Book");
- private JLabel labelDelete = new JLabel ( "Enter Name of Book", JLabel.LEFT);
- public guiBook() throws Exception{
- pane.add("Display All Books", panel1);
- panel1.add(areaShow, BorderLayout.WEST);
- areaShow.setEditable(false);
- panel1.add(selectArea, BorderLayout.EAST);
- {
- pane.add("Update Book Price", panel2);
- panel2.add(update1);
- panel2.add(updateField1);
- panel2.add(update2);
- panel2.add(updateField2);
- panel2.add(updateButon);
- updateButon.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e){
- String bookName = updateField1.getText();
- String price = updateField2.getText();
- try {
- BookQuerie.updateBooks(bookName, price);
- }
- catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- });
- pane.add("Insert New Book",panel3);
- panel3.add(labelInsert1);
- panel3.add(firstInsert);
- panel3.add(labelInsert2);
- panel3.add(secondInsert);
- panel3.add(labelInsert3);
- panel3.add(thirdInsert);
- panel3.add(insertButton);
- insertButton.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e){
- String bookName = firstInsert.getText();
- String authorName = secondInsert.getText();
- String price = thirdInsert.getText();
- try {
- BookQuerie.insertBooks(bookName,authorName, price);
- }
- catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- });
- pane.add("Delete Book", panel4);
- panel4.add(labelDelete);
- panel4.add(firstDelete);
- panel4.add(buttonDelete);
- buttonDelete.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent e){
- String bookName = firstDelete.getText();
- try {
- BookQuerie.deleteBooks(bookName);
- }
- catch (SQLException e1) {
- e1.printStackTrace();
- }
- }
- });
- add(pane);
- }
- }
- public static void main(String[] args) throws Exception{
- guiBook app = new guiBook();
- app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- app.setSize(1000,400);
- app.setVisible(true);
- }
- }
- package bookDatabase;
- import java.sql.*;
- import javax.swing.JOptionPane;
- public class BookQuerie {
- private static PreparedStatement selectAll;
- private static PreparedStatement updateBook;
- private static PreparedStatement insertBook;
- private static PreparedStatement deleteBook;
- private Connection connect;
- private String bookName;
- private static String authorName;
- private static String price;
- Book boooks = new Book(bookName, authorName, price);
- public BookQuerie(String bookName, String authorName, String price) {
- try{
- connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookshop", "root", "");
- selectAll = connect.prepareStatement("SELECT * FROM BOOK");
- updateBook = connect.prepareStatement("UPDATE book SET price = ? WHERE bookName = ?");
- insertBook = connect.prepareStatement("INSERT INTO book VALUES (?, ?, ?)");
- deleteBook= connect.prepareStatement("DELETE FROM book WHERE bookName = ? ");
- }
- catch
- (SQLException ex){
- ex.printStackTrace();
- }
- }
- public static String selectAll(String bookName, String authorName, String price) throws SQLException{
- ResultSet rs = selectAll.executeQuery();
- while(rs.next()){
- rs.getString ("bookName");
- rs.getString ("authorName");
- rs.getString ("price");
- }
- return selectAll(bookName, authorName, price);
- }
- public static int updateBooks(String bookName, String price) throws SQLException{
- updateBook.setString(1, bookName );
- updateBook.setString(2, price);
- int i = updateBook.executeUpdate();
- if(i>0){
- JOptionPane.showMessageDialog(null, "Data Is Saved!!");
- System.exit(0);
- }
- else{
- JOptionPane.showMessageDialog(null, "Data is Not Saved! :( " );
- System.exit(0);
- }
- return updateBooks(bookName, price);
- }
- public static int insertBooks(String bookName, String authorName, String price) throws SQLException{
- insertBook.setString(1, bookName);
- insertBook.setString(2,authorName);
- insertBook.setString(3, price);
- int i = insertBook.executeUpdate();
- if(i>0){
- JOptionPane.showMessageDialog(null, "Data Is Saved!!");
- }
- else{
- JOptionPane.showMessageDialog(null, "Data is Not Saved! :( " );
- }
- return insertBooks(bookName, authorName, price );
- }
- public static int deleteBooks (String bookName) throws SQLException{
- deleteBook.setString(1, bookName);
- return deleteBook.executeUpdate();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement