Advertisement
Guest User

quersio altacorredor

a guest
Jun 7th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. package com.prog.ud4;
  2.  
  3. import javax.swing.JFrame;
  4.  
  5. import java.awt.GridBagLayout;
  6. import java.awt.GridLayout;
  7.  
  8. import javax.swing.JLabel;
  9. import javax.swing.JPanel;
  10.  
  11. import java.sql.*;
  12. import java.awt.BorderLayout;
  13. import java.awt.FlowLayout;
  14. import java.awt.GridBagConstraints;
  15. import java.awt.Insets;
  16. import java.awt.event.ActionEvent;
  17. import java.awt.event.ActionListener;
  18.  
  19. import javax.swing.JTextField;
  20. import javax.swing.JComboBox;
  21. import javax.swing.DefaultComboBoxModel;
  22. import javax.swing.JButton;
  23.  
  24. public class altaCorredor {
  25.  
  26. public altaCorredor(){
  27.  
  28.  
  29. JFrame frame = new JFrame();
  30. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31. frame.setTitle("Alta Corredor");
  32.  
  33. JPanel panel1 = new JPanel(new GridLayout(6,0,0,0));
  34. JPanel panel2 = new JPanel(new GridLayout(6,1,0,0));
  35. JPanel buttons_panel = new JPanel(new FlowLayout());
  36.  
  37. JLabel etiqueta1 = new JLabel("Nombre");
  38. JLabel etiqueta2 = new JLabel("Edad");
  39. JLabel etiqueta3 = new JLabel("Ciudad");
  40. JLabel etiqueta4 = new JLabel("Club");
  41. JTextField texto1 = new JTextField(50);
  42. JTextField texto2 = new JTextField(2);
  43. JTextField texto3 = new JTextField(50);
  44.  
  45. JComboBox<String> combo = new JComboBox<String>();
  46. try {
  47. Class.forName("com.mysql.jdbc.Driver");
  48. Connection con = DriverManager.getConnection ("jdbc:mysql://localhost/prog","root", "");
  49. Statement st = con.createStatement();
  50. ResultSet rs = st.executeQuery("Select nombre from club");
  51. while(rs.next()){
  52. String clubs=rs.getString("nombre");
  53. combo.addItem(clubs);
  54.  
  55. }
  56. con.close();
  57. }
  58. catch(Exception e){};
  59.  
  60. JButton aceptar = new JButton("Aceptar");
  61.  
  62. panel1.add(etiqueta1);
  63. panel2.add(texto1);
  64. panel1.add(etiqueta2);
  65. panel2.add(texto2);
  66. panel1.add(etiqueta3);
  67. panel2.add(texto3);
  68. panel1.add(etiqueta4);
  69. panel2.add(combo);
  70.  
  71. buttons_panel.add(aceptar);
  72. aceptar.addActionListener(new ActionListener() {
  73. public void actionPerformed(ActionEvent e) {
  74. String nombre = texto1.getText().toString();
  75. String edad = texto2.getText().toString();
  76. String ciudad = texto3.getText().toString();
  77. String club = combo.getSelectedItem().toString();
  78.  
  79.  
  80.  
  81. try {
  82. Class.forName("com.mysql.jdbc.Driver");
  83. Connection con = DriverManager.getConnection ("jdbc:mysql://localhost/prog","root", "");
  84. PreparedStatement pst = con.prepareStatement("insert into prog.corredor (nombre,edad,ciudad,club) values(?,?,?,?)");
  85. pst.setString(1, nombre);
  86. pst.setString(2, edad);
  87. pst.setString(3, ciudad);
  88. pst.setString(4, club);
  89. pst.executeUpdate();
  90. con.close();
  91. }
  92.  
  93.  
  94. catch(Exception ex){};
  95.  
  96.  
  97.  
  98. }
  99. });
  100.  
  101. frame.getContentPane().add(panel1, BorderLayout.WEST);
  102. frame.getContentPane().add(panel2, BorderLayout.EAST);
  103. frame.getContentPane().add(buttons_panel, BorderLayout.PAGE_END);
  104.  
  105. frame.pack();
  106. frame.setLocation(350,450);
  107. frame.setVisible(true);
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement