Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package application;
- import javafx.scene.Scene;
- import javafx.stage.Stage;
- import javafx.scene.text.Font;
- import javafx.scene.text.Text;
- import javafx.scene.control.Button;
- import javafx.scene.control.TextField;
- import javafx.scene.Group;
- import javafx.scene.Node;
- import javafx.scene.layout.GridPane;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.geometry.Pos;
- import com.mysql.jdbc.PreparedStatement;
- public class HomePage extends Stage {
- public HomePage(String userName) {
- Stage current = this;
- Connection con;
- // Text Area for search label
- TextField search = new TextField();
- search.setMinSize(100, 50);
- //search button
- Button buttonFour = new Button("Search by name");
- buttonFour.setMinSize(100, 50);
- //search button
- Button buttonFive = new Button("Search by ISBN");
- buttonFive.setMinSize(100, 50);
- //personal info button
- Button buttonOne = new Button("Edit personal info");
- buttonOne.setMinSize(100, 50);
- //Shopping cart button
- Button buttonTwo = new Button("Shopping cart");
- buttonTwo.setMinSize(100, 50);
- //Shopping cart button
- Button buttonThree = new Button("Logout");
- buttonThree.setMinSize(100, 50);
- //Styling nodes
- buttonOne.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
- buttonTwo.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
- buttonThree.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
- buttonFour.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
- buttonFive.setStyle("-fx-background-color: darkslateblue; -fx-text-fill: white;");
- //Creating a Grid Pane
- GridPane gridPane = new GridPane();
- //Setting size for the pane
- gridPane.setMinSize(600, 400);
- //Setting the padding
- 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(buttonFive, 0, 0);
- gridPane.add(search,1,0);
- gridPane.add(buttonFour, 2, 0);
- gridPane.add(buttonOne, 1, 1);
- gridPane.add(buttonTwo, 1, 2);
- gridPane.add(buttonThree, 1, 3);
- buttonFive.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- String ISBN = search.getText();
- ResultSet rs = null;
- // Empty input
- if(ISBN.isEmpty()) {
- System.out.println("Enter an ISBN");
- }
- else {
- System.out.println(ISBN);
- // check ISBN is an integer value or not
- try {
- int i = Integer.parseInt(ISBN.trim());
- // connect to db and get resultSet
- try {
- Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/Online_Book_Store?useSSL=false", "root", "ahmed666" );
- PreparedStatement p = (PreparedStatement) con.prepareStatement("select * from BOOK_STORE where ISBN="+ISBN);
- rs = p.executeQuery();
- }
- catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // send results to the new page
- SearchResult page = new SearchResult(rs,current);
- current.close();
- page.show();
- }
- catch(NumberFormatException nfe)
- {
- System.out.println("Invalid ISBN");
- }
- }
- }
- });
- buttonFour.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- String title = search.getText();
- ResultSet rs = null;
- // Empty input
- if(title.isEmpty()) {
- System.out.println("Enter a Title");
- }
- else {
- System.out.println(title);
- // connect to db and get resultSet
- try {
- Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/Online_Book_Store?useSSL=false", "root", "ahmed666" );
- PreparedStatement p = (PreparedStatement) con.prepareStatement("select * from BOOK_STORE where TITLE='"+title+"'");
- rs = p.executeQuery();
- }
- catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- // send results to the new page
- SearchResult page = new SearchResult(rs , current);
- current.close();
- page.show();
- }
- }
- });
- Scene scene = new Scene(gridPane);
- scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
- setScene(scene);
- setTitle(userName);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement