I have a vehicle sales application. In the MySQL database I have purchased vehicles (for customers) and vehicles not yet purchased (available for purchase).
In the database I distinguish the tables: vehicles and vehicles_buying.
Organization chart of the database | Model E-R: link
It turns out that in a JFrame
or Java application or Java Swing I show a technical sheet of the vehicles that I have in the table compra_compra (both purchased and not purchased).
Image of purchased vehicles: link
Image of vehicles not purchased: link
As you can see I missed the error: " java.sql.SQLException: Illegal operation on empty result set.
", this is because it tries to load the client's data but since it is not bought that vehicle does not exist and can not be loaded.
How could I leave those customer data "null" and load the vehicle's data?
Any questions I am willing to comment on. I leave code to be more useful.
Code:
public static ArrayList ficha_tecnica_compra() {
//Al ser la ficha técnica sólo de un bastidor, guardamos los datos en un ArrayList.
ArrayList<String> bastidores = new ArrayList<String>();
String bd = Conexiones.bbdd;
Connection c = (Connection) Conexiones.conexion_a_BBDD(bd);
Statement stm;
ResultSet rs;
try {
//Consulta para sacar todos los bastidores.
stm = c.createStatement();
String consulta_bastidores = "SELECT bastidor FROM vehiculos_compra;";
rs = stm.executeQuery(consulta_bastidores);
//System.out.println("CONSULTA TODOS BASTIDORES: Mostramos todos bastidores de la tabla 'vehiculos_compra'.\n");
int i = 0;
while (rs.next()) {
String bastidor = rs.getString("bastidor");
bastidores.add(bastidor);
i++;
}
c.close();
} catch (SQLException e) {
e.printStackTrace();
}
return bastidores;
}
public static Object[] datos_de_un_bastidor_compra(String bastidor) {
//Creamos un objeto "v" de 11 valores: marca, modelo, matricula, fecha_v_compra, nombre, apellidos, dni, tlf_contacto, direccion, ciudad, email.
Object[] v = new Object[11];
String bd = Conexiones.bbdd;
Connection c = (Connection) Conexiones.conexion_a_BBDD(bd);
Statement stm;
ResultSet rs;
try {
//Consulta para sacar todos los datos del "vehiculos_compra" de un bastidor.
stm = c.createStatement();
String consulta_de_un_bastidor = "SELECT v.marca, v.modelo, v.matricula, c.*, vc.fecha_v_compra, m.nombre \n" +
"FROM clientes_compra AS cc, vehiculos AS v, vehiculos_compra AS vc, clientes AS c, marca AS m\n" +
"WHERE cc.ID_VEHICULO='" + bastidor + "' AND v.bastidor=cc.ID_VEHICULO AND c.DNI=cc.DNI AND m.ID=v.marca;";
rs = stm.executeQuery(consulta_de_un_bastidor);
//System.out.println("CONSULTA DATOS: Datos de un bastidor en concreto.\n");
rs.next();
//Guardamos los valores extraidos de la consulta en cada tipo de dato específico (variables).
String marca = rs.getString("m.nombre");
String modelo = rs.getString("modelo");
String matricula = rs.getString("matricula");
Date fecha_v_compra = rs.getDate("fecha_v_compra");
String nombre = rs.getString("c.nombre");
String apellidos = rs.getString("apellidos");
String dni = rs.getString("dni");
String tlf_contacto = rs.getString("tlf_contacto");
String direccion = rs.getString("direccion");
String ciudad = rs.getString("ciudad");
String email = rs.getString("email");
//Volcamos esas variables en el objeto "v".
v[0] = marca;
v[1] = modelo;
v[2] = matricula;
v[3] = fecha_v_compra;
v[4] = nombre;
v[5] = apellidos;
v[6] = dni;
v[7] = tlf_contacto;
v[8] = direccion;
v[9] = ciudad;
v[10] = email;
c.close();
} catch (SQLException e) {
e.printStackTrace();
}
//Devolvemos el objeto "v".
return v;
}
When changing the frame (the charges in a combobox, the remaining data of that frame is updated, it is the primary key of all the tables). That's why the following function (event):
private void jComboBox_bastidorItemStateChanged(java.awt.event.ItemEvent evt) {
//Recogemos en la variable "bastidor" el valor seleccionado en el combobox con el método "getSelectedItem()".
String bastidor = (String) jComboBox_bastidor.getSelectedItem();
//Guardamos en un objecto "v" los datos del bastidor gracias a la función "Conexiones.datos_de_un_bastidor_compra(bastidor);".
Object[] v = Conexiones.datos_de_un_bastidor_compra(bastidor);
//Datos del coche --> Otorgamos a los campos jTextField los valores recogidos en el "Object[] v".
textfield_marca.setText(String.valueOf(v[0]));
textfield_marca.setEditable(false);
textfield_modelo.setText(String.valueOf(v[1]));
textfield_modelo.setEditable(false);
textfield_matricula.setText(String.valueOf(v[2]));
textfield_matricula.setEditable(false);
textfield_fechacompra.setText(String.valueOf(v[3]));
textfield_fechacompra.setEditable(false);
//Datos del cliente --> Otorgamos a los campos jTextField los valores recogidos en el "Object[] v".
textfield_nombre.setText(String.valueOf(v[4]));
textfield_nombre.setEditable(false);
textfield_apellidos.setText(String.valueOf(v[5]));
textfield_apellidos.setEditable(false);
textfield_DNI.setText(String.valueOf(v[6]));
textfield_DNI.setEditable(false);
textfield_tlf_contacto.setText(String.valueOf(v[7]));
textfield_tlf_contacto.setEditable(false);
textfield_direccion.setText(String.valueOf(v[8]));
textfield_direccion.setEditable(false);
textfield_ciudad.setText(String.valueOf(v[9]));
textfield_ciudad.setEditable(false);
textfield_email.setText(String.valueOf(v[10]));
textfield_email.setEditable(false);
//Imagen del coche.
label_foto.setIcon(new ImageIcon("..\imagenes\"+String.valueOf(v[1])+".png"));
}