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.
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 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(); } } |
i have attached the video link below. which will do this tutorials step by step.