Answer the question
In order to leave comments, you need to log in
The user is not added to the database (Java, Mysql)?
I'm learning java. There was a problem, the user is not added to the database. There are no errors in the console.
Lesson
code:
DatebassHandler.java file
package sample;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DatebassHandler extends Configs{
Connection dbConnection;
public Connection getDbConnection() throws ClassNotFoundException, SQLException {
String connectionString = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName +
"?verifyServerCertificate=false"+
"&useSSL=false"+
"&requireSSL=false"+
"&useLegacyDatetimeCode=false"+
"&"+
"&serverTimezone=UTC";
Class.forName("com.mysql.cj.jdbc.Driver");
dbConnection=DriverManager.getConnection(connectionString,dbUser,dbPass);
return dbConnection;
}
public void signUpUser(User user){
String insert = "INSERT INTO " + Const.USER_TABEL+"("+ Const.USER_LOGIN + ","+ Const.USER_CITY +","+ Const.USER_PASSWORD+ ","+
Const.USER_GENDER+")"+ "VALUES(?,?,?,?)";
try {
PreparedStatement prSt = getDbConnection().prepareStatement(insert);
prSt.setString(1, user.getLogin());
prSt.setString(2, user.getCity());
prSt.setString(3, user.getPassword());
prSt.setString(4, user.getGender());
prSt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
package sample;
public class User {
private String login;
private String city;
private String password;
private String gender;
public User(String login, String city, String password, String gender) {
this.login = login;
this.city = city;
this.password = password;
this.gender = gender;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
public class SignUpController {
@FXML
private TextField login_field;
@FXML
private PasswordField password_field;
@FXML
private Button signUpButton;
@FXML
private TextField signUpCity;
@FXML
private CheckBox signUpCheckBoxMale;
@FXML
private CheckBox signUpCheckBoxFamale;
@FXML
void initialize(){
signUpButton.setOnAction(event->{
signUpNewuser();
});
}
private void signUpNewuser() {
DatebassHandler dbHandler = new DatebassHandler();
String login = login_field.getText();
String city = signUpCity.getText();
String password = password_field.getText();
String gender = "";
if(signUpCheckBoxMale.isSelected())
gender = "Male";
else
gender = "Female";
User user = new User(login,city,password,gender);
dbHandler.signUpUser(user);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question