This Javafx Mysql crud will teach you how to do basic database functions that are CREATE RETIEVE UPDATE and DELETE. using Mysql Database. The INSERT, SELECT, UPDATE and DELETE statements can be used in any database system, because this is support by all relational database systems.
The tutorial you learn javafx mysql crud operation step by step. Crud operations are must when you developing a javafx mini projects. learn how to INSERT, SELECT, UPDATE and DELETE in database by writing code to manage the registation table in the database named studcruds. records table consist of following columns name,mobile,course.
we will do the java best practice for understand java programming clearly and easily.
Install Scence Builder in your computer follow this link to download the scene builder which will offers all visual layout components which will easy to drag and drop and make the attractive look. After installed the Scence Builder in your computer successfully.open the netbeans and select new project ->JavaFx- you can select here JavaFx FXML Application and click next it will automatically configure the scene builder path. i attached the
screen shot image below.
Double click and open the FXMLDocument.fxml your Scence Builder will be loaded and open. then your make your design as you like.
after done the design you must name textfields and tables and tables coloums. after that copy code. go to view->Sample Skeleton
Copy the code
And paste into FXMLDocumentController inside the class. remove the default design and keep as it is a initialize method. this code which will manage the Add Records and View Records from the database.
package javafxcrudd; import java.net.URL; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Label; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.TextField; import java.sql.*; import java.util.logging.Level; import java.util.logging.Logger; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.control.Alert; import javafx.scene.control.TableRow; /** * * @author kobinath */public class FXMLDocumentController implements Initializable { @FXML private Label label; @FXML private TextField txtName; @FXML private TextField txtMobile; @FXML private TextField txtCourse; @FXML private TableView<Student> table; @FXML private TableColumn<Student, String> IDColmn; @FXML private TableColumn<Student, String> NameColmn; @FXML private TableColumn<Student, String> MobileColmn; @FXML private TableColumn<Student, String> CourseColmn; @FXML private Button btnAdd; @FXML private Button btnUpdate; @FXML private Button btnDelete; @FXML void Add(ActionEvent event) { String stname,mobile,course; stname = txtName.getText(); mobile = txtMobile.getText(); course = txtCourse.getText(); try { pst = con.prepareStatement("insert into registation(name,mobile,course)values(?,?,?)"); pst.setString(1, stname); pst.setString(2, mobile); pst.setString(3, course); pst.executeUpdate(); Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Student Registation"); alert.setHeaderText("Student Registation"); alert.setContentText("Record Addedddd!"); alert.showAndWait(); table(); txtName.setText(""); txtMobile.setText(""); txtCourse.setText(""); txtName.requestFocus(); } catch (SQLException ex) { Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex); } } public void table() { Connect(); ObservableList<Student> students = FXCollections.observableArrayList(); try { pst = con.prepareStatement("select id,name,mobile,course from registation"); ResultSet rs = pst.executeQuery(); { while (rs.next()) { Student st = new Student(); st.setId(rs.getString("id")); st.setName(rs.getString("name")); st.setMobile(rs.getString("mobile")); st.setCourse(rs.getString("course")); students.add(st); } } table.setItems(students); IDColmn.setCellValueFactory(f -> f.getValue().idProperty()); NameColmn.setCellValueFactory(f -> f.getValue().nameProperty()); MobileColmn.setCellValueFactory(f -> f.getValue().mobileProperty()); CourseColmn.setCellValueFactory(f -> f.getValue().courseProperty()); } catch (SQLException ex) { Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex); } table.setRowFactory( tv -> { TableRow<Student> myRow = new TableRow<>(); myRow.setOnMouseClicked (event -> { if (event.getClickCount() == 1 && (!myRow.isEmpty())) { myIndex = table.getSelectionModel().getSelectedIndex(); id = Integer.parseInt(String.valueOf(table.getItems().get(myIndex).getId())); txtName.setText(table.getItems().get(myIndex).getName()); txtMobile.setText(table.getItems().get(myIndex).getMobile()); txtCourse.setText(table.getItems().get(myIndex).getCourse()); } }); return myRow; }); } @FXML void Delete(ActionEvent event) { myIndex = table.getSelectionModel().getSelectedIndex(); id = Integer.parseInt(String.valueOf(table.getItems().get(myIndex).getId())); try { pst = con.prepareStatement("delete from registation where id = ? "); pst.setInt(1, id); pst.executeUpdate(); Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Student Registationn"); alert.setHeaderText("Student Registation"); alert.setContentText("Deletedd!"); alert.showAndWait(); table(); } catch (SQLException ex) { Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex); } } @FXML void Update(ActionEvent event) { String stname,mobile,course; myIndex = table.getSelectionModel().getSelectedIndex(); id = Integer.parseInt(String.valueOf(table.getItems().get(myIndex).getId())); stname = txtName.getText(); mobile = txtMobile.getText(); course = txtCourse.getText(); try { pst = con.prepareStatement("update registation set name = ?,mobile = ? ,course = ? where id = ? "); pst.setString(1, stname); pst.setString(2, mobile); pst.setString(3, course); pst.setInt(4, id); pst.executeUpdate(); Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Student Registationn"); alert.setHeaderText("Student Registation"); alert.setContentText("Updateddd!"); alert.showAndWait(); table(); } catch (SQLException ex) { Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex); } } Connection con; PreparedStatement pst; int myIndex; int id; public void Connect() { try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/studcruds","root",""); } catch (ClassNotFoundException ex) { } catch (SQLException ex) { ex.printStackTrace(); } } @Override public void initialize(URL url, ResourceBundle rb) { Connect(); table(); } }
package javafxcrudd; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; /** * * @author kobinath */public class Student { private final StringProperty id; private final StringProperty name; private final StringProperty mobile; private final StringProperty course; public Student() { id = new SimpleStringProperty(this, "id"); name = new SimpleStringProperty(this, "name"); mobile = new SimpleStringProperty(this, "mobile"); course = new SimpleStringProperty(this, "course"); } public StringProperty idProperty() { return id; } public String getId() { return id.get(); } public void setId(String newId) { id.set(newId); } public StringProperty nameProperty() { return name; } public String getName() { return name.get(); } public void setName(String newName) { name.set(newName); } public StringProperty mobileProperty() { return mobile; } public String getMobile() { return mobile.get(); } public void setMobile(String newMobile) { mobile.set(newMobile); } public StringProperty courseProperty() { return course; } public String getCourse() { return course.get(); } public void setCourse(String newCourse) { course.set(newCourse); } }
Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…
Act as a Java developer to create a program that calculates the gross wage for…
Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…
In this tutorial, we will teach you how to create a simple school management system…
I have design the Admin Basic templete using React MUI Design Admin Dashboard and Login.Here…
In this tutorial ,i am to going teach the Laravel Breeze.Laravel Breeze provides a simple…