JButton to connect to a BD

0
JButton Conectar = new JButton("Conectar");

in my main class.

I would like that button to execute another class that I have in the package.

SaveDB inside contains the Connection method.

I tried this but it does not work.

GuardarDB db = new GuardarDB();
Conectar.addActionListener(db);

Then I tried the derivatives that netbeans gave me as "correction" but none worked.

package tallereslaborales;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


public class GuardarDB {

    static Connection con = null;

    static String url = "jdbc:mysql://jorgemartini.u-host.cl:3306/jorgemar_vilcun";
    static String user = "";
    static String pass = "";


    public static Connection Conexion(){
        PreparedStatement psInsertar;
        Statement stmmt;

        try {
            Class.forName("com.mysql.jdbc.Driver");

            try {
                Socias personas = new Socias();
                System.out.println(personas.getNombre());
                con = DriverManager.getConnection(url,user,pass);
                JOptionPane.showMessageDialog(null, "¡Conexión exitosa a la BD!");

                psInsertar = con.prepareStatement("INSERT INTO talleres (nombre,rut,taller,telefono,sector)" + " values(?,?,?,?,?)");                

                psInsertar.setString(1,personas.getNombre());
                psInsertar.setString(2,String.valueOf(personas.getNumero()));
                psInsertar.setString(3,personas.getTaller());
                psInsertar.setString(4,String.valueOf(personas.getNumero()));
                psInsertar.setString(5,personas.getTaller());

                psInsertar.executeUpdate();

            } catch (SQLException ex) {
                Logger.getLogger(GuardarDB.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(null, "NO HAY CONEXIÓN EN LA BD! COMUNIQUESE CON UN ADMINISTRADOR. " + "\n" + "          [email protected]");
            }


        } catch (ClassNotFoundException ex) {
            Logger.getLogger(GuardarDB.class.getName()).log(Level.SEVERE, null, ex);
        }

        return con;
    }


}
    
asked by Jorge Martini Schwenke 25.09.2017 в 04:49
source

1 answer

0

When doing

Conectar.addActionListener(db);

You're passing him a class and you should not pass that on to him. You can do the following:

Conectar.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent e) { 
        //Aquí es donde llamas al método de la otra clase.
        //Depende como lo hayas creado tendrás que crear un objeto del tipo de la clase y llamar a su método
        // o si es estático con poner el nombre de la clase y el del método vale
    } 
} );

What is not clear is where you say "I would like that button to execute another class that I have in the package".

The classes do not run, you can create objects of that type or call methods of that class (the methods do run).

    
answered by 25.09.2017 / 15:04
source