Advertisement
Guest User

Untitled

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