Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Main.java
- import java.io.IOException;
- import javafx.application.Application;
- import javafx.fxml.FXMLLoader;
- import javafx.stage.Stage;
- import javafx.scene.Scene;
- import javafx.scene.layout.AnchorPane;
- import javafx.scene.layout.BorderPane;
- public class Main extends Application {
- private static Stage primaryStage;
- @Override
- public void start(Stage primaryStage) {
- Main.primaryStage = primaryStage;
- try {
- showMainScene();
- } catch (IOException e) {
- System.out.println("Error: " + e.getMessage());
- e.printStackTrace();
- }
- }
- public static void showMainScene() throws IOException{
- FXMLLoader fxmlLoader = new FXMLLoader();
- fxmlLoader.setLocation(Main.class.getResource("/com/example/views/CustomerView.fxml"));
- AnchorPane layout = fxmlLoader.load();
- Scene scene = new Scene(layout);
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- public static void showListOfCustomers() throws IOException{
- FXMLLoader fxmlLoader = new FXMLLoader();
- fxmlLoader.setLocation(Main.class.getResource("/com/example/views/ListOfCustomersView.fxml"));
- BorderPane layout = fxmlLoader.load();
- Scene scene = new Scene(layout);
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
- ===============================================================================================================
- CustomerModel.java
- public class CustomerModel {
- private Long id;
- private String firstName;
- private String lastName;
- private String phoneNumber;
- public CustomerModel(){
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public String getPhoneNumber() {
- return phoneNumber;
- }
- public void setPhoneNumber(String phoneNumber) {
- this.phoneNumber = phoneNumber;
- }
- }
- ===============================================================================================================
- CustomerController.java
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- import javax.sql.rowset.JdbcRowSet;
- import javax.sql.rowset.RowSetProvider;
- import com.example.models.CustomerModel;
- public class CustomerController {
- private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/javafx";
- private static final String DATABASE_USERNAME = "root";
- private static final String DATABASE_PASSWORD = "password";
- private JdbcRowSet jdbcRowSet = null;
- private final String ID_COLUMN = "id";
- private final String FIRST_NAME_COLUMN = "first_name";
- private final String LAST_NAME_COLUMN = "last_name";
- private final String PHONE_NUMBER_COLUMN = "phone_number";
- public CustomerController(){
- try{
- Class.forName(JDBC_DRIVER);
- jdbcRowSet = RowSetProvider.newFactory().createJdbcRowSet();
- jdbcRowSet.setUrl(DATABASE_URL);
- jdbcRowSet.setUsername(DATABASE_USERNAME);
- jdbcRowSet.setPassword(DATABASE_PASSWORD);
- jdbcRowSet.setCommand("SELECT * FROM customers");
- jdbcRowSet.execute();
- } catch (Exception e){
- System.out.println("Error: " + e.getMessage());
- }
- }
- public CustomerModel addCustomerToDatabase(CustomerModel customerModel){
- try{
- jdbcRowSet.moveToInsertRow();
- jdbcRowSet.updateString(FIRST_NAME_COLUMN, customerModel.getFirstName());
- jdbcRowSet.updateString(LAST_NAME_COLUMN, customerModel.getLastName());
- jdbcRowSet.updateString(PHONE_NUMBER_COLUMN, customerModel.getPhoneNumber());
- jdbcRowSet.insertRow();
- jdbcRowSet.moveToCurrentRow();
- } catch (SQLException e){
- System.out.println("Error: " + e.getMessage());
- try{
- jdbcRowSet.rollback();
- customerModel = null;
- } catch(Exception ex){}
- } catch (Exception exception) {
- System.out.println("Error: " + exception.getMessage());
- }
- return customerModel;
- }
- public List<CustomerModel> getAllCustomers() throws SQLException{
- List<CustomerModel> listOfCustomers = new ArrayList<>();
- jdbcRowSet.beforeFirst();
- while(jdbcRowSet.next()){
- CustomerModel customerModel = new CustomerModel();
- Long id = jdbcRowSet.getLong(ID_COLUMN);
- String firstName = jdbcRowSet.getString(FIRST_NAME_COLUMN);
- String lastName = jdbcRowSet.getString(LAST_NAME_COLUMN);
- String phoneNumber = jdbcRowSet.getString(PHONE_NUMBER_COLUMN);
- customerModel.setId(id);
- customerModel.setFirstName(firstName);
- customerModel.setLastName(lastName);
- customerModel.setPhoneNumber(phoneNumber);
- listOfCustomers.add(customerModel);
- }
- return listOfCustomers;
- }
- }
- ===============================================================================================================
- CustomerViewController.java
- import java.io.IOException;
- import java.net.URL;
- import java.util.ResourceBundle;
- import com.example.controllers.CustomerController;
- import com.example.models.CustomerModel;
- import application.Main;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.control.Button;
- import javafx.scene.control.TextField;
- public class CustomerViewController implements Initializable{
- @FXML private TextField tfId;
- @FXML private TextField tfFirstName;
- @FXML private TextField tfLastName;
- @FXML private TextField tfPhoneNumber;
- @FXML private Button btnAddCustomerToDatabase;
- @FXML private Button btnShowList;
- private CustomerController customerController;
- public CustomerViewController(){
- }
- @Override
- public void initialize(URL location, ResourceBundle resources) {
- customerController = new CustomerController();
- assert btnAddCustomerToDatabase != null : "fx:id=\"btnAddCustomerToDatabase\" was not injected.";
- assert btnShowList != null : "Felmeddelande vid null";
- btnAddCustomerToDatabase.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- addCustomerToDatabase();
- }
- });
- btnShowList.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- switchSceneToList();
- }
- });
- }
- public void addCustomerToDatabase(){
- CustomerModel customer = getTextFieldData();
- customerController.addCustomerToDatabase(customer);
- System.out.println("Customer added! Hooray!");
- }
- public CustomerModel getTextFieldData(){
- CustomerModel customer = new CustomerModel();
- customer.setFirstName(tfFirstName.getText());
- customer.setLastName(tfLastName.getText());
- customer.setPhoneNumber(tfPhoneNumber.getText());
- return customer;
- }
- public void switchSceneToList(){
- try {
- Main.showListOfCustomers();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- ===============================================================================================================
- ListOfCustomersViewController.java
- import java.net.URL;
- import java.sql.SQLException;
- import java.util.List;
- import java.util.ResourceBundle;
- import com.example.controllers.CustomerController;
- import com.example.models.CustomerModel;
- import javafx.collections.FXCollections;
- import javafx.collections.ObservableList;
- import javafx.fxml.FXML;
- import javafx.fxml.Initializable;
- import javafx.scene.control.ListView;
- public class ListOfCustomersViewController implements Initializable{
- @FXML private ListView<String> lvListOfCustomers;
- private CustomerController customerController;
- public ListOfCustomersViewController() {
- }
- @Override
- public void initialize(URL location, ResourceBundle resources) {
- customerController = new CustomerController();
- ObservableList<String> observableList = FXCollections.observableArrayList();
- List<CustomerModel> localListOfCustomers;
- try {
- localListOfCustomers = customerController.getAllCustomers();
- for(CustomerModel customer : localListOfCustomers){
- observableList.add("ID: " + customer.getId() + "\n " + customer.getFirstName() + " " + customer.getLastName());
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- lvListOfCustomers.setItems(observableList);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement