Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.prog.ud4;
- import javax.swing.JFrame;
- import java.awt.GridBagLayout;
- import java.awt.GridLayout;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import java.sql.*;
- import java.awt.BorderLayout;
- import java.awt.FlowLayout;
- import java.awt.GridBagConstraints;
- import java.awt.Insets;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JTextField;
- import javax.swing.JComboBox;
- import javax.swing.DefaultComboBoxModel;
- import javax.swing.JButton;
- public class altaCorredor {
- public altaCorredor(){
- JFrame frame = new JFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setTitle("Alta Corredor");
- JPanel panel1 = new JPanel(new GridLayout(6,0,0,0));
- JPanel panel2 = new JPanel(new GridLayout(6,1,0,0));
- JPanel buttons_panel = new JPanel(new FlowLayout());
- JLabel etiqueta1 = new JLabel("Nombre");
- JLabel etiqueta2 = new JLabel("Edad");
- JLabel etiqueta3 = new JLabel("Ciudad");
- JLabel etiqueta4 = new JLabel("Club");
- JTextField texto1 = new JTextField(50);
- JTextField texto2 = new JTextField(2);
- JTextField texto3 = new JTextField(50);
- JComboBox<String> combo = new JComboBox<String>();
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection ("jdbc:mysql://localhost/prog","root", "");
- Statement st = con.createStatement();
- ResultSet rs = st.executeQuery("Select nombre from club");
- while(rs.next()){
- String clubs=rs.getString("nombre");
- combo.addItem(clubs);
- }
- con.close();
- }
- catch(Exception e){};
- JButton aceptar = new JButton("Aceptar");
- panel1.add(etiqueta1);
- panel2.add(texto1);
- panel1.add(etiqueta2);
- panel2.add(texto2);
- panel1.add(etiqueta3);
- panel2.add(texto3);
- panel1.add(etiqueta4);
- panel2.add(combo);
- buttons_panel.add(aceptar);
- aceptar.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String nombre = texto1.getText().toString();
- String edad = texto2.getText().toString();
- String ciudad = texto3.getText().toString();
- String club = combo.getSelectedItem().toString();
- try {
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection ("jdbc:mysql://localhost/prog","root", "");
- PreparedStatement pst = con.prepareStatement("insert into prog.corredor (nombre,edad,ciudad,club) values(?,?,?,?)");
- pst.setString(1, nombre);
- pst.setString(2, edad);
- pst.setString(3, ciudad);
- pst.setString(4, club);
- pst.executeUpdate();
- con.close();
- }
- catch(Exception ex){};
- }
- });
- frame.getContentPane().add(panel1, BorderLayout.WEST);
- frame.getContentPane().add(panel2, BorderLayout.EAST);
- frame.getContentPane().add(buttons_panel, BorderLayout.PAGE_END);
- frame.pack();
- frame.setLocation(350,450);
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement