This JDBC will teach you how to do basic database functions that are CREATE RETIEVE in Netbeans. using Mysql Database. The INSERT, SELECT statements can be used in any database system, because this is support by all relational database systems.
We will learn how to INSERT, SELECT in database by writing code to manage the addressbook table in the database named records. records table consist of following columns name,address,age.
First Step
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.
After created the project you will get the project structure look like 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 javanewcrud; import java.net.URL; import java.sql.*; import java.util.ResourceBundle; import java.util.logging.Level; import java.util.logging.Logger; import javafx.collections.FXCollections; import javafx.collections.ObservableList; 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.Alert; import javafx.scene.control.Label; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.TextField; public class FXMLDocumentController implements Initializable { @FXML private Label label; @FXML private TextField txtMname; @FXML private TextField txtAddress; @FXML private TextField txtAge; @FXML private TableView<Record> table; @FXML private TableColumn<Record, String> idColoum; @FXML private TableColumn<Record, String> MnameColoum; @FXML private TableColumn<Record, String> AddressColoum; @FXML private TableColumn<Record, String> AgeColoum; Connection con; PreparedStatement pst; public void Connect() { try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/addressbook","root",""); } catch (ClassNotFoundException ex) { } catch (SQLException ex) { } } @FXML void Add(ActionEvent event) { Connect(); String name = txtMname.getText(); String address = txtAddress.getText(); String age = txtAge.getText(); try { pst = con.prepareStatement("insert into records(name,address,age)values(?,?,?)"); pst.setString(1, name); pst.setString(2, address); pst.setString(3, age); int status = pst.executeUpdate(); if(status==1) { Alert alert = new Alert(Alert.AlertType.INFORMATION); alert.setTitle("Success"); alert.setHeaderText("Member"); alert.setContentText("Record Addedd Successfully"); alert.showAndWait(); table(); txtMname.setText(""); txtAddress.setText(""); txtAge.setText(""); txtMname.requestFocus(); } else { Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("Fail"); alert.setHeaderText("Member"); alert.setContentText("Record Addedd Failded"); alert.showAndWait(); } } catch (SQLException ex) { Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex); } } public void table() { ObservableList<Record> records = FXCollections.observableArrayList(); try { pst = con.prepareStatement("select id,name,address,age from records"); ResultSet rs = pst.executeQuery(); { while (rs.next()) { Record record = new Record(); record.setId(rs.getString("id")); record.setName(rs.getString("name")); record.setAddress(rs.getString("address")); record.setAge(rs.getString("age")); records.add(record); } } table.setItems(records); idColoum.setCellValueFactory(f -> f.getValue().idProperty()); MnameColoum.setCellValueFactory(f -> f.getValue().nameProperty()); AddressColoum.setCellValueFactory(f -> f.getValue().addressProperty()); AgeColoum.setCellValueFactory(f -> f.getValue().ageProperty()); } catch (SQLException ex) { Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex); } } @Override public void initialize(URL url, ResourceBundle rb) { Connect(); table(); } }
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…