finishing up a DB/Java exercise
so i feel like im almost there, just not understanding the final steps, could someone let me know what i need to do to finish this program up? also as a side note, how do i post my code here WITH line numbers to make it easy for you guys?
this program primarily i worked from the
private void stockInfoJButtonActionPerformed( ActionEvent event ) line onward, and im having trouble understand what i need to do to get it finished up. i think im ok up until the "computetotalvalue method, but im starting to not understand exactly what i should be doing in that method. right now when i compile i get a "missing return statement", but there are probably additional issues as well. the DB is called "stockInformation" and contains stockName, stockSymbol, shares, and price fields, i am trying to access those, and also show an additional field in the java program that calculates the # of shares times the price and displays the total.....
Code:
// Exercise 26.11: StockPortfolio.java
// Displays a client's stock portfolio.
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.text.*;
import javax.swing.*;
public class StockPortfolio extends JFrame
{
// JLabels for prompt
private JLabel prompt1JLabel;
private JLabel prompt2JLabel;
// JComboBox for stock names
private JComboBox stockNamesJComboBox;
// JButton for stock information
private JButton stockInfoJButton;
// JPanel for displaying stock information
private JPanel stockInfoJPanel;
// JLabel and JTextField for stock name
private JLabel stockNameJLabel;
private JTextField stockNameJTextField;
// JLabel and JTextField for stock symbol
private JLabel stockSymbolJLabel;
private JTextField stockSymbolJTextField;
// JLabel and JTextField for number of shares
private JLabel sharesJLabel;
private JTextField sharesJTextField;
// JLabel and JTextField for price of shares
private JLabel priceJLabel;
private JTextField priceJTextField;
// JLabel and JTextField for total value
private JLabel totalJLabel;
private JTextField totalJTextField;
// instance variables for database processing
private Connection myConnection;
private Statement myStatement;
private ResultSet myResultSet;
// constructor
public StockPortfolio( String databaseDriver, String databaseURL )
{
// establish a connection to db
try
{
Class.forName ( databaseDriver );
// connect to a database
myConnection =
DriverManager.getConnection (databaseURL );
// create Statement for executing SQL
myStatement = myConnection.createStatement();
}
catch ( SQLException exception )
{
exception.printStackTrace();
}
catch ( ClassNotFoundException exception )
{
exception.printStackTrace();
}
createUserInterface(); // set up GUI
} // end constructor
// create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null );
// set up prompt1JLlabel
prompt1JLabel = new JLabel();
prompt1JLabel.setBounds( 8, 16, 350, 16 );
prompt1JLabel.setText( "Select the name of " +
"the stock for which you want " );
contentPane.add( prompt1JLabel );
// set up prompt2JLabel
prompt2JLabel = new JLabel();
prompt2JLabel.setBounds( 8, 32, 350, 16 );
prompt2JLabel.setText( "information, and then " +
"press the Stock Information button." );
contentPane.add( prompt2JLabel );
// set up stockNamesJComboBox
stockNamesJComboBox = new JComboBox();
stockNamesJComboBox.setBounds( 76, 65, 200, 26 );
stockNamesJComboBox.addItem( "" );
contentPane.add( stockNamesJComboBox );
// load stock names into stockNamesJComboBox
loadStockNames();
// set up stockInfoJButton
stockInfoJButton = new JButton();
stockInfoJButton.setBounds( 100, 100, 150, 23 );
stockInfoJButton.setText( "Stock Information" );
contentPane.add( stockInfoJButton );
stockInfoJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when stockInfoJButton is clicked
public void actionPerformed( ActionEvent event )
{
stockInfoJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end addActionLstener
// set up stockInfoJPanel
stockInfoJPanel = new JPanel();
stockInfoJPanel.setBounds( 18, 145, 300, 192 );
stockInfoJPanel.setLayout( null );
stockInfoJPanel.setBorder( BorderFactory.createTitledBorder(
BorderFactory.createEtchedBorder(), "Stock Info" ) );
contentPane.add( stockInfoJPanel );
// set up stockNameJLabel
stockNameJLabel = new JLabel();
stockNameJLabel.setBounds( 8, 24, 150, 23 );
stockNameJLabel.setText( "Stock name:" );
stockInfoJPanel.add( stockNameJLabel );
// set up stockNameJTextField
stockNameJTextField = new JTextField();
stockNameJTextField.setBounds( 158, 24, 125, 24 );
stockNameJTextField.setEditable( false );
stockNameJTextField.setBorder(
BorderFactory.createLoweredBevelBorder() );
stockNameJTextField.setHorizontalAlignment(
JTextField.CENTER );
stockInfoJPanel.add( stockNameJTextField );
// set up stockSymbolJLabel
stockSymbolJLabel = new JLabel();
stockSymbolJLabel.setBounds( 8, 56, 150, 23 );
stockSymbolJLabel.setText( "Stock symbol:" );
stockInfoJPanel.add( stockSymbolJLabel );
// set up stockSymbolJTextField
stockSymbolJTextField = new JTextField();
stockSymbolJTextField.setBounds( 158, 56, 125, 24 );
stockSymbolJTextField.setEditable( false );
stockSymbolJTextField.setBorder(
BorderFactory.createLoweredBevelBorder() );
stockSymbolJTextField.setHorizontalAlignment(
JTextField.CENTER );
stockInfoJPanel.add( stockSymbolJTextField );
// set up sharesJLabel
sharesJLabel = new JLabel();
sharesJLabel.setBounds( 8, 88, 150, 23 );
sharesJLabel.setText( "Number of shares:" );
stockInfoJPanel.add( sharesJLabel );
// set up sharesJTextField
sharesJTextField = new JTextField();
sharesJTextField.setBounds( 158, 88, 125, 24 );
sharesJTextField.setEditable( false );
sharesJTextField.setBorder(
BorderFactory.createLoweredBevelBorder() );
sharesJTextField.setHorizontalAlignment( JTextField.CENTER );
stockInfoJPanel.add( sharesJTextField );
// set up priceJLabel
priceJLabel = new JLabel();
priceJLabel.setBounds( 8, 120, 150, 23 );
priceJLabel.setText( "Price per share:" );
stockInfoJPanel.add( priceJLabel );
// set up priceJTextField
priceJTextField = new JTextField();
priceJTextField.setBounds( 158, 120, 125, 24 );
priceJTextField.setEditable( false );
priceJTextField.setBorder(
BorderFactory.createLoweredBevelBorder() );
priceJTextField.setHorizontalAlignment( JTextField.CENTER );
stockInfoJPanel.add( priceJTextField );
// set up totalJLabel
totalJLabel = new JLabel();
totalJLabel.setBounds( 8, 152, 150, 23 );
totalJLabel.setText( "Total value:" );
stockInfoJPanel.add( totalJLabel );
// set up totalJTextField
totalJTextField = new JTextField();
totalJTextField.setBounds( 158, 152, 125, 24 );
totalJTextField.setEditable( false );
totalJTextField.setBorder(
BorderFactory.createLoweredBevelBorder() );
totalJTextField.setHorizontalAlignment( JTextField.CENTER );
stockInfoJPanel.add( totalJTextField );
// ensure database connection is closed
// when user quits application
addWindowListener(
new WindowAdapter() // anonymous inner class
{
public void windowClosing( WindowEvent event )
{
frameWindowClosing( event );
}
} // end anonymous inner class
); // end addWindowListener
// set properties of application's window
setTitle( "Stock Portfolio" ); // set title bar string
setSize( 350, 380 ); // set window size
setVisible( true ); // display window
} // end method createUserInterface
// load stock names into stockNamesJComboBox
private void loadStockNames()
{
// add stock names in database to stockNamesJComboBox
try
{
myResultSet = myStatement.executeQuery(
"SELECT stockNames FROM stockInformation" );
// add stockNames to stockNamesJComboBox
while ( myResultSet.next() )
{
stockNamesJComboBox.addItem(
myResultSet.getString ( "stockNames" ) );
}
myResultSet.close(); // close myResultSet
}// end try
catch ( SQLException exception)
{
exception.printStackTrace();
}
} // end method loadStockNames
// user clicked stockInfoJButton
private void stockInfoJButtonActionPerformed( ActionEvent event )
{
// get selected stock
String stockName =
( String ) stockNamesJComboBox.getSelectedItem();
//display stock data
displayStockData ( stockName );
} // end method stockInfoJButtonActionPerformed
//display stock information
private void displayStockData ( String stockName )
{
//display stock name, stock symbol, shares and price
try
{
//get stock name, stock symbol and shares
myResultSet = myStatement.executeQuery( "SELECT stockSymbol,"
+ " shares, price FROM stockInformation WHERE " +
"stockName = " + ( stockName ) );
// non empty result
if (myResultSet.next () )
{
stockNameJTextField.setText(
myResultSet.getString( "stockName" ) );
stockSymbolJTextField.setText(
myResultSet.getString( "stockSymbol" ) );
sharesJTextField.setText(
myResultSet.getString( "shares" ) );
priceJTextField.setText(
myResultSet.getString( "price" ) );
}
myResultSet.close(); // close myResultSet
}
catch ( SQLException exception )
{
exception.printStackTrace();
}
}// end method display stock data
// calculate total value
private String computeTotalValue( int shares, double price )
{
try
{
//get shares and price
shares = myResultSet.getInt( "shares" );
price = myResultSet.getDouble( "price" );
// define display format
DecimalFormat dollars = new DecimalFormat( "$0.00" );
return dollars.format( shares * price );
}
catch ( SQLException exception )
{
exception.printStackTrace();
}
} // end method computeTotalValue
// user close window
private void frameWindowClosing( WindowEvent event )
{
// close myStatement and database connection
try
{
myStatement.close();
myConnection.close();
}
catch ( SQLException sqlException )
{
sqlException.printStackTrace();
}
finally
{
System.exit( 0 );
}
} // end method frameWindowClosing
// method main
public static void main( String[] args )
{
// check command-line arguments
if ( args.length == 2 )
{
// get command-line arguments
String databaseDriver = args[ 0 ];
String databaseURL = args[ 1 ];
// create new StockPortfolio
StockPortfolio application =
new StockPortfolio( databaseDriver, databaseURL );
}
else
{
System.out.println( "Usage: java StockPortfolio " +
"databaseDriver databaseURL" );
}
} // end method main
} // end class StockPortfolio
im guessing im starting to go wrong around line 310, but this is my first week with DB's so basic explanations are beneficial!