Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public DialogoInserccion(String[] columnas, Object[] filas, String tabla, String BD){
- super((JFrame) null, true);
- this.filas = filas;
- this.columnas = columnas;
- this.tabla = tabla;
- this.BD = BD;
- this.conexion = conexion;
- etiquetas = new JLabel[columnas.length];
- campos = new JTextField[columnas.length];
- for(int i=0; i<columnas.length;i++){
- JLabel etiqueta = new JLabel(columnas[i]);
- JTextField campo = new JTextField();
- etiquetas[i] = etiqueta;
- campos[i] = campo;
- this.add(etiqueta);
- this.add(campo);
- }
- btInsertar = new JButton("Insertar");
- this.add(btInsertar);
- btInsertar.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- MySQL.conecta("jdbc:mysql://localhost:3306/" + BD, "pepe", "pepa");
- PreparedStatement prst = null;
- for(int i=0; i<etiquetas.length;i++){
- // System.out.println(etiquetas[i].getText());
- }
- MySQL.ejecutaConsulta("SELECT * FROM " + tabla);
- ResultSet rs = MySQL.getRs();
- String[] columnas = new String[rs.getMetaData().getColumnCount()];
- for(int i=0; i<columnas.length;i++){
- columnas[i] = rs.getMetaData().getColumnName(i);
- System.out.println("columnas: " + columnas[i]);
- }
- for(int i=0; i<columnas.length;i++){
- MySQL.ejecutaConsultaAccion("INSERT INTO " +tabla+"("+columnas[i]+")" +
- " VALUES("+campos[i].getText()+")");
- System.out.println("Tabla: " + columnas[i]);
- }
- } catch (ClassNotFoundException ex) {
- JOptionPane.showMessageDialog(null, "Clase no encontrada");
- } catch (SQLException ex) {
- JOptionPane.showMessageDialog(null, "Problema al ejecutar la consulta en diálogo " + ex.getMessage());
- }
- }
- });
- this.setLayout(new GridLayout(0, 2, 0, 10));
- pack();
- this.setVisible(true);
- }
- btInsertar.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- try {
- MySQL.conecta("jdbc:mysql://localhost:3306/" + BD, "pepe", "pepa");
- MySQL.ejecutaConsulta("SELECT * FROM " + tabla);
- for(int i=0; i<campos.length; i++){
- MySQL.ejecutaConsultaAccion("INSERT INTO "+ tabla +"("+etiquetas[i].getText()+")" +
- " values("+campos[i].getText()+")");
- }
- } catch (ClassNotFoundException ex) {
- JOptionPane.showMessageDialog(null, "Clase no encontrada");
- } catch (SQLException ex) {
- System.out.println(ex.getMessage() + "n");
- }
- }
- });
Add Comment
Please, Sign In to add comment