PreparedStatement does not insert data

0

Good afternoon: I request the collaboration to solve the following problem that I have with two files in java, one is called test and the other connections, the test file has a table that brings the data from the mysql database and with a click to the button I insert them in Some Textfield that are inside the same test file, which must have the data to be registered in another table, but it asks me create a preparedStatement in the connection file. Here are the codes of the two files, test and connection.

Test:

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
    // TODO add your handling code here:
    int seleccion = jTable1.rowAtPoint(evt.getPoint());
    //codigo.setText(String.valueOf(jTable1.getValueAt(seleccion, 0)));
    //horaVoto.setText(String.valueOf(jTable1.getValueAt(seleccion, 1)));
    //estudiante.setText(String.valueOf(jTable1.getValueAt(seleccion, 2)));
    codigoCandidato.setText(String.valueOf(jTable1.getValueAt(seleccion, 3)));

    int column = jTable1.getColumnModel().getColumnIndexAtX(evt.getX());
    int row = evt.getY()/jTable1.getRowHeight();

    if(row < jTable1.getRowCount() && row>=0 && column < jTable1.getColumnCount() && column >=0){
        Object value = jTable1.getValueAt(row, column);

    if(value instanceof JButton){
            ((JButton)value).doClick();
            JButton boton = (JButton) value;
            System.out.println("Click");


        if(boton.getName().equals("R")){
            System.out.println("Boton Insertar");

          try{
                JOptionPane.showMessageDialog(null,"Voto Realizado");
                String query = "insert into voto(codigoVoto,horaVoto,codigoEstudiante,codigoCandidato)values(?,?,?,?)";
                PreparedStatement a=conexion.prepareStatement(query);
                a.setString(1,codigo.getText());
                a.setString(2,horaVoto.getText());
                a.setString(3,estudiante.getText());
                a.setString(4,codigoCandidato.getText());
                a.executeUpdate();
                JOptionPane.showMessageDialog(null,"Su voto fue registrado");

            }
    catch(HeadlessException | SQLException e){
        JOptionPane.showMessageDialog(null,e);
    }


        }

        }
    }
}                                    

Connection File:

import java.awt.HeadlessException;
import java.sql.*;
import javax.swing.JOptionPane;


public class conexion {

static PreparedStatement prepareStatement(String query) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

}

public String db="votsi";
public String Url="Jdbc:mysql://localhost:3306/"+db;
public String User="root";
public String Clave="";

public conexion(){

}

public Connection conectar(){
    Connection link=null;
    try{
        Class.forName("org.gjt.mm.mysql.Driver");
        link=DriverManager.getConnection(this.Url,this.User,this.Clave);
    }
    catch(ClassNotFoundException | SQLException e){
        JOptionPane.showMessageDialog(null,e);

    }
    return link
   }   
 }
    
asked by Carlos Herrera 07.02.2018 в 20:08
source

1 answer

0

When you talk to:

conexion.prepareStatement

You are calling the static method that you declared in the "connection" class and from what I see that method does nothing. That's why the data is not passed.

In this case what you have to do is prepare the statement in "a real connection", by real connection I am referring to an object of type "Connection".

Using your "connection" class, what you could do is create a "Connection" object inside your ActionListener with the help of your "connect" method. Create the connection would look like this:

conexion InstanciaDeTuClase = new conexion(); /*Creas la instancia para poder llamar 
al metodo "conectar"*/

Connection conexionDeVerdad = InstanciaDeTuClase.conectar(); /*Ahora creas una 
instancia de la clase "Connection" con ayuda de tu método "conectar" lo cual 
significa que "conexionDeVerdad" tomara los valores de la variable "link" 
que regresa tu método "conectar".*/

And now you would prepare the statement with the help of your "truth connection"

PreparedStatement a = conexionDeVerdad.prepareStatement(query);

If you do it this way, your code should work.

    
answered by 08.02.2018 в 07:58