· 6 years ago · Apr 06, 2019, 08:26 PM
1package application;
2
3import java.sql.Connection;
4import java.sql.Date;
5import java.sql.DriverManager;
6import java.sql.PreparedStatement;
7import java.sql.SQLException;
8import java.sql.Statement;
9import javafx.event.ActionEvent;
10import javafx.fxml.FXML;
11import javafx.scene.control.Button;
12import javafx.scene.control.CheckBox;
13import javafx.scene.control.Label;
14import javafx.scene.control.PasswordField;
15import javafx.scene.control.TextField;
16
17
18public class Controll
19{
20 int id;
21 long dateRegistration;
22
23 @FXML
24 private TextField regSurname;
25
26 @FXML
27 private TextField regLogin;
28
29 @FXML
30 private Button butt2;
31
32 @FXML
33 private PasswordField regPass;
34
35 @FXML
36 private Button butt1;
37
38 @FXML
39 private TextField regEmail;
40
41 @FXML
42 private PasswordField logPass;
43
44 @FXML
45 private CheckBox showPassLog;
46
47 @FXML
48 private CheckBox showPassReg;
49
50 @FXML
51 private TextField logLogin;
52
53 @FXML
54 private TextField regName;
55
56 @FXML
57 private PasswordField regPassConf;
58
59 @FXML
60 private Label checkRegister;
61
62 // private static final String DRIVER = null;
63 public static final String DRIVER = "org.sqlite.JDBC";
64 public static final String DB_URL = "jdbc:sqlite:rejestracja.db";
65
66 private Connection conn;
67 private Statement stat;
68
69 public Controll()
70 {
71 try
72 {
73 Class.forName(Controll.DRIVER);
74 }catch(ClassNotFoundException e)
75 {
76 System.out.println("Brak sterowania JDBC");
77 e.printStackTrace();
78 }
79
80 try
81 {
82 conn = DriverManager.getConnection(DB_URL);
83 stat = conn.createStatement();
84 }catch(SQLException e)
85 {
86 System.err.println("Problem z otwarciem połączenia");
87 e.printStackTrace();
88 }
89 createTables();
90 }
91
92 public boolean createTables()
93 {
94 String createRegistration = "create table if not exists registration(ID integer primary key autoincrement, firstName varchar(10), surName varchar(15), login varchar(15), mail varchar(20), password varchar(15), dateRegistration date,permissions varchar(15))";
95 try
96 {
97 stat.execute(createRegistration);
98 }catch(SQLException e)
99 {
100 System.err.println("Bład przy tworzeniu tabeli");
101 e.printStackTrace();
102 return false;
103 }
104 return true;
105
106
107 }
108
109 public boolean insertRegistration(int ID, String regName, String regSurname, String regLogin, String regEmail, String regPass,long dateRegistration, String permission)
110 {
111 try
112 {
113 PreparedStatement prepStmt = conn.prepareStatement("insert into registration values(NULL, ?, ?, ?, ?, ?, ?, 'user');");
114 prepStmt.setString(1, regName);
115 prepStmt.setString(2, regSurname);
116 prepStmt.setString(3, regLogin);
117 prepStmt.setString(4, regEmail);
118 prepStmt.setString(5, regPass);
119 prepStmt.setFloat(6, dateRegistration);
120 prepStmt.execute();
121 }catch(SQLException e)
122 {
123 System.out.println("Bład przy wstawianiu danych do tabeli rejetracja!");
124 e.printStackTrace();
125 return false;
126 }
127 return true;
128 }
129
130 public void closeConnection()
131 {
132 try
133 {
134 conn.close();
135 }catch(SQLException e)
136 {
137 System.err.println("Problem z zamknięcie połączenia");
138 e.printStackTrace();
139 }
140 }
141 @FXML
142 void logSend(ActionEvent event) {
143 System.out.println("login: "+logLogin.getText());
144 System.out.println("hasło: "+logPass.getText());
145
146 }
147 @FXML
148 void regSend(ActionEvent event) {
149 System.out.println("Imie: "+regName.getText());
150 System.out.println("Nazwisko: "+regSurname.getText());
151 System.out.println("login: "+regLogin.getText());
152 System.out.println("email: "+regEmail.getText());
153 System.out.println("hasło: "+regPass.getText());
154 Date date = new Date(dateRegistration);
155 System.out.println(date);
156 insertRegistration(id, regName.toString(), regSurname.toString(), regLogin.toString(), regEmail.toString(), regPass.toString(), dateRegistration, null);
157 }
158
159 @FXML
160 void showPassReg(ActionEvent event) {
161 if (showPassReg.isSelected())
162 {
163 regPass.setPromptText(regPass.getText());
164 regPass.setText("");
165 regPass.setDisable(true);
166 regPassConf.setPromptText(regPass.getText());
167 regPassConf.setText("");
168 regPassConf.setDisable(true);
169 }
170 else
171 {
172 regPass.setPromptText(regPass.getText());
173 regPass.setText("");
174 regPass.setDisable(false);
175 regPassConf.setPromptText(regPass.getText());
176 regPassConf.setText("");
177 regPassConf.setDisable(false);
178 }
179 }
180
181 @FXML
182 void showPassLog(ActionEvent event) {
183 if (showPassLog.isSelected())
184 {
185 logPass.setPromptText(logPass.getText());
186 logPass.setText("");
187 logPass.setDisable(true);
188 }
189 else
190 {
191 logPass.setPromptText(logPass.getText());
192 logPass.setText("");
193 logPass.setDisable(false);
194 }
195 }
196
197}