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)));
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);
}
}
}
}
}
Archivo Conexión:
import java.awt.HeadlessException; import java.sql.*; import javax.swing.JOptionPane;
public class conexion {Archivo Conexión:
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
}
}
}
}