NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

import java.awt.EventQueue;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTextPane;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import java.awt.Color;

public class Book {


private JFrame frame;
Connection connection = null;
Statement sta=null;
private JTable table;
DefaultTableModel model;
protected DefaultTableModel defaultTableModel;



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

/**
* Create the application.
*/
public Book() {
initialize();
}

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

JLabel Book = new JLabel("Store book");
Book.setBounds(115, 26, 124, 14);
frame.getContentPane().add(Book);

JLabel lblname = new JLabel("Title");
lblname.setBounds(23, 54, 46, 14);
frame.getContentPane().add(lblname);

JLabel lblisb = new JLabel("ISBN");
lblisb.setBounds(23, 79, 46, 14);
frame.getContentPane().add(lblisb);

JLabel lblNewLabel_2 = new JLabel("Author");
lblNewLabel_2.setBounds(23, 123, 46, 14);
frame.getContentPane().add(lblNewLabel_2);

JLabel lblNewLabel_3 = new JLabel("Year");
lblNewLabel_3.setBounds(23, 148, 71, 14);
frame.getContentPane().add(lblNewLabel_3);
JTextPane txtnamebook = new JTextPane();
txtnamebook.setBounds(104, 54, 70, 20);
frame.getContentPane().add(txtnamebook);

JTextPane lblauthor = new JTextPane();
lblauthor.setBounds(104, 117, 70, 20);
frame.getContentPane().add(lblauthor);

JTextPane txtisbn = new JTextPane();
txtisbn.setBounds(104, 89, 70, 20);
frame.getContentPane().add(txtisbn);

JTextPane txtcopy = new JTextPane();
txtcopy.setBounds(104, 143, 70, 20);
frame.getContentPane().add(txtcopy);

JLabel lblNewLabel_3_1 = new JLabel("Price");
lblNewLabel_3_1.setBounds(23, 180, 46, 14);
frame.getContentPane().add(lblNewLabel_3_1);

JTextPane txtprice = new JTextPane();
txtprice.setBounds(104, 174, 70, 20);
frame.getContentPane().add(txtprice);

JButton btnadd = new JButton("Insert");
btnadd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Name = txtnamebook.getText();
String ISBN =txtisbn.getText();
String author = lblauthor.getText();
int year =Integer.parseInt(txtcopy.getText().trim());
int price =Integer.parseInt(txtprice.getText().trim());


String msg = "";
msg += " n";

JOptionPane.showMessageDialog(btnadd, "You want to add it?");



try {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore?useSSL=false", "root", "root");
String query = "INSERT INTO book values('" + Name + "','" + ISBN + "','" + author + "','" +year + "','" + price + "')";

Statement sta = connection.createStatement();
int x = sta.executeUpdate(query);
if (x == 0) {
JOptionPane.showMessageDialog(btnadd, "Inserted Succesfully");
}
connection.close();
} catch (Exception exception) {
exception.printStackTrace();
}

//int a = JOptionPane.showConfirmDialog(btnadd, "?");
//if (a == JOptionPane.YES_OPTION) {
//dispose();
//Login obj = new Login();
// obj.setTitle("Student-Login");
//obj.setVisible(true);
}
// dispose();
//Login obj = new Login();

// obj.setTitle("Student-Login");
// obj.setVisible(true);
//}
});

btnadd.setBounds(10, 255, 74, 23);
frame.getContentPane().add(btnadd);

JButton btnDelete = new JButton("Delete");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Name = txtnamebook.getText();
String ISBN =txtisbn.getText();
String author = lblauthor.getText();
int year =Integer.parseInt(txtcopy.getText().trim());
int price =Integer.parseInt(txtprice.getText().trim());
try {
int row = table.getSelectedRow();
String id = (table.getModel().getValueAt(row, 1)).toString();
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore?useSSL=false", "root", "root");
String query = "DELETE FROM book where Name = ?";



Statement sta = connection.createStatement();
int x = sta.executeUpdate(query);
if (x == 0) {
JOptionPane.showMessageDialog(btnadd, "Deleted Succesfully");
}
connection.close();
} catch (Exception exception) {
exception.printStackTrace();
}
}
});
btnDelete.setBounds(93, 255, 81, 23);
frame.getContentPane().add(btnDelete);

JButton btnupdate = new JButton("Update");
btnupdate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Name = txtnamebook.getText();
String ISBN =txtisbn.getText();
String author = lblauthor.getText();
int year =Integer.parseInt(txtcopy.getText().trim());
int price =Integer.parseInt(txtprice.getText().trim());
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore?useSSL=false", "root", "root");
String sql = "update book Set ISBN= ?, author= ?, year= ?, price= ? where Name= ?";

Statement sta = connection.createStatement();
//sta.setString
int x = sta.executeUpdate(sql);
if (x == 0) {
JOptionPane.showMessageDialog(btnadd, "Updated Succesfully");
}
connection.close();
} catch (Exception exception) {
exception.printStackTrace();
}
}
});

btnupdate.setHorizontalAlignment(SwingConstants.RIGHT);
btnupdate.setBounds(10, 282, 74, 23);
frame.getContentPane().add(btnupdate);

JButton btnexit = new JButton("Exit");
btnexit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnexit.setBounds(93, 282, 81, 23);
frame.getContentPane().add(btnexit);

JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(212, 25, 330, 282);
frame.getContentPane().add(scrollPane);

table = new JTable();
table.setBackground(new Color(176, 224, 230));
model=new DefaultTableModel();
Object[] column = {"name","isbn","author","year","price"};
Object[] row = new Object[0];
model.setColumnIdentifiers(column);
table.setModel(model);
scrollPane.setViewportView(table);

JButton btnrow = new JButton("add row");
btnrow.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String Name = txtnamebook.getText();
String ISBN =txtisbn.getText();
String author = lblauthor.getText();
int year =Integer.parseInt(txtcopy.getText().trim());
int price =Integer.parseInt(txtprice.getText().trim());
try {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore?useSSL=false", "root", "root");
String sql = "select * from BOOK where Name = '" +Name + "'";//Storing MySQL query in A string variable



Statement sta = connection.createStatement();
//sta.setString
int x = sta.executeUpdate(sql);
while (((ResultSet) sta).next()) {
if (Name.equalsIgnoreCase(Name)) {
x = 1;
defaultTableModel.addRow(new Object[]{Name, ISBN, author,year,price});//Adding row in Table
frame.setVisible(true);//Setting the visibility of second Frame
frame.validate();
break;
}
if (x == 0) {
JOptionPane.showMessageDialog(null, "No Such book Found");

} }
connection.close();

}
catch (Exception exception) {
exception.printStackTrace();
}












}
});
btnrow.setBounds(350, 308, 32, 23);
frame.getContentPane().add(btnrow);


}
}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.