Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package application;
- import java.sql.Connection;
- import java.sql.Date;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.time.LocalDate;
- import java.time.format.DateTimeFormatter;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Alert;
- import javafx.scene.control.Button;
- import javafx.scene.control.ButtonType;
- import javafx.scene.control.ChoiceBox;
- import javafx.scene.control.DatePicker;
- import javafx.scene.control.TextField;
- import javafx.scene.control.Alert.AlertType;
- import javafx.scene.layout.GridPane;
- import javafx.scene.text.Text;
- import javafx.stage.Stage;
- import sun.util.resources.LocaleData;
- public class ModifyBook extends Stage{
- ModifyBook ( String type ,String ISBN ,Stage prev , Stage pPrev , int id ) throws SQLException{
- Stage current = this;
- setAttributes(type ,prev,ISBN,current , pPrev ,id);
- }
- private void setAttributes(String type ,Stage prev ,String isbn , Stage current , Stage pPrev , int id) throws SQLException {
- String input;
- Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/Online_Book_Store?useSSL=false", "root", "ahmed666" );
- String query="select * from BOOK_STORE where ISBN = ?";
- PreparedStatement pst=con.prepareStatement(query);
- pst.setString(1,isbn);
- ResultSet rs = pst.executeQuery();
- while(rs.next()) {
- Text ISBN = new Text("book ISBN ");
- Text title = new Text("title ");
- Text bookCopies = new Text("bookCopies ");
- Text pubisherID = new Text("publisher ID ");
- Text pubplicationYear = new Text("publication year ");
- Text threshold = new Text("threshold ");
- Text price = new Text("price ");
- Text category = new Text("category");
- Text authorID= new Text("Author ID");
- Text authorName = new Text("Author Name");
- TextField bookISBNField = new TextField();
- bookISBNField.setText(rs.getString("ISBN"));
- TextField titleField = new TextField(rs.getString("TITLE"));
- TextField BookCopiesField = new TextField(rs.getString("BOOK_COPIES"));
- TextField publisherIDField = new TextField(rs.getString("PUBLISHER_ID"));
- DatePicker publicationYearField = new DatePicker();
- TextField thresholdField = new TextField(rs.getString("THRESHOLD"));
- TextField priceField = new TextField(rs.getString("PRICE"));
- TextField AuthorIDField = new TextField(rs.getString("AUTHOR_ID"));
- TextField AuthorNameField = new TextField(rs.getString("AUTHOR_Name"));
- ChoiceBox categoryField = new ChoiceBox();
- categoryField.getItems().addAll( "Science", "Art", "Religion","History" , "Geography");
- categoryField.setValue(rs.getString("CATEGORY"));
- Button editBook = new Button("edit book");
- Button cancel = new Button("cancel");
- GridPane gridPane = new GridPane();
- gridPane.setMinSize(600, 400);
- gridPane.setPadding(new Insets(10, 10, 10, 10));
- // Setting the vertical and horizontal gaps between the columns
- gridPane.setVgap(30);
- gridPane.setHgap(30);
- // Setting the Grid alignment
- gridPane.setAlignment(Pos.CENTER);
- // Arranging all the nodes in the grid
- gridPane.add(ISBN, 0, 0);
- gridPane.add(bookISBNField, 1, 0);
- gridPane.add(title, 0, 1);
- gridPane.add(titleField, 1, 1);
- gridPane.add(bookCopies, 0, 2);
- gridPane.add(BookCopiesField, 1, 2);
- gridPane.add(pubisherID, 0, 3);
- gridPane.add(publisherIDField, 1, 3);
- gridPane.add(pubplicationYear, 0, 4);
- gridPane.add(publicationYearField, 1, 4);
- gridPane.add(threshold, 0, 5);
- gridPane.add(thresholdField, 1, 5);
- gridPane.add(price, 0, 6);
- gridPane.add(priceField, 1, 6);
- gridPane.add(authorID, 0, 7);
- gridPane.add(AuthorIDField, 1, 7);
- gridPane.add(authorName, 0, 8);
- gridPane.add(AuthorNameField, 1, 8);
- gridPane.add(category, 0, 9);
- gridPane.add(categoryField, 1, 9);
- gridPane.add(editBook, 0, 10);
- gridPane.add(cancel, 1, 10);
- // Styling nodes
- editBook.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
- cancel.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
- ISBN.setStyle("-fx-font: normal bold 20px 'serif' ");
- title.setStyle("-fx-font: normal bold 20px 'serif' ");
- bookCopies.setStyle("-fx-font: normal bold 20px 'serif' ");
- pubisherID.setStyle("-fx-font: normal bold 20px 'serif' ");
- pubplicationYear.setStyle("-fx-font: normal bold 20px 'serif' ");
- threshold.setStyle("-fx-font: normal bold 20px 'serif' ");
- price.setStyle("-fx-font: normal bold 20px 'serif' ");
- category.setStyle("-fx-font: normal bold 20px 'serif' ");
- authorID.setStyle("-fx-font: normal bold 20px 'serif' ");
- authorName.setStyle("-fx-font: normal bold 20px 'serif' ");
- // Creating a scene object
- Scene scene2 = new Scene(gridPane);
- // Setting title to the Stage
- setTitle("ADD BOOK ");
- // Adding scene to the stage
- setScene(scene2);
- cancel.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- current.close();
- prev.show();
- }
- });
- editBook.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- String ISBN = bookISBNField.getText();
- String title = titleField.getText();
- String BookCopies = BookCopiesField.getText();
- String publisherID = publisherIDField.getText();
- //String publicationYear = publicationYearField.getText();
- String threshold = thresholdField.getText();
- String price = priceField.getText();
- String category = (String) categoryField.getValue();
- String authorID = AuthorIDField.getText();
- String authorName = AuthorNameField.getText();
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- Connection conn = null;
- try {
- conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Online_Book_Store?useSSL=false",
- "root", "ahmed666");
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- String query;
- String selectQuery;
- String selectQuery2 ;
- query = "update BOOK_STORE set ISBN=? , TITLE=? , BOOK_COPIES=? , PUBLISHER_ID=? , PUBLICATION_YEAR=? , THRESHOLD=? , PRICE=? , CATEGORY=? ,AUTHOR_ID =? , AUTHOR_Name =? where ISBN=? ";
- selectQuery = "select * from BOOK_STORE where ISBN = ?";
- PreparedStatement selectStmt = conn.prepareStatement(selectQuery);
- selectStmt.setString(1, ISBN);
- ResultSet res = selectStmt.executeQuery();
- selectQuery2 = "select * from PUBLISHER where ID = ?";
- PreparedStatement selectStmt2 = conn.prepareStatement(selectQuery2);
- selectStmt2.setString(1, publisherID);
- ResultSet res2 = selectStmt2.executeQuery();
- PreparedStatement stmt = conn.prepareStatement(query);
- stmt.setString(1, ISBN);
- stmt.setString(2, title);
- stmt.setString(3, BookCopies);
- stmt.setString(4, publisherID);
- stmt.setObject(5, publicationYearField.getValue());
- stmt.setString(6, threshold);
- stmt.setString(7, price);
- stmt.setString(8, category);
- stmt.setString(9, authorID);
- stmt.setString(10, authorName);
- stmt.setString(11, isbn);
- if (price.isEmpty()) {
- Alert alert = new Alert(AlertType.WARNING, "please enter book price", ButtonType.OK);
- alert.showAndWait();
- } else if (threshold.isEmpty()) {
- Alert alert = new Alert(AlertType.WARNING, "please enter book threshold", ButtonType.OK);
- alert.showAndWait();
- } else if (!res2.first()) {
- Alert alert = new Alert(AlertType.WARNING, "please enter this publisher first", ButtonType.OK);
- alert.showAndWait();
- }
- else if(ISBN.equals(isbn)) {
- stmt.executeUpdate();
- current.close();
- if(type.equals("ISBN")) {
- SearchForManager search = new SearchForManager(ISBN , type , pPrev , id);
- search.show();
- }
- else if(type == "TITLE") {
- SearchForManager search = new SearchForManager(title , type , pPrev , id);
- search.show();
- }
- else {
- SearchForManager search = new SearchForManager(category , type , pPrev , id);
- search.show();
- }
- }
- else if (res.first() ) {
- Alert alert = new Alert(AlertType.WARNING, "dublicate ISBN", ButtonType.OK);
- alert.showAndWait();
- }
- else {
- stmt.executeUpdate();
- current.close();
- if(type.equals("ISBN")) {
- SearchForManager search = new SearchForManager(ISBN , type , pPrev , id);
- search.show();
- }
- else if(type == "TITLE") {
- SearchForManager search = new SearchForManager(title , type , pPrev , id);
- search.show();
- }
- else {
- SearchForManager search = new SearchForManager(category , type , pPrev , id);
- search.show();
- }
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- conn.close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement