-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertarUsuario.java
349 lines (275 loc) · 10.1 KB
/
InsertarUsuario.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package HackatonInicioUsuario;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class InsertarUsuario extends JFrame {
private JPanel contentPane;
private String base = "usuarios";
private final String URL = "jdbc:mysql://localhost:3306/" + base + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
private final String USERNAME = "";
private final String PASSWORD = "";
public Connection con = null;
public Connection getConnection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = (Connection) DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException | SQLException e) {
System.err.println(e);
}
return con;
}
public void limpiarCajas() {
txtUsuario.setText(null);
txtNombre.setText(null);
txtPassword.setText(null);
txtConfirmarPassword.setText(null);
txtCorreo.setText(null);
}
private JTextField txtUsuario;
private JPasswordField txtPassword;
private JPasswordField txtConfirmarPassword;
private JTextField txtNombre;
private JTextField txtCorreo;
public UsuPoo usu = new UsuPoo();
private JTextField txtPais;
private JTextField txtEstado;
private JTextField txtCondado;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
InsertarUsuario frame = new InsertarUsuario();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public InsertarUsuario() {
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 545, 531);
contentPane = new JPanel();
contentPane.setBackground(Color.DARK_GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("USER");
lblNewLabel.setFont(new Font("Futura Bk BT", Font.BOLD, 11));
lblNewLabel.setForeground(Color.WHITE);
lblNewLabel.setBounds(42, 338, 101, 14);
contentPane.add(lblNewLabel);
txtUsuario = new JTextField();
txtUsuario.setBackground(Color.WHITE);
txtUsuario.setBounds(153, 338, 290, 20);
contentPane.add(txtUsuario);
txtUsuario.setColumns(10);
JLabel lblNewLabel_1 = new JLabel("PASSWORD");
lblNewLabel_1.setFont(new Font("Futura Bk BT", Font.BOLD, 11));
lblNewLabel_1.setForeground(Color.WHITE);
lblNewLabel_1.setBounds(42, 114, 114, 14);
contentPane.add(lblNewLabel_1);
txtPassword = new JPasswordField();
txtPassword.setBackground(Color.WHITE);
txtPassword.setBounds(153, 114, 290, 20);
contentPane.add(txtPassword);
JLabel lblNewLabel_2 = new JLabel("<html>CONFIRM PASSWORD</html>");
lblNewLabel_2.setFont(new Font("Futura Bk BT", Font.BOLD, 11));
lblNewLabel_2.setForeground(Color.WHITE);
lblNewLabel_2.setBounds(42, 163, 89, 28);
contentPane.add(lblNewLabel_2);
txtConfirmarPassword = new JPasswordField();
txtConfirmarPassword.setBackground(Color.WHITE);
txtConfirmarPassword.setBounds(153, 163, 290, 20);
contentPane.add(txtConfirmarPassword);
JLabel lblNewLabel_3 = new JLabel("NAME");
lblNewLabel_3.setFont(new Font("Futura Bk BT", Font.BOLD, 11));
lblNewLabel_3.setForeground(Color.WHITE);
lblNewLabel_3.setBounds(42, 65, 89, 14);
contentPane.add(lblNewLabel_3);
txtNombre = new JTextField();
txtNombre.setBackground(Color.WHITE);
txtNombre.setBounds(153, 65, 290, 20);
contentPane.add(txtNombre);
txtNombre.setColumns(10);
JLabel lblNewLabel_4 = new JLabel("E-MAIL ADRESS");
lblNewLabel_4.setFont(new Font("Futura Bk BT", Font.BOLD, 11));
lblNewLabel_4.setForeground(Color.WHITE);
lblNewLabel_4.setBounds(42, 392, 101, 14);
contentPane.add(lblNewLabel_4);
txtCorreo = new JTextField();
txtCorreo.setBackground(Color.WHITE);
txtCorreo.setBounds(153, 392, 290, 20);
contentPane.add(txtCorreo);
txtCorreo.setColumns(10);
JButton btnRegistrar = new JButton("REGISTER");
btnRegistrar.setForeground(Color.WHITE);
btnRegistrar.setBackground(Color.GRAY);
btnRegistrar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String pass = new String(txtPassword.getPassword());
String passCon = new String(txtConfirmarPassword.getPassword());
if (txtUsuario.getText().equals("") || pass.equals("") || passCon.equals("")
|| txtNombre.getText().equals("") || txtCorreo.getText().equals("")
|| txtPais.getText().equals("") || txtEstado.getText().equals("")
|| txtCondado.getText().equals("")) {
JOptionPane.showMessageDialog(null, "There are spaces that need to be filled");
} else {
if (pass.equals(passCon)) {
if (usuarioExistente(txtUsuario.getText()) == 0) {
if (esEmail(txtCorreo.getText())) {
String nuevoPass = hash.sha1(pass);
usu.setUsuario(txtUsuario.getText());
usu.setPassword(nuevoPass);
usu.setNombre(txtNombre.getText());
usu.setCorreo(txtCorreo.getText());
usu.setId_tipo(1);
usu.setPais(txtPais.getText());
usu.setCiudad(txtCondado.getText());
usu.setEstado(txtEstado.getText());
int confirmar = JOptionPane.showConfirmDialog(null,
"The Password ans the usar cnt be changed, are you sure you want the password to be <<"
+ pass + ">> and the user <<" + txtUsuario.getText()
+ ">> ? \n We recomend that you take a picture of this pane or anotate the information",
"Porfavor seleccione", JOptionPane.YES_NO_OPTION);
if (confirmar == 0) {
if (registrar(usu)) {
JOptionPane.showMessageDialog(null, "Information Saved");
limpiarCajas();
} else {
JOptionPane.showMessageDialog(null, "Error at saving the user");
limpiarCajas();
}
} else {
JOptionPane.showMessageDialog(null, "Perfect, change the password for something safer");
}
} else {
JOptionPane.showMessageDialog(null, "The E-Mail doesnt have E-mail format");
}
} else {
JOptionPane.showMessageDialog(null, "The user already exists");
}
} else {
JOptionPane.showMessageDialog(null, "Passwords arent the same");
}
}
}
});
btnRegistrar.setBounds(227, 445, 101, 23);
contentPane.add(btnRegistrar);
JButton btnAtras = new JButton("BACK");
btnAtras.setForeground(Color.WHITE);
btnAtras.setBackground(Color.GRAY);
btnAtras.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnAtras.setBounds(417, 11, 89, 23);
contentPane.add(btnAtras);
JLabel lblNewLabel_1_1 = new JLabel("COUNTRY");
lblNewLabel_1_1.setForeground(Color.WHITE);
lblNewLabel_1_1.setFont(new Font("Dialog", Font.BOLD, 11));
lblNewLabel_1_1.setBounds(42, 228, 114, 14);
contentPane.add(lblNewLabel_1_1);
txtPais = new JTextField();
txtPais.setBounds(114, 226, 131, 20);
contentPane.add(txtPais);
txtPais.setColumns(10);
JLabel lblNewLabel_2_1 = new JLabel("<html>STATE</html>");
lblNewLabel_2_1.setForeground(Color.WHITE);
lblNewLabel_2_1.setFont(new Font("Dialog", Font.BOLD, 11));
lblNewLabel_2_1.setBounds(258, 225, 89, 20);
contentPane.add(lblNewLabel_2_1);
txtEstado = new JTextField();
txtEstado.setColumns(10);
txtEstado.setBounds(312, 226, 131, 20);
contentPane.add(txtEstado);
JLabel lblNewLabel_2_1_1 = new JLabel("<html>CITY/COUNTY</html>");
lblNewLabel_2_1_1.setForeground(Color.WHITE);
lblNewLabel_2_1_1.setFont(new Font("Dialog", Font.BOLD, 11));
lblNewLabel_2_1_1.setBounds(123, 283, 89, 20);
contentPane.add(lblNewLabel_2_1_1);
txtCondado = new JTextField();
txtCondado.setColumns(10);
txtCondado.setBounds(217, 284, 196, 20);
contentPane.add(txtCondado);
}
public boolean registrar(UsuPoo usu) {
PreparedStatement ps = null;
Connection con = getConnection();
try {
String sql = "INSERT INTO `usuarios`.`usuarios` (`usuario`, `password`, `nombre`, `correo`, `id_tipo`) VALUES (?,?,?,?,?); ";
ps = con.prepareStatement(sql);
ps.setString(1, usu.getUsuario());
ps.setString(2, usu.getPassword());
ps.setString(3, usu.getNombre());
ps.setString(4, usu.getCorreo());
ps.setInt(5, usu.getId_tipo());
ps.execute();
return true;
} catch (SQLException e) {
System.err.println(e);
return false;
} finally {
try {
con.close();
} catch (SQLException e) {
System.err.println(e);
}
}
}
public int usuarioExistente(String usuario) {
PreparedStatement ps = null;
ResultSet rs = null;
Connection con = getConnection();
try {
String sql = "SELECT count(`id_tipo`) FROM `usuarios`.`usuarios` WHERE (`usuario` = ?); ";
ps = con.prepareStatement(sql);
ps.setString(1, usuario);
rs = ps.executeQuery();
if (rs.next()) {
return rs.getInt(1);
}
return 1;
} catch (SQLException e) {
System.err.println(e);
return 1;
} finally {
try {
con.close();
} catch (SQLException e) {
System.err.println(e);
}
}
}
public boolean esEmail(String correo) {
Pattern pattern = Pattern.compile(
"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
Matcher matcher = pattern.matcher(correo);
return matcher.find();
}
}