Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main extends Application {
- @Override
- public void start(Stage primaryStage) {
- try {
- Parent root = FXMLLoader.load(getClass().getResource("view/Login.fxml"));
- Scene scene = new Scene(root);
- scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
- primaryStage.setScene(scene);
- primaryStage.show();
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
- public class LoginController implements Initializable {
- public LoginModel loginModel = new LoginModel();
- @FXML
- private Label isConnected;
- @FXML
- private TextField txtUsername;
- @FXML
- private TextField txtPassword;
- @Override
- public void initialize(URL location, ResourceBundle resources) {
- // TODO Auto-generated method stub
- if (loginModel.isDbConnected()) {
- isConnected.setText("Connected");
- } else {
- isConnected.setText("Not Connected");
- }
- }
- public void Login(ActionEvent event){
- try {
- if (loginModel.isLogin(txtUsername.getText(), txtPassword.getText())){
- isConnected.setText("username and password is correct");
- ((Node)event.getSource()).getScene().getWindow().hide();
- Stage primaryStage = new Stage();
- FXMLLoader loader = new FXMLLoader();
- Pane root = loader.load(getClass().getResource("view/test1.fxml").openStream());
- CompanyMainController companyMainController = (CompanyMainController)loader.getController();
- companyMainController.GetUser(txtUsername.getText());
- Scene scene = new Scene(root);
- scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
- primaryStage.setScene(scene);
- primaryStage.show();
- } else {
- isConnected.setText("username and password is not correct");
- }
- } catch (SQLException e) {
- isConnected.setText("username and password is not correct");
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public class CompanyMainController implements Initializable {
- @FXML
- private Label userLbl;
- @Override
- public void initialize(URL location, ResourceBundle resources) {
- // TODO Auto-generated method stub
- }
- public void GetUser(String user) {
- // TODO Auto-generated method stub
- userLbl.setText(user);
- }
- public void SignOut(ActionEvent event) {
- try {
- ((Node)event.getSource()).getScene().getWindow().hide();
- Stage primaryStage = new Stage();
- FXMLLoader loader = new FXMLLoader();
- Pane root = loader.load(getClass().getResource("/application/Login.fxml").openStream());
- Scene scene = new Scene(root);
- scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
- primaryStage.setScene(scene);
- primaryStage.show();
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- }
- public class LoginModel {
- Connection conection;
- public LoginModel () {
- conection = SqliteConnection.Connector();
- //*******************************if not conected all closes************************************
- if (conection == null) {
- System.out.println("connection not successful");
- System.exit(1);}
- }
- public boolean isDbConnected() {
- try {
- return !conection.isClosed();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- return false;
- }
- }
- public boolean isLogin(String user, String pass) throws SQLException {
- PreparedStatement preparedStatement = null;
- ResultSet resultSet = null;
- // soo this one is searcing in db employee section for username and password
- String query = "select * from employee where username = ? and password = ?";
- try {
- preparedStatement = conection.prepareStatement(query);
- preparedStatement.setString(1, user);
- preparedStatement.setString(2, pass);
- resultSet = preparedStatement.executeQuery();
- if (resultSet.next()) {
- return true;
- }
- else {
- return false;
- }
- } catch (Exception e) {
- return false;
- // TODO: handle exception
- } finally {
- preparedStatement.close();
- resultSet.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement