kia9372
دوشنبه 16 فروردین 1395, 10:18 صبح
یه فرم ورود با SQL Server 2014 ساختم و به دیتا بیس هم وصل میشه ولی وقتی یوزر نیم و پسورد رو میزنم
میزنم که ببینم درست کار میکنه یا نه این خطا میاد : java.lang.NullPointerException باید چیکار کنم ؟
public static void Start(Stage PrimaryStage) {
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER_RIGHT);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(35, 35, 35, 35));
Scene scene = new Scene(grid, 450, 300);
PrimaryStage.setTitle("فرم ورود");
PrimaryStage.setScene(scene);
PrimaryStage.show();
scene.getStylesheets().add(Login.class.getResource ("StyleSheet.css").toExternalForm());
Label lblWelcome = new Label("Log In");
grid.add(lblWelcome, 0, 0, 2, 1);
Label lblUsername = new Label("User Name : ");
grid.add(lblUsername, 0, 1);
final TextField txtUsername = new TextField();
grid.add(txtUsername, 1, 1);
txtUsername.setScaleY(1);
Label lblPassword = new Label("Password : ");
grid.add(lblPassword, 0, 2);
final PasswordField txtpassword = new PasswordField();
grid.add(txtpassword, 1, 2);
Button btnSingin = new Button("Sign In");
HBox hbox = new HBox(10);
hbox.setAlignment(Pos.CENTER_RIGHT);
hbox.getChildren().add(btnSingin);
grid.add(hbox, 1, 4);
btnSingin.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
Connection conn ;
ResultSet rs ;
PreparedStatement pst ;
conn = SQLConnection.connectDB();
String sqlString = "select * from Admin where username=? and password=? ";
try {
pst = conn.prepareStatement(sqlString);
pst.setString(1, txtUsername.getText());
pst.setString(1, txtpassword.getText());
rs = pst.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(null, " Username And Password Was Correct ");
} else {
JOptionPane.showMessageDialog(null, " Username And Password Not Was correct . . . ");
}
} catch (Exception e) {
System.out.println(e);
}
}
}
);
}
میزنم که ببینم درست کار میکنه یا نه این خطا میاد : java.lang.NullPointerException باید چیکار کنم ؟
public static void Start(Stage PrimaryStage) {
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER_RIGHT);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(35, 35, 35, 35));
Scene scene = new Scene(grid, 450, 300);
PrimaryStage.setTitle("فرم ورود");
PrimaryStage.setScene(scene);
PrimaryStage.show();
scene.getStylesheets().add(Login.class.getResource ("StyleSheet.css").toExternalForm());
Label lblWelcome = new Label("Log In");
grid.add(lblWelcome, 0, 0, 2, 1);
Label lblUsername = new Label("User Name : ");
grid.add(lblUsername, 0, 1);
final TextField txtUsername = new TextField();
grid.add(txtUsername, 1, 1);
txtUsername.setScaleY(1);
Label lblPassword = new Label("Password : ");
grid.add(lblPassword, 0, 2);
final PasswordField txtpassword = new PasswordField();
grid.add(txtpassword, 1, 2);
Button btnSingin = new Button("Sign In");
HBox hbox = new HBox(10);
hbox.setAlignment(Pos.CENTER_RIGHT);
hbox.getChildren().add(btnSingin);
grid.add(hbox, 1, 4);
btnSingin.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
Connection conn ;
ResultSet rs ;
PreparedStatement pst ;
conn = SQLConnection.connectDB();
String sqlString = "select * from Admin where username=? and password=? ";
try {
pst = conn.prepareStatement(sqlString);
pst.setString(1, txtUsername.getText());
pst.setString(1, txtpassword.getText());
rs = pst.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(null, " Username And Password Was Correct ");
} else {
JOptionPane.showMessageDialog(null, " Username And Password Not Was correct . . . ");
}
} catch (Exception e) {
System.out.println(e);
}
}
}
);
}