Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.43 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.EventQueue;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Connection;
  8. import java.sql.Statement;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11.  
  12. import javax.swing.JFrame;
  13. import javax.swing.JPanel;
  14. import javax.swing.border.EmptyBorder;
  15. import javax.swing.text.DefaultEditorKit.InsertBreakAction;
  16.  
  17.  
  18.  
  19.  
  20.  
  21. import javax.swing.JLabel;
  22. import javax.swing.JOptionPane;
  23.  
  24. import java.awt.Font;
  25. import java.awt.Color;
  26. import javax.swing.ImageIcon;
  27. import java.awt.Toolkit;
  28. import javax.swing.JTextField;
  29. import javax.swing.JPasswordField;
  30. import javax.swing.JComboBox;
  31. import javax.swing.JFormattedTextField;
  32. import javax.swing.DefaultComboBoxModel;
  33. import javax.swing.JButton;
  34. import java.awt.event.ActionListener;
  35. import java.awt.event.ActionEvent;
  36.  
  37. public class Registro extends JFrame {
  38.  
  39. private JPanel contentPane;
  40. private JTextField tfUsuario;
  41. private JTextField tfNombre;
  42. private JTextField tfTelefono;
  43. private JPasswordField tfContraseña;
  44. private JTextField tfEmail;
  45. /**
  46. * Launch the application.
  47. */
  48. int campos = 0, EmailValido = 0;
  49. private static Connection connection;
  50.  
  51. public static void main(String[] args) {
  52. connection = null;
  53. Statement query = null;
  54. try{
  55. //STEP 2: Register JDBC driver
  56. Class.forName("com.mysql.jdbc.Driver");
  57.  
  58. //STEP 3: Open a connection
  59. System.out.println("Connecting to a selected database...");
  60. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java_project","root", "usbw");
  61. System.out.println("Connected database successfully...");
  62.  
  63. //STEP 4: Execute a query
  64. System.out.println("Creating statement...");
  65. }catch(SQLException se){
  66. //Handle errors for JDBC
  67. se.printStackTrace();
  68. }catch(Exception e){
  69. //Handle errors for Class.forName
  70. e.printStackTrace();
  71. }finally{
  72. //finally block used to close resources
  73. try{
  74. if(query!=null)
  75. connection.close();
  76. }catch(SQLException se){
  77. se.printStackTrace();
  78. }// do nothing
  79. try{
  80. if(connection!=null)
  81. connection.close();
  82. }catch(SQLException se){
  83. se.printStackTrace();
  84. }//end finally try
  85. }//end try
  86.  
  87. EventQueue.invokeLater(new Runnable() {
  88. public void run() {
  89. try {
  90. Registro frame = new Registro(connection);
  91. frame.setVisible(true);
  92. } catch (Exception e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. });
  97. }
  98.  
  99. /**
  100. * Create the frame.
  101. */
  102. public class EmailValidator {
  103.  
  104. private Pattern pattern;
  105. private Matcher matcher;
  106.  
  107. private static final String EMAIL_PATTERN =
  108. "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
  109. + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
  110.  
  111. public EmailValidator() {
  112. pattern = Pattern.compile(EMAIL_PATTERN);
  113. }
  114.  
  115. /**
  116. * Validate hex with regular expression
  117. *
  118. * @param hex
  119. * hex for validation
  120. * @return true valid hex, false invalid hex
  121. */
  122. public boolean validate(final String hex) {
  123.  
  124. matcher = pattern.matcher(hex);
  125. return matcher.matches();
  126.  
  127. }
  128. }
  129.  
  130. public Registro(final Connection connection) {
  131. setForeground(Color.BLACK);
  132. setTitle("Registro");
  133. setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Gonzalo\\Desktop\\Programaci\u00F3n\\eclipse\\configuration\\org.eclipse.osgi\\379\\0\\.cp\\icons\\elcl16\\add_repository.png"));
  134. setResizable(false);
  135. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  136. setBounds(100, 100, 545, 429);
  137. contentPane = new JPanel();
  138. contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  139. setContentPane(contentPane);
  140. contentPane.setLayout(null);
  141.  
  142. JLabel lblRegistro = new JLabel("Registro");
  143. lblRegistro.setIcon(new ImageIcon("C:\\Users\\Gonzalo\\Desktop\\Programaci\u00F3n\\eclipse\\configuration\\org.eclipse.osgi\\402\\0\\.cp\\themes\\slate\\graphics\\icons\\ctool\\wb_nav.png"));
  144. lblRegistro.setForeground(new Color(255, 165, 0));
  145. lblRegistro.setFont(new Font("Trebuchet MS", Font.ITALIC, 20));
  146. lblRegistro.setBounds(201, 11, 127, 67);
  147. contentPane.add(lblRegistro);
  148.  
  149. tfUsuario = new JTextField();
  150. tfUsuario.setBounds(91, 93, 127, 20);
  151. contentPane.add(tfUsuario);
  152. tfUsuario.setColumns(10);
  153.  
  154. tfNombre = new JTextField();
  155. tfNombre.setBounds(91, 124, 127, 20);
  156. contentPane.add(tfNombre);
  157. tfNombre.setColumns(10);
  158.  
  159. tfTelefono = new JTextField();
  160. tfTelefono.setBounds(91, 186, 127, 20);
  161. contentPane.add(tfTelefono);
  162. tfTelefono.setColumns(10);
  163.  
  164. tfContraseña = new JPasswordField();
  165. tfContraseña.setBounds(91, 155, 127, 20);
  166. contentPane.add(tfContraseña);
  167.  
  168. JLabel lblNombre = new JLabel("Usuario");
  169. lblNombre.setBounds(10, 96, 71, 14);
  170. contentPane.add(lblNombre);
  171.  
  172. JLabel lblNombre_1 = new JLabel("Nombre");
  173. lblNombre_1.setBounds(10, 127, 71, 14);
  174. contentPane.add(lblNombre_1);
  175.  
  176. JLabel lblContrasea = new JLabel("Contrase\u00F1a");
  177. lblContrasea.setBounds(10, 158, 86, 14);
  178. contentPane.add(lblContrasea);
  179.  
  180. JLabel lblNewLabel = new JLabel("Tel\u00E9fono");
  181. lblNewLabel.setBounds(10, 189, 86, 14);
  182. contentPane.add(lblNewLabel);
  183.  
  184. JLabel lblPas = new JLabel("Pa\u00EDs");
  185. lblPas.setBounds(10, 220, 46, 14);
  186. contentPane.add(lblPas);
  187.  
  188. tfEmail = new JTextField();
  189. tfEmail.setBounds(91, 248, 193, 20);
  190. contentPane.add(tfEmail);
  191. tfEmail.setColumns(10);
  192.  
  193. JLabel lblNewLabel_1 = new JLabel("Email");
  194. lblNewLabel_1.setBounds(10, 251, 86, 14);
  195. contentPane.add(lblNewLabel_1);
  196.  
  197. final JComboBox comboboxPais = new JComboBox();
  198. comboboxPais.setModel(new DefaultComboBoxModel(new String[] {" ", "Argentina", "Bolivia", "Chile", "Colombia", "Costa Rica", "Ecuador", "El Salvador", "Espa\u00F1a", "Guatemala", "Guinea Ecuatorial", "Honduras", "M\u00E9xico", "Panam\u00E1", "Paraguay", "Per\u00FA", "Puerto Rico", "Rep\u00FAblica Dominicana", "Uruguay", "Venezuela"}));
  199. comboboxPais.setBounds(91, 217, 127, 20);
  200. contentPane.add(comboboxPais);
  201.  
  202. JLabel lblUsuarioQueSe = new JLabel("Usuario que se usar\u00E1 para el login.");
  203. lblUsuarioQueSe.setBounds(238, 96, 260, 14);
  204. contentPane.add(lblUsuarioQueSe);
  205.  
  206.  
  207. String texto = "<html><body>Nombre y apellido.<br>(Separado por un espacio. Con iniciales may\u00FAscula).</body></html>";
  208. JLabel lblNombreYApellido = new JLabel(texto);
  209. lblNombreYApellido.setBounds(238, 113, 301, 62);
  210. contentPane.add(lblNombreYApellido);
  211.  
  212. JLabel lblTelfonoDeContacto = new JLabel("Tel\u00E9fono de contacto.");
  213. lblTelfonoDeContacto.setBounds(238, 189, 200, 14);
  214. contentPane.add(lblTelfonoDeContacto);
  215.  
  216. JLabel lblPasDeResidencia = new JLabel("Pa\u00EDs de residencia.");
  217. lblPasDeResidencia.setBounds(238, 220, 154, 14);
  218. contentPane.add(lblPasDeResidencia);
  219.  
  220. JLabel lblCorreoElectrnicoPara = new JLabel("Correo electr\u00F3nico para el contacto.");
  221. lblCorreoElectrnicoPara.setBounds(294, 251, 235, 14);
  222. contentPane.add(lblCorreoElectrnicoPara);
  223.  
  224. JButton btnregistrarme = new JButton("\u00A1Registrarme!");
  225. btnregistrarme.addActionListener(new ActionListener() {
  226. public void actionPerformed(ActionEvent arg0) {
  227.  
  228. String usuario = tfUsuario.getText();
  229. System.out.println(usuario);
  230.  
  231. String nombre = tfNombre.getText();
  232. System.out.println(nombre);
  233.  
  234. char[] pass = tfContraseña.getPassword();
  235. System.out.println(pass);
  236.  
  237. String telefono = tfTelefono.getText();
  238. System.out.println(telefono);
  239.  
  240. String pais = (String)comboboxPais.getSelectedItem();
  241. String value = comboboxPais.getSelectedItem().toString();
  242. System.out.println(pais);
  243.  
  244. String email = tfEmail.getText();
  245. System.out.println(email);
  246.  
  247. EmailValidator emailValidator = new EmailValidator();
  248. if(!emailValidator.validate(tfEmail.getText().trim())) {
  249. System.out.print("Invalid Email ID");
  250. }else{
  251. EmailValido = 1;
  252. }
  253.  
  254. if(usuario.isEmpty() || nombre.isEmpty() || telefono.isEmpty() || email.isEmpty() || pais==" " || pass.length == 0 || EmailValido == 0){
  255. JOptionPane.showMessageDialog(null, "Completa todos los campos.");
  256. campos = 0;
  257. } else {
  258. JOptionPane.showMessageDialog(null, "Registrado con éxito.");
  259. EmailValido = 0;
  260.  
  261. tfUsuario.setText("");
  262. tfNombre.setText("");
  263. tfContraseña.setText("");
  264. tfTelefono.setText("");
  265. comboboxPais.setSelectedIndex(0);
  266. tfEmail.setText("");
  267.  
  268.  
  269. try {
  270. java.sql.Statement query1 = connection.createStatement();
  271. ResultSet rs = query1.executeQuery(" insert into usuarios (ID, Usuario, Password, Nombre, Rango,Telefono,email,Pais)"
  272. + " values (0, 1, 2, 3, 4, 5, 6, 7, 8)");
  273.  
  274. } catch (Exception e) {
  275. e.printStackTrace();
  276. }
  277.  
  278. }
  279.  
  280.  
  281. }
  282. });
  283. btnregistrarme.setIcon(new ImageIcon("C:\\Users\\Gonzalo\\Desktop\\Programaci\u00F3n\\eclipse\\configuration\\org.eclipse.osgi\\218\\0\\.cp\\icons\\full\\obj16\\correction_rename.png"));
  284. btnregistrarme.setBounds(201, 310, 147, 23);
  285. contentPane.add(btnregistrarme);
  286. }
  287. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement