Good afternoon someone could help me with the following please ... I want to obtain the ID of the rows obtained by the following SQL query and save them in a variable but I do not know how to do it.
SELECT * FROM productos;
But if I know how to obtain the columns of a specific row and save them in a variable, the code I use to obtain the columns is as follows:
Connection con;
//Obtenemos datos de tabla producto
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/susyken", "root", "");
String sentencia = "SELECT * FROM producto WHERE Id_Producto="+codigo+";";
Statement stmt = null; //Esto sustituirá a ps
stmt = con.createStatement(); //Usamos create... no prepare...
ResultSet rs = stmt.executeQuery(sentencia);
while (rs.next()) {
idProducto = rs.getString(1); //Si codigo es del tipo numérico en la BD debes usar getInt
nombreProducto = rs.getString(2);
existencia = rs.getString(3);
precio = rs.getString(4);
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, "Error");
}
Could someone help me know how to do it?