Java SQL Statement for selecting record from database and using as double variable <p>Java - Netbeans IDE</p> <p>I have this code but the variable Priceperitem is not found, can anyone explain? or show me an easier way of selecting a record from a table in a database and setting its value as a variable?</p> <p>Price P/Item is the name of column within the database table.</p> <pre><code> String sql = "SELECT Price P/Item FROM tblResources"; try { pst = conn.prepareStatement(sql); rs = pst.executeQuery(); while (rs.next()){ double Priceperitem = rs.getDouble("Price P/Item"); } } catch (Exception e) { JOptionPane.showMessageDialog(null, "Runtime Error"); } </code></pre> <p>and then :</p> <pre><code>try { Total = Quantity * Priceperitem; (This is where Priceperitem is not found.) btnCalculateTotal.setText("Total: £"+Total+"0"); }catch (Exception e){ System.out.println("Error Calculating Total"); } </code></pre>