Main.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package duycode; import ...; public class Main extends Application { public static void main(String[] args) { launch(args); } public static Stage mainStage; @Override public void start(Stage primaryStage) { mainStage = primaryStage; try{ Parent root = FXMLLoader.load(getClass().getResource("listperson.fxml")); Scene scene = new Scene(root,400,600); primaryStage.setScene(scene); primaryStage.setTitle("List Person"); primaryStage.show(); } catch (Exception e){ System.out.println(e.getMessage()); } } } |
ConnectMySQL.class
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package duycode; import ...; public class ConnectMySQL { public static Connection ConnectMySQL() throws Exception{ Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/duycode?useUnicode=true&characterEncoding=utf-8"; String username = "root"; String password = ""; return DriverManager.getConnection(url,username,password); } } |
listperson.fxml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.text.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <Pane fx:controller="duycode.ListStudents" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="613.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"> <children> <Text layoutX="210.0" layoutY="57.0" strokeType="OUTSIDE" strokeWidth="0.0" text="List Students"> <font> <Font size="32.0" /> </font> </Text> <TextField fx:id="searchName" layoutX="315.0" layoutY="100.0" onAction="#searchname" prefHeight="31.0" prefWidth="265.0" promptText="Search name" /> <TableView fx:id="tbView" layoutX="20.0" layoutY="143.0" prefHeight="374.0" prefWidth="560.0"> <columns> <TableColumn fx:id="colID" prefWidth="67.0" text="ID" /> <TableColumn fx:id="colName" prefWidth="228.0" text="Name" /> <TableColumn fx:id="colAge" prefWidth="118.0" text="Age" /> <TableColumn fx:id="colMark" prefWidth="145.0" text="Mark" /> </columns> </TableView> <Button onAction="#addStudent" layoutX="400.0" layoutY="541.0" mnemonicParsing="false" prefHeight="31.0" prefWidth="180.0" text="Add Student" /> <Button onAction="#deleteStudent" layoutX="20.0" layoutY="541.0" mnemonicParsing="false" text="Delete" /> </children> </Pane> |
ListStudent.class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
package duycode; import ...; public class ListStudents implements Initializable { public TableView<Student> tbView = new TableView<>(); public TableColumn<Student,Integer> colID = new TableColumn<>(); public TableColumn<Student,String> colName = new TableColumn<>(); public TableColumn<Student,Integer> colAge = new TableColumn<>(); public TableColumn<Student,Double> colMark = new TableColumn<>(); public TextField searchName = new TextField(); @Override public void initialize(URL location, ResourceBundle resources) { colID.setCellValueFactory(new PropertyValueFactory<>("id")); colName.setCellValueFactory(new PropertyValueFactory<>("name")); colAge.setCellValueFactory(new PropertyValueFactory<>("age")); colMark.setCellValueFactory(new PropertyValueFactory<>("mark")); String queryData = "SELECT * FROM liststudent"; selectquery(queryData); } public void selectquery(String queryData){ ObservableList<Student> list1 = FXCollections.observableArrayList(); try{ Connection conn = ConnectMySQL.ConnectMySQL(); Statement stm = conn.createStatement(); ResultSet resultData = stm.executeQuery(queryData); while (resultData.next()){ list1.add(new Student( resultData.getInt("id"), resultData.getString("student_name"), resultData.getInt("age"), resultData.getDouble("mark")) ); } conn.close(); } catch (Exception e){ System.out.println(e.getMessage()); } tbView.setItems(list1); } public void searchname(){ String txtSearch = searchName.getText(); String queryData = "SELECT * FROM liststudent WHERE student_name LIKE '%"+txtSearch+"%'"; selectquery(queryData); } public void deleteStudent(){ int deleteID = tbView.getSelectionModel().getSelectedItems().get(0).getId(); String deleteSelect = "DELETE FROM liststudent WHERE id="+deleteID; confirmdelete(deleteSelect); } public void confirmdelete(String deleteSelect){ Pane confirm = new Pane(); confirm.setPadding(new Insets(20,200,0,10)); Text textconfirm = new Text(150,50,"Are you sure ?"); Font fontcf = new Font("Arial",20); textconfirm.setFont(fontcf); Button button1 = new Button("Yes"); button1.setLayoutX(75); button1.setLayoutY(100); button1.setPrefSize(100,25); Button button2 = new Button("No"); button2.setLayoutX(225); button2.setLayoutY(100); button2.setPrefSize(100,25); confirm.getChildren().addAll(textconfirm,button1,button2); Scene scenecofirm = new Scene(confirm, 400,200); Stage stageconfirm = new Stage(); stageconfirm.setTitle("Yes or No"); stageconfirm.setScene(scenecofirm); stageconfirm.show(); button1.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { try{ Connection conn = ConnectMySQL.ConnectMySQL(); Statement stm = conn.createStatement(); stm.executeUpdate(deleteSelect); conn.close(); } catch (Exception e){ System.out.println(e.getMessage()); } String queryData = "SELECT * FROM liststudent"; selectquery(queryData); stageconfirm.close(); } }); button2.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { stageconfirm.close(); } }); } public void addStudent(){ try{ Parent addStudent = FXMLLoader.load(getClass().getResource("addstudent.fxml")); Main.mainStage.getScene().setRoot(addStudent); Main.mainStage.setTitle("Add student"); } catch (Exception e){ System.out.println(e.getMessage()); } } } |