Structura
Clasa UserNouDaoImpl.java
=============================================public class UserNouDaoImpl implements UserNouDaoIntf{private static MyDataSource ds=MyDataSource.getInstance();private static final Logger LOG = Logger.getLogger(UserNouDaoImpl.class.getName());@Overridepublic void save(UserNou uN) throws SQLException {String sql = "INSERT INTO user VALUES( null, '" + uN.getNume_prenume() + "' , '" + uN.getLogin() + "' ,'"+ uN.getParola() + "' , '" + uN.getEmail()+ "' , '" + uN.getDataNasteri()+ "' , '" + uN.getComboboxGen()+ "', null );";Connection conn = null;Statement stat = null;try {conn = ds.getConnection();stat = conn.createStatement();stat.executeUpdate(sql);LOG.info("Adaugat cu cucces User-ul " + uN);} catch (Exception e) {LOG.severe(e.toString());throw new SQLException(e.getMessage());} finally {if (stat != null) {try {stat.close();} catch (SQLException ex) {Logger.getLogger(UserNouDaoImpl.class.getName()).log(Level.SEVERE, null, ex);}}if (conn != null) {try {conn.close();} catch (SQLException ex) {Logger.getLogger(UserNouDaoImpl.class.getName()).log(Level.SEVERE, null, ex);}}}}}
Interfata: UserNouDaoIntf.java
public interface UserNouDaoIntf {
void save(UserNou uN) throws SQLException;
}
==============================================
FXML: AccesUder.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #32989e;" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.iucosoft.bibliotecanoastra.gui.AccesUserController">
<children>
<GridPane layoutX="200.0" layoutY="68.0" prefHeight="332.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="68.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" spacing="10.0" style="-fx-background-color: #cbecfd; -fx-border-color: #2a2ae5;">
<children>
<Label text="Inregistrare" textFill="#900a0a">
<font>
<Font name="Ubuntu Bold" size="17.0" />
</font>
<VBox.margin>
<Insets bottom="10.0" left="10.0" right="15.0" top="10.0" />
</VBox.margin>
</Label>
<HBox alignment="CENTER" spacing="3.0">
<children>
<TextField fx:id="tfNumePrenume" maxWidth="150.0" prefHeight="25.0" prefWidth="150.0" promptText="Nume Prenule" />
<Label fx:id="labelErrNume" text="*" textFill="RED" visible="false">
<font>
<Font name="System Bold" size="16.0" />
</font>
</Label>
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
</HBox>
<HBox alignment="CENTER" spacing="3.0">
<children>
<TextField fx:id="tfLogin" maxWidth="150.0" prefHeight="25.0" prefWidth="204.0" promptText="Login" />
<Label fx:id="labelErLog" text="*" textFill="RED" visible="false">
<font>
<Font name="System Bold" size="16.0" />
</font>
</Label>
</children>
</HBox>
<HBox alignment="CENTER" spacing="3.0">
<children>
<PasswordField fx:id="tfParola" maxWidth="150.0" promptText="Parola" />
<Label fx:id="labelErrParole" text="*" textFill="RED" visible="false">
<font>
<Font name="System Bold" size="16.0" />
</font>
</Label>
</children>
</HBox>
<HBox alignment="CENTER" spacing="3.0">
<children>
<TextField fx:id="tfEmail" maxWidth="150.0" prefHeight="25.0" prefWidth="204.0" promptText="E-mail" />
<Label fx:id="labelErrEmail" text="*" textFill="RED" visible="false">
<font>
<Font name="System Bold" size="16.0" />
</font>
</Label>
</children>
</HBox>
<HBox alignment="CENTER" spacing="3.0">
<children>
<DatePicker fx:id="dataNasteri" prefHeight="25.0" prefWidth="151.0" promptText="Data nasterii" />
<Label fx:id="labelErrData" text="*" textFill="RED" visible="false">
<font>
<Font name="System Bold" size="16.0" />
</font>
</Label>
</children>
</HBox>
<HBox alignment="CENTER" spacing="3.0">
<children>
<Label text="Gen " />
<ComboBox fx:id="comboBoxGen" prefWidth="120.0" />
<Label fx:id="labelErrGen" text="*" textFill="RED" visible="false">
<font>
<Font name="System Bold" size="16.0" />
</font>
</Label>
</children>
</HBox>
<Button mnemonicParsing="false" onAction="#handleActionCreazaCont" prefHeight="44.0" prefWidth="128.0" style="-fx-background-radius: 10;" text="Creeaza cont">
<font>
<Font name="System Bold" size="14.0" />
</font>
<VBox.margin>
<Insets top="10.0" />
</VBox.margin>
</Button>
</children>
</VBox>
<VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" spacing="10.0" style="-fx-background-color: #c4f9cf; -fx-border-color: #2a2ae5;" GridPane.columnIndex="1">
<children>
<Label text="Audentificare" textFill="#0a1a8f">
<font>
<Font name="Ubuntu Bold" size="17.0" />
</font>
<VBox.margin>
<Insets />
</VBox.margin>
</Label>
<Label fx:id="eror_login" prefHeight="17.0" prefWidth="88.0" text="introdu LOGIN" textFill="RED" visible="false">
<VBox.margin>
<Insets bottom="-10.0" />
</VBox.margin>
</Label>
<TextField fx:id="tfLoginIn" maxWidth="150.0" prefHeight="25.0" prefWidth="204.0" promptText="Login" />
<Label fx:id="eror_parola" layoutX="119.0" layoutY="126.0" prefHeight="17.0" prefWidth="101.0" text="introdu PAROLA" textFill="RED" visible="false">
<VBox.margin>
<Insets bottom="-10.0" />
</VBox.margin>
</Label>
<PasswordField fx:id="tfParolaIn" maxWidth="150.0" promptText="Parola" />
<Button fx:id="btnLogin" mnemonicParsing="false" onAction="#handleActionLogin" prefHeight="44.0" prefWidth="128.0" style="-fx-background-radius: 10;" text="Login">
<font>
<Font name="Ubuntu Bold" size="15.0" />
</font>
<VBox.margin>
<Insets top="10.0" />
</VBox.margin>
</Button>
<Label fx:id="lEroare" alignment="CENTER" contentDisplay="CENTER" prefHeight="20.0" prefWidth="271.0" textAlignment="CENTER" textFill="#fc0000">
<font>
<Font name="Times New Roman Bold Italic" size="17.0" />
</font>
</Label>
</children>
<GridPane.margin>
<Insets />
</GridPane.margin>
</VBox>
</children>
</GridPane>
<HBox alignment="CENTER" layoutX="100.0" layoutY="6.0" prefHeight="68.0" prefWidth="600.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Label text="Forma de audentificare" textFill="WHITE">
<font>
<Font name="Times New Roman Bold Italic" size="30.0" />
</font>
</Label>
</children>
</HBox>
</children>
</AnchorPane>
==================================================
Controller: AccesUserController.java
public class AccesUserController implements Initializable {
private UserNouDaoIntf userNouDao;
ObservableList<String> comboboxGen = FXCollections.observableArrayList(
"Masculin", "Femenin", "Neutru");
@FXML
private TextField tfNumePrenume;
@FXML
private TextField tfLogin;
@FXML
private PasswordField tfParola;
@FXML
private TextField tfEmail;
@FXML
private DatePicker dataNasteri;
@FXML
private ComboBox<String> comboBoxGen;
@FXML
private TextField tfLoginIn;
@FXML
private PasswordField tfParolaIn;
@FXML
private Label eror_login;
@FXML
private Label eror_parola;
@FXML
private Label lEroare;
@FXML
private Button btnLogin;
@FXML
private Label labelErrNume;
@FXML
private Label labelErLog;
@FXML
private Label labelErrParole;
@FXML
private Label labelErrEmail;
@FXML
private Label labelErrData;
@FXML
private Label labelErrGen;
private static final Logger LOG = Logger.getLogger(AccesUserController.class.getName());
@Override
public void initialize(URL url, ResourceBundle rb) {
userNouDao = new UserNouDaoImpl(); // facem legatura cu SQL
comboBoxGen.setItems(comboboxGen);// adaugam in combobox iteni de sus
}
@FXML
private void handleActionCreazaCont(ActionEvent event) {
UserNou uN = readForm();
labelErrNume.setVisible(false);
labelErLog.setVisible(false);
labelErrParole.setVisible(false);
labelErrEmail.setVisible(false);
labelErrData.setVisible(false);
labelErrGen.setVisible(false);
if (tfNumePrenume.getText().equals("")) {
labelErrNume.setVisible(true);
}
if (tfLogin.getText().equals("")) {
labelErLog.setVisible(true);
}
if (String.valueOf(tfParola.getText()).equals("")) {
labelErrParole.setVisible(true);
}
if (tfEmail.getText().equals("")) {
labelErrEmail.setVisible(true);
}
if (String.valueOf(dataNasteri.getValue()).equals("")) {
labelErrData.setVisible(true);
}
if (String.valueOf(comboBoxGen.getValue()).equals("")) {
labelErrGen.setVisible(true);
} else {
try {
userNouDao.save(uN);
arataMesaj(Alert.AlertType.INFORMATION, "Mesaj de informare! ", "Ai creeat contul cu cucces!!", "Loginul tau e: " + uN.getLogin()+ " . Acum te poti audentifica.");
clearForm();
} catch (SQLException ex) {
arataMesaj(Alert.AlertType.ERROR, "Error", "Mesaj de roare", "Inregistrarea nu sa efectuat, mai incearca ...\n" + ex.getMessage());
}
}
}
@FXML
private void handleActionLogin(ActionEvent event) throws SQLException {
eror_login.setVisible(false);
eror_parola.setVisible(false);
if (tfLoginIn.getText().equals("")) {
eror_login.setVisible(true);
}
if (String.valueOf(tfParolaIn.getText()).equals("")) {
eror_parola.setVisible(true);
} else {
Connection con = MyDataSource.getInstance().getConnection();
PreparedStatement ps;
try {
ps = con.prepareStatement("SELECT * FROM user WHERE login = ? AND parola = ?");
ps.setString(1, tfLoginIn.getText());
ps.setString(2, String.valueOf(tfParolaIn.getText()));
ResultSet rs = ps.executeQuery();
if (rs.next()) {
System.out.println("Este !!");
btnLogin.getScene().getWindow().hide();
FXMLDocumentController mV = new FXMLDocumentController();
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("FXMLDocument.fxml"));
try {
loader.load();
} catch (IOException ex) {
Logger.getLogger(FXMLDocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
Parent root = loader.getRoot();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.showAndWait();
} else {
System.out.println("Nui");
lEroare.setText("Gresit Username sau Passwords");
}
} catch (SQLException ex) {
Logger.getLogger(AccesUserController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public UserNou readForm() {
UserNou uN = new UserNou();
uN.setNume_prenume(tfNumePrenume.getText());
uN.setLogin(tfLogin.getText());
uN.setParola(tfParola.getText());
uN.setEmail(tfEmail.getText());
uN.setDataNasteri(dataNasteri.getValue());
uN.setComboboxGen((String) comboBoxGen.getValue());
return uN;
}
public void clearForm() {
tfNumePrenume.setText("");
tfLogin.setText("");
tfParola.setText("");
tfEmail.setText("");
dataNasteri.setValue(null);
comboBoxGen.setValue(null);
}
private void arataMesaj(Alert.AlertType tip, String titlu, String header, String content) {
Alert alert = new Alert(tip);
alert.setTitle(titlu);
alert.setHeaderText(header);
alert.setContentText(content);
alert.showAndWait();
}
}
=================================================
Model: UserNou.java
public class UserNou {
private String nume_prenume;
private String login;
private String parola;
private String email;
private LocalDate dataNasteri;
private String comboboxGen;
public UserNou() {
}
public UserNou(String nume_prenume, String login, String parola, String email, LocalDate dataNasteri, String comboboxGen) {
this.nume_prenume = nume_prenume;
this.login = login;
this.parola = parola;
this.email = email;
this.dataNasteri = dataNasteri;
this.comboboxGen = comboboxGen;
}
public String getNume_prenume() {
return nume_prenume;
}
public void setNume_prenume(String nume_prenume) {
this.nume_prenume = nume_prenume;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getParola() {
return parola;
}
public void setParola(String parola) {
this.parola = parola;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public LocalDate getDataNasteri() {
return dataNasteri;
}
public void setDataNasteri(LocalDate dataNasteri) {
this.dataNasteri = dataNasteri;
}
public String getComboboxGen() {
return comboboxGen;
}
public void setComboboxGen(String comboboxGen) {
this.comboboxGen = comboboxGen;
}
@Override
public int hashCode() {
int hash = 3;
hash = 29 * hash + Objects.hashCode(this.login);
hash = 29 * hash + Objects.hashCode(this.email);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final UserNou other = (UserNou) obj;
if (!Objects.equals(this.login, other.login)) {
return false;
}
if (!Objects.equals(this.email, other.email)) {
return false;
}
return true;
}
@Override
public String toString() {
return "User Nou: " + nume_prenume + ", login: " + login + ", parola: " + parola + ", email: " + email + ", dataNasteri: " + dataNasteri + ", Gen: " + comboboxGen ;
}
}
==================================================
Bonus, conectarea cu baza de date: MyDataSurce.java
public class MyDataSource {
private static final Logger LOG = Logger.getLogger(MyDataSource.class.getName());
String driverName = "com.mysql.jdbc.Driver";
String urlDB = "jdbc:mysql://localhost:3306/biblioteca";
String dbuser = "root";
String password = "12345";
Connection connection;
public MyDataSource() {
try {
loadProperties();
loadDriver();
testConnection();
} catch (ClassNotFoundException ex) {
LOG.log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
LOG.log(Level.SEVERE, null, ex);
}
}
private void loadProperties() {
LOG.info("Am citit prorietatile din fisierul de configuratie");
}
private void loadDriver() throws ClassNotFoundException {
Class.forName(driverName);
LOG.info("diver " + driverName + " incarcat cu succes!");
}
private void testConnection() throws SQLException {
connection = DriverManager.getConnection(urlDB, dbuser, password);
LOG.info("conexiunea creata cu succes!");
}
public Connection getConnection() throws SQLException {
if (connection == null || connection.isClosed()) {
connection = DriverManager.getConnection(urlDB, dbuser, password);
}
return connection;
}
public static MyDataSource getInstance() {
return MyDataSourceHolder.INSTANCE;
}
private static class MyDataSourceHolder {
private static final MyDataSource INSTANCE = new MyDataSource();
}


Niciun comentariu:
Trimiteți un comentariu