Skip to content

Commit 92d93f7

Browse files
committed
Added a login page and made connection with database.
0 parents  commit 92d93f7

File tree

9 files changed

+254
-0
lines changed

9 files changed

+254
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Example user template template
3+
### Example user template
4+
5+
# IntelliJ project files
6+
.idea
7+
*.iml
8+
out
9+
gen

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#**Task and bug tracking system**
2+
3+
Login page is half-ready now.
4+
5+
#### Developed with
6+
7+
Java 14, JavaFX 15, MySQL 8
8+
9+
10+
#### How to
11+
12+
1) Download **Java**.
13+
14+
2) Download **IntelliJ**.
15+
16+
3) Download **JavaFX**.
17+
Here is a tutorial https://www.jetbrains.com/help/idea/javafx.html#add-javafx-lib .
18+
19+
4) Download **MySQL**. Don't forget
20+
about **Java Driver**,
21+
so you can connect to database.
22+
23+
5) Download **SceneBuilder**
24+
which is a drag and drop UI designer tool
25+
that generates FXML.
26+
27+
6) Add src folder to libraries in Project Structure...
28+
(that helped to fix problem "TaskTrackingApp module is not found").
29+
30+
7) Create a MySQL server. Then create a database called dbts. I used DataGrips with student licension.
31+
32+
8) Install a plugin **Database Navigator** in IntelliJ.
33+
Use the plugin to connect with database.
34+
In class DatabaseConnection edit database name,
35+
url, user, password.
36+
37+

SQL scripts/scriptUsers.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
CREATE DATABASE dbts;
2+
3+
USE dbts;
4+
5+
CREATE TABLE users
6+
(
7+
id INT unsigned NOT NULL AUTO_INCREMENT,
8+
email VARCHAR(150) NOT NULL,
9+
name VARCHAR(150) NOT NULL,
10+
surname VARCHAR(150),
11+
password VARCHAR(150) NOT NULL,
12+
PRIMARY KEY (id)
13+
);
14+
15+
INSERT INTO users ( email, name, surname, password) VALUES
16+
( 'nastya@gmail.com', 'Анастасия', 'Сибирская', 'Sibir1sky' );

src/module-info.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module TaskTrackingApp {
2+
requires javafx.graphics;
3+
requires javafx.fxml;
4+
requires javafx.controls;
5+
requires java.sql;
6+
requires mysql.connector.java;
7+
8+
opens sample;
9+
}

src/sample/DatabaseConnection.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package sample;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
6+
public class DatabaseConnection {
7+
private Connection dbLink;
8+
9+
public Connection getConnection(){
10+
String dbName = "dbts"; //DataBase Tracking System
11+
String dbUser = "root";
12+
String dbPassword = "root";
13+
String url = "jdbc:mysql://localhost:3306/" + dbName;
14+
15+
try{
16+
Class.forName("com.mysql.cj.jdbc.Driver");
17+
dbLink = DriverManager.getConnection(url, dbUser, dbPassword);
18+
}catch(Exception e){
19+
e.printStackTrace();
20+
e.getCause();
21+
}
22+
23+
return dbLink;
24+
}
25+
26+
public Connection getLink() {
27+
return dbLink;
28+
}
29+
}

src/sample/LoginController.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package sample;
2+
3+
import javafx.fxml.FXML;
4+
import javafx.fxml.Initializable;
5+
import javafx.scene.control.Button;
6+
import javafx.scene.control.Label;
7+
import javafx.scene.control.TextField;
8+
import javafx.scene.image.Image;
9+
import javafx.scene.image.ImageView;
10+
import javafx.stage.Stage;
11+
12+
import java.io.File;
13+
import java.net.URL;
14+
import java.sql.Connection;
15+
import java.sql.DriverManager;
16+
import java.sql.ResultSet;
17+
import java.sql.Statement;
18+
import java.util.ResourceBundle;
19+
20+
import util.MyChecker;
21+
22+
public class LoginController implements Initializable {
23+
@FXML
24+
private TextField loginField;
25+
26+
@FXML
27+
private Label invalidLoginLabel;
28+
29+
@FXML
30+
private Button continueButton;
31+
32+
@FXML
33+
private Button cancelButton;
34+
35+
@FXML
36+
private ImageView leftImage;
37+
38+
public void onContinue(){
39+
if(loginField.getText().isBlank()){
40+
invalidLoginLabel.setText("Введите почту");
41+
}
42+
else if( !MyChecker.isEmail( loginField.getText() ) ){
43+
invalidLoginLabel.setText("Не является почтой или содержит недопустимые символы. \nВводите в формате email@box.any с -._");
44+
}
45+
else if (!validateEmail()){
46+
invalidLoginLabel.setText("Почта не найдена. \nПроверьте правильность или зарегистрируйтесь");
47+
}
48+
else{
49+
invalidLoginLabel.setText("Продолжение следует...");
50+
}
51+
}
52+
53+
public void onCancel(){
54+
Stage stage = (Stage) cancelButton.getScene().getWindow(); // could be better code here
55+
stage.close();
56+
}
57+
58+
@Override
59+
public void initialize(URL url, ResourceBundle resourceBundle) {
60+
File file1 = new File("images/0.png"); //no picture now
61+
Image image1 = new Image(file1.toURI().toString());
62+
leftImage.setImage(image1);
63+
}
64+
65+
private boolean validateEmail(){
66+
DatabaseConnection toolDB = new DatabaseConnection();
67+
Connection toDB = toolDB.getConnection();
68+
69+
String verifyLogin = "SELECT count(1) FROM users WHERE email = '" + loginField.getText() + "';";
70+
71+
try{
72+
Statement statement = toDB.createStatement();
73+
ResultSet queryResult = statement.executeQuery(verifyLogin);
74+
75+
while(queryResult.next()){
76+
if(queryResult.getInt(1) == 1){
77+
return true;
78+
} else {
79+
return false;
80+
}
81+
}
82+
83+
}catch(Exception e){
84+
e.printStackTrace();
85+
e.getCause();
86+
}
87+
88+
return false;
89+
}
90+
}

src/sample/Main.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package sample;
2+
3+
import javafx.application.Application;
4+
import javafx.fxml.FXMLLoader;
5+
import javafx.scene.Parent;
6+
import javafx.scene.Scene;
7+
import javafx.stage.Stage;
8+
import javafx.stage.StageStyle;
9+
10+
public class Main extends Application {
11+
12+
@Override
13+
public void start(Stage primaryStage) throws Exception{
14+
Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
15+
primaryStage.initStyle(StageStyle.UNDECORATED);
16+
primaryStage.setTitle("Hello World");
17+
primaryStage.setScene(new Scene(root));
18+
primaryStage.show();
19+
}
20+
21+
22+
public static void main(String[] args) {
23+
launch(args);
24+
}
25+
}

src/sample/login.fxml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<?import javafx.scene.control.*?>
4+
<?import javafx.scene.image.*?>
5+
<?import javafx.scene.layout.*?>
6+
7+
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="520.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.LoginController">
8+
<left>
9+
<AnchorPane prefHeight="400.0" prefWidth="200.0" BorderPane.alignment="CENTER_LEFT">
10+
<children>
11+
<ImageView fx:id="leftImage" fitHeight="400.0" fitWidth="200.0" layoutX="-1.0" pickOnBounds="true" preserveRatio="true" />
12+
</children></AnchorPane>
13+
</left>
14+
<right>
15+
<AnchorPane prefWidth="320.0" BorderPane.alignment="CENTER_RIGHT">
16+
<children>
17+
<Label layoutX="55.0" layoutY="159.0" text="Почта" />
18+
<TextField fx:id="loginField" layoutX="55.0" layoutY="187.0" promptText="email@box.any" />
19+
<Button fx:id="continueButton" layoutX="55.0" layoutY="273.0" mnemonicParsing="false" onAction="#onContinue" prefHeight="27.0" prefWidth="125.0" text="Продолжить" />
20+
<Button fx:id="cancelButton" layoutX="55.0" layoutY="311.0" mnemonicParsing="false" onAction="#onCancel" prefHeight="27.0" prefWidth="125.0" text="Отмена" />
21+
<Label fx:id="invalidLoginLabel" layoutX="55.0" layoutY="214.0" prefHeight="27.0" prefWidth="157.0" text="Пользователь не найден" />
22+
</children></AnchorPane>
23+
</right>
24+
</BorderPane>

src/util/MyChecker.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package util;
2+
3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
6+
public class MyChecker {
7+
private static final String regex = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
8+
9+
public static boolean isEmail(String email){
10+
Pattern pattern = Pattern.compile(regex);
11+
Matcher matcher = pattern.matcher(email);
12+
return matcher.matches();
13+
}
14+
15+
}

0 commit comments

Comments
 (0)