I've made a javaFx login form connected with MySQL connection works fine but when i try to login I get wrong name And Password I will provide my code and screenshot of MySQL so anyone who tries to help will not get confuse
package sample;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DataBaseProject1 extends Application {
Connection conn;
PreparedStatement pst = null;
ResultSet rs = null;
@Override
public void start(Stage primaryStage) throws Exception
{
//GUIS a = new GUIS();
//a.createConnection();
//a.display();
DataBaseProject1 d = new DataBaseProject1();
d.createConnection();
primaryStage.setTitle("Retrive Database Values Into CheckBox");
//primaryStage.getIcons().add(new Image("file:user-icon.png"));
BorderPane layout = new BorderPane();
Scene newscene = new Scene(layout, 1200, 700, Color.rgb(0, 0, 0, 0));
Group root = new Group();
Scene scene = new Scene(root, 320, 200, Color.rgb(0, 0, 0, 0));
scene.getStylesheets().add(getClass().getResource("Style.css").toExternalForm());
Color foreground = Color.rgb(255, 255, 255, 0.9);
//Rectangila Background
Rectangle background = new Rectangle(320, 250);
background.setX(0);
background.setY(0);
background.setArcHeight(15);
background.setArcWidth(15);
background.setFill(Color.rgb(0 ,0 , 0, 0.55));
background.setStroke(foreground);
background.setStrokeWidth(1.5);
VBox vbox = new VBox(5);
vbox.setPadding(new Insets(10,0,0,10));
Label label = new Label("Label");
//label.setTextFill(Color.WHITESMOKE);
label.setFont(new Font("SanSerif", 20));
TextField username = new TextField();
username.setFont(Font.font("SanSerif", 20));
username.setPromptText("Username");
username.getStyleClass().add("field-background");
PasswordField password =new PasswordField();
password.setFont(Font.font("SanSerif", 20));
password.setPromptText("Password");
password.getStyleClass().add("field-background");
Button btn = new Button("Login");
btn.setFont(Font.font("SanSerif", 15));
btn.setOnAction(e ->{
try{
String user = username.getText();
String pass = password.getText();
String query = "SELECT * FROM userdatabasetable Where UserName = " + "'" + user + "'" + " AND Password = " + "'" +pass + "'" + " ";
rs = pst.executeQuery(query);
if(rs.next()){
label.setText("Login Successful");
primaryStage.setScene(newscene);
primaryStage.show();
}else{
label.setText("Login Failed");
}
username.clear();
password.clear();
pst.close();
rs.close();
}catch(Exception e1){
label.setText("SQL Error");
System.out.println("Wrong UserName Or Password");
//System.err.println(e1);
}
});
vbox.getChildren().addAll(label, username, password, btn);
root.getChildren().addAll(background, vbox);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
Connection createConnection ()
{
try
{
//Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/UserDataBase","yusof","1234");
System.out.println("DataBase Connected Successfully");
//con.close();
}
catch (ClassNotFoundException | SQLException ex)
{
Logger.getLogger(DataBaseProject1.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
OUTPUT:
DataBase Connected Successfully Wrong UserName Or Password
SCREENSHOT FOR MySQL:enter image description here
e1.printStackTrace().