I need to insert data in mysql
into a customer table, using a graphical interface with JFX
.
The problem is that you can not access to capture the data of this form in my connection class.
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXComboBox?>
<?import com.jfoenix.controls.JFXDatePicker?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.text.Font?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="entrevinos_login.FormUserSignUpController">
<children>
<AnchorPane id="AnchorPane" fx:id="btnregistrar" layoutX="10.0" layoutY="9.0" prefHeight="523.0" prefWidth="910.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Pane layoutX="-12.0" layoutY="-10.0" prefHeight="524.0" prefWidth="132.0" style="-fx-background-color: #311019;" AnchorPane.leftAnchor="-12.0" />
<Label layoutX="195.0" layoutY="35.0" text="Registro de Usuarios" textFill="#ab1c44">
<font>
<Font name="System Bold" size="25.0" />
</font>
</Label>
<JFXTextField fx:id="CédulaInput" focusColor="#ab1c44" labelFloat="true" layoutX="195.0" layoutY="110.0" prefHeight="29.0" prefWidth="247.0" promptText="Cédula de Identidad" />
<JFXTextField fx:id="EmailInput" focusColor="#ab1c44" labelFloat="true" layoutX="533.0" layoutY="110.0" prefHeight="29.0" prefWidth="270.0" promptText="Email" />
<JFXTextField fx:id="NombreInput" focusColor="#ab1c44" labelFloat="true" layoutX="195.0" layoutY="173.0" prefHeight="29.0" prefWidth="247.0" promptText="Nombre" />
<JFXTextField fx:id="Apellido" focusColor="#ab1c44" labelFloat="true" layoutX="533.0" layoutY="173.0" prefHeight="29.0" prefWidth="270.0" promptText="Primer Apellido" />
<JFXDatePicker fx:id="FechaInput" defaultColor="#ab1c44" layoutX="195.0" layoutY="243.0" overLay="true" prefHeight="29.0" prefWidth="247.0" promptText="Fecha de Nacimiento" />
<JFXTextField fx:id="TeléfonoInput" focusColor="#ab1c44" labelFloat="true" layoutX="533.0" layoutY="243.0" prefHeight="29.0" prefWidth="270.0" promptText="Teléfono" />
<JFXComboBox fx:id="PaisInput" focusColor="#ab1c44" labelFloat="true" layoutX="195.0" layoutY="316.0" prefHeight="31.0" prefWidth="247.0" promptText="País" />
<JFXComboBox fx:id="ProvinciaInput" focusColor="#ab1c44" labelFloat="true" layoutX="533.0" layoutY="316.0" prefHeight="31.0" prefWidth="132.0" promptText="Provincia" />
<JFXComboBox fx:id="CantónInput" focusColor="#ab1c44" labelFloat="true" layoutX="672.0" layoutY="316.0" prefHeight="31.0" prefWidth="132.0" promptText="Cantón" />
<JFXButton layoutX="208.0" layoutY="413.0" text="Borrar todo" />
<JFXButton layoutX="640.0" layoutY="413.0" prefHeight="31.0" prefWidth="164.0" style="-fx-background-color: #ab1c44;" text="Continuar" textFill="WHITE" />
<Separator layoutX="187.0" layoutY="78.0" prefHeight="12.0" prefWidth="625.0" />
<Label layoutX="868.0" layoutY="-4.0" text="X" textFill="#ab1c44">
<font>
<Font name="System Bold" size="25.0" />
</font>
</Label>
<ImageView fitHeight="36.0" fitWidth="43.0" layoutX="136.0" layoutY="35.0" onMouseClicked="#GoBackToLogin" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@images/icons8_Back_To_100px.png" />
</image>
</ImageView>
</children>
</AnchorPane>
</children>
</AnchorPane>
Database access:
package entrevinos_login;
import database.Database;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
public class FormUserSignUpController implements Initializable {
Connection con;
@Override
public void initialize(URL url, ResourceBundle rb) {
try {
createConnection();
} catch (SQLException ex) {
Logger.getLogger(FormUserSignUpController.class.getName()).log(Level.SEVERE, null, ex);
}
}
void createConnection() throws SQLException {
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/entrevinos","root","mysql");
}catch (ClassNotFoundException | SQLException ex){
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
}
@FXML
private void btnContinuar(ActionEvent event) throws IOException {
try{
String name = .getText();
Statement stmt = con.createStatement();
String dbop = "INSERT INTO CLIENTES VALUES('"+ nombre +"')";
stmt.execute(dbop);
}catch(SQLException ex){
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
}
}
@FXML
private void GoBackToLogin(MouseEvent event) throws IOException {
System.out.println("x");
Parent home_page_parent = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene home_page_scene = new Scene (home_page_parent);
Stage app_stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
app_stage.hide();
app_stage.setScene(home_page_scene);
app_stage.show();
}
}