?
Solved

For Orangehead911

Posted on 2003-03-03
28
Medium Priority
?
293 Views
Last Modified: 2010-03-31
Orangehead911

Thanks for the offer to help first of all.  This stuff is rather daunting when you have only been exposed to it for a few weeks and are expected to write this type of program without help.  So, thanks again.

Here is the situation.  The GUI works.  I think I also have most all of the components in place within the code to capture the information from my database (which is in MS Access 2000 on my own computer).

The expected outcome should be the following: The user should choose a product id from the dropdown menu and then click "Display ProductID Details".  After clicking the button, the details would populate the textfields 2-9.  The application should be pulling one table called "products" from the small database and presenting the user with all of the information about the product # they choose from the drop down list.  
I have altered the code just a bit from the code that you helped me with earlier.  I needed to get rid of one TextField box that wasn't doing anything.  So, here is what I have so far.  I am using JDeveloper to build this application if that helps (which I am also trying to learn how to use).

I am not sure if I am passing information correctly from the database and whether it is being overwritten by the textfields.

Here is the code, again the GUI works, just no data being pulled from the database:


package mypackage1;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JList;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import java.util.List;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import java.awt.Color;
import java.sql.*;
 
public class Frame1 extends JFrame
implements ActionListener
{
     Connection connection;
     
     //setting up the list of product numbers, buttons
     //text fields and labels for text fields
     private String items[] =
     {
          "1","2", "3", "4", "5", "6", "7",
          "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
          "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
          "29", "30", "31", "32", "33", "34", "35"
     };
     
      private int amount[] = {45,72,63,52,78,92,120,98,67};
      private JButton jButton1 = new JButton();
      private JLabel jLabel1 = new JLabel();
      private JTextField jTextField1 = new JTextField();
      private JLabel jLabel2 = new JLabel();
      private JLabel jLabel3 = new JLabel();
      private JLabel jLabel4 = new JLabel();
      private JLabel jLabel5 = new JLabel();
      private JLabel jLabel6 = new JLabel();
      private JLabel jLabel7 = new JLabel();
      private JLabel jLabel8 = new JLabel();
      private JTextField jTextField2 = new JTextField();
      private JTextField jTextField3 = new JTextField();
      private JTextField jTextField4 = new JTextField();
      private JTextField jTextField5 = new JTextField();
      private JTextField jTextField6 = new JTextField();
      private JTextField jTextField7 = new JTextField();
      private JTextField jTextField8 = new JTextField();
      private JTextField jTextField9 = new JTextField();
      private JLabel jLabel9 = new JLabel();
      private JComboBox jComboBox1 = new JComboBox(items);
      private JList jList1 = new JList();
     
      public Frame1()
      {
         try
          {
            jbInit();
            initDb();
            readDataFromDb();
          }
        catch(Exception e)
           {
            e.printStackTrace();
           }
      }
     
      public void jbInit()
      {
        //set the fields within GUI to display names of fields
        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(461, 321));
        this.setBackground(new Color(33, 41, 227));
        this.setForeground(Color.blue);
        jButton1.setText("Display ProductID Details");
        jButton1.setBounds(new Rectangle(0, 5, 185, 25));
        jTextField2.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          jTextField2_actionPerformed(e);
        }
      });
        jLabel9.setText("Product ID");
        jLabel9.setBounds(new Rectangle(195, 5, 75, 20));
        jComboBox1.setBounds(new Rectangle(15, 45, 124, 20));
        jLabel2.setText("Supplier ID");
        jLabel2.setBounds(new Rectangle(195, 35, 70, 15));
        jLabel3.setText("Product Name");
        jLabel3.setBounds(new Rectangle(175, 65, 135, 15));
        jLabel4.setText("Quantity Per Product");
        jLabel4.setBounds(new Rectangle(175, 90, 130, 20));
        jLabel5.setText("Price");
        jLabel5.setBounds(new Rectangle(175, 130, 110, 15));
        jLabel6.setText("Quantity in Stock");
        jLabel6.setBounds(new Rectangle(175, 160, 110, 20));
        jLabel7.setText("Quantity On Order");
        jLabel7.setBounds(new Rectangle(175, 200, 120, 15));
        jLabel8.setText("Re-Order Level");
        jLabel8.setBounds(new Rectangle(175, 235, 100, 15));
        jTextField2.setText("2");
        jTextField2.setBounds(new Rectangle(310, 30, 60, 20));
        jTextField9.setText("9");
        jTextField9.setBounds(new Rectangle(310, 5, 90, 20));
        jTextField3.setText("3");
        jTextField3.setBounds(new Rectangle(270, 60, 180, 20));
        jTextField4.setText("4");
        jTextField4.setBounds(new Rectangle(310, 90, 85, 20));
        jTextField5.setText("5");
        jTextField5.setBounds(new Rectangle(310, 125, 85, 20));
        jTextField6.setText("6");
        jTextField6.setBounds(new Rectangle(310, 160, 85, 20));
        jTextField7.setText("7");
        jTextField7.setBounds(new Rectangle(310, 195, 85, 20));
        jTextField8.setText("8");
        jTextField8.setBounds(new Rectangle(310, 230, 85, 20));
           
        this.getContentPane().add(jComboBox1, null);
        this.getContentPane().add(jLabel9, null);
           this.getContentPane().add(jTextField9, null);
           this.getContentPane().add(jTextField8, null);
           this.getContentPane().add(jTextField7, null);
           this.getContentPane().add(jTextField6, null);
           this.getContentPane().add(jTextField5, null);
           this.getContentPane().add(jTextField4, null);
           this.getContentPane().add(jTextField3, null);
           this.getContentPane().add(jTextField2, null);
           this.getContentPane().add(jLabel8, null);
           this.getContentPane().add(jLabel7, null);
           this.getContentPane().add(jLabel6, null);
           this.getContentPane().add(jLabel5, null);
           this.getContentPane().add(jLabel4, null);
           this.getContentPane().add(jLabel3, null);
           this.getContentPane().add(jLabel2, null);
           this.getContentPane().add(jButton1, null);
           this.getContentPane().add(jTextField1, null);
           jButton1.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          jButton1_actionPerformed(e);
        }
      });
 
       jList1.addListSelectionListener( new ListSelectionListener()
      {
        public void valueChanged(ListSelectionEvent listSelectionEvent)
        {
      }});}
 
 public void actionPerformed(ActionEvent e)
        {
          jButton1_actionPerformed(e);
        }
      public void get_quantity()
      {
           int list[] = jList1.getSelectedIndices();
           int i= list[0];
           jTextField2.setText(toString(amount[i]));
      }
        public void jButton1_actionPerformed(ActionEvent e)
      {
           get_quantity();
      }
         public String toString(int x)
      {
         return "Quantity is " + x + " ";
      }
         public void initDb()
     {
           try
           {
            // Load the database drivers.
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
           System.out.println("drivers loaded");
               
           // Obtain connection to the database.
               
          connection = DriverManager.getConnection("jdbc:odbc:TechInventory", "", "");
          System.out.println("Connection established");
               
          }
          catch (ClassNotFoundException cnf)
          {
           System.out.println("Could not find class: " + cnf);
          }
          catch (SQLException sqle)
          {
            System.out.println("SQL problem: " + sqle);
          }
     }
     
      public void readDataFromDb()
      {
         Statement statement;
         ResultSet rs;
         {
          try
           {
           // Create statement to read and write to/from database.
           statement = connection.createStatement();
           System.out.println("Statement created");
                     
           rs = statement.executeQuery("SELECT * FROM PRODUCTS");
                               
          // Get next row.
          while( rs.next() )
           {
            jTextField9.setText(rs.getString("ProductID"));
            jTextField2.setText(rs.getString("SupplierID"));
            jTextField3.setText(rs.getString("ProductName"));
            jTextField4.setText(rs.getString("QuantityPerProduct"));
            jTextField5.setText(rs.getString("Price"));
            jTextField6.setText(rs.getString("QuantityInStock"));
            jTextField7.setText(rs.getString("QuantityOnOrder"));
            jTextField8.setText(rs.getString("ReOrderLevel"));
           }
          // Close statement and connection.
      statement.close();
        }
      catch (SQLException sqle)
        {
           System.out.println("SQL problem: " + sqle);
        }
    }
}
     
     public void closeConnectionToDb()
     {
        try
        {
          connection.close();
        }
          catch (SQLException sqle)
          {
            System.out.println("SQL problem: " + sqle);
          }
     }
     
    public static void main(String args[])
    {
      Frame1 frame = new Frame1();
      frame.setVisible(true);
    }
 
  private void jTextField2_actionPerformed(ActionEvent e)
  {
  }
}


0
Comment
Question by:SunKosi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 17
  • 11
28 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 8061186
Please describe the database you're trying to access in terms of tables and references between those tables if you have more than one table? Please also describe the ODBC settings that you have to access the database from JDBC.
0
 

Author Comment

by:SunKosi
ID: 8061400
OK.  There are 5 tables in the TechInventory database.  They are Stores, Transfer Request, Products, Suppliers, and Purchase Orders.  I only need to access the one table "Products" at this time.  I must access all of the items in that table which consist of Product ID, Supplier ID, ProductName, QuantityPerProduct, Price, QuantityInStock, QuantityOnOrder, ReOrderLevel  These are the items already set up in the TextFields.  The products table Primary key is Product ID and it is connected to the Suppliers Table by the secondary key "Supplier ID".

The Products Table has 35 products in it with products 1-35 (which is my string at the beginning of the code).  You should, through the GUI, select the product number in the list, click the "Display ProductID Details" and the info of that product ID would be displayed.

The ODBC settings...I downloaded the database from my class' website and followed the instructions below.  

"To create an ODBC connection to an MS Access database open the "ODBC Data Source" control panel in Setting > Control Panel > Administrative Tools (on most Windows Machines)." I selected Microsoft Access Driver(*mdb), and named the driver TechInventory--the same as the database name.  

The rest of the code you see for the driver came from the java.sun.com site.  I just tried to use as much code from the examples as I could.  Hope that answers your questions.  Please let me know what else I can provide to help you help me.

Thanks

P.S.  I'm also a big ski fanatic (only been up 35 times this season).
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 8066382
>>  I must access all of the items in that table which consist of Product ID, Supplier ID, ProductName, QuantityPerProduct, Price, QuantityInStock, QuantityOnOrder, ReOrderLevel

I notice spaces in the name for 'Product ID' and 'Supplier ID', are there spaces in the field names in the database?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:SunKosi
ID: 8066467
There are no spaces in any of the column headings in the database.  Everything is one word.  Here is each item in each table of the DB just as it appears in each table.  The * denotes the connections (keys)

Stores          

StoreID * connects to TransferRequest table
StoreName
ManagerName
Address
City
State
PostalCode
Phone

TransferRequest

StoreID  *
RequestDate
ProductID * connects to Products table
QuantityRequired
ReceivedDate

Products

ProductID *
SupplierID * connects to Suppliers table
ProductName
QuantityPerProduct
Price
QuantityInStock
QuantityOnOrder
ReOrderLevel

Suppliers

SupplierID *  connects to PurchaseOrders table
CompanyName
ContactName
ContactTitle
Address
City
Region
PostalCode
Country
Phone
Fax

PurchaseOrders

POID
SupplierID *
ProductID
QuantityOrdered
UnitPrice
OrderDate
RequiredDate
ReceivedDate
PaymentDate
PaymentAmount


 
0
 

Author Comment

by:SunKosi
ID: 8067241
Orangehead911,

I have upped the points in hope that you can help me get this finished within a few hours.  I must turn this in today no matter what condition it is in.

Thanks,
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 8067466
Ok, I didn't realize that the deadline was today. Give an hour or two!
0
 

Author Comment

by:SunKosi
ID: 8067500
Thanks!!
0
 

Author Comment

by:SunKosi
ID: 8067544
I have done what little I know how at this point.  Here is where I am at now.  Basically just re-naming some of the labels and such.

package mypackage1;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JList;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import java.util.List;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import java.awt.Color;
import java.sql.*;
 
public class Frame1 extends JFrame
implements ActionListener
{
     Connection connection;
   
     //setting up the list of product numbers, buttons
     //text fields and labels for text fields
     private String items[] =
     {
          "1","2", "3", "4", "5", "6", "7",
          "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
          "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
          "29", "30", "31", "32", "33", "34", "35"
     };
     

      private int amount[] = {};
      private JButton jButton1 = new JButton();
      private JLabel jLabel1 = new JLabel();
      private JLabel jLabel2 = new JLabel();
      private JLabel jLabel3 = new JLabel();
      private JLabel jLabel4 = new JLabel();
      private JLabel jLabel5 = new JLabel();
      private JLabel jLabel6 = new JLabel();
      private JLabel jLabel7 = new JLabel();
      private JLabel jLabel8 = new JLabel();
      private JTextField jTextProductID = new JTextField();
      private JTextField jTextField1 = new JTextField();
      private JTextField jTextSupplierID = new JTextField();
      private JTextField jTextProductName = new JTextField();
      private JTextField jTextQuantityPerProduct = new JTextField();
      private JTextField jTextPrice = new JTextField();
      private JTextField jTextQuantityInStock = new JTextField();
      private JTextField jTextQuantityOnOrder = new JTextField();
      private JTextField jTextReOrderLevel = new JTextField();
      private JLabel jLabel9 = new JLabel();
      private JComboBox jComboBox1 = new JComboBox(items);
      private JList jList1 = new JList();
     
      public Frame1()
      {
         try
          {
            jbInit();
           
          }
        catch(Exception e)
           {
            e.printStackTrace();
           }
      }
     
      public void jbInit()
      {
        //set the fields within GUI to display names of fields
        this.getContentPane().setLayout(null);
        this.setSize(new Dimension(461, 321));
        this.setBackground(new Color(33, 41, 227));
        this.setForeground(Color.blue);
        jButton1.setText("Display ProductID Details");
        jButton1.setBounds(new Rectangle(0, 5, 185, 25));
        jTextSupplierID.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          jTextField2_actionPerformed(e);
        }
      });
        jLabel9.setText("Product ID");
        jLabel9.setBounds(new Rectangle(195, 5, 75, 20));
        jComboBox1.setBounds(new Rectangle(15, 45, 124, 20));
        jLabel2.setText("Supplier ID");
        jLabel2.setBounds(new Rectangle(195, 35, 70, 15));
        jLabel3.setText("Product Name");
        jLabel3.setBounds(new Rectangle(175, 65, 135, 15));
        jLabel4.setText("Quantity Per Product");
        jLabel4.setBounds(new Rectangle(175, 90, 130, 20));
        jLabel5.setText("Price");
        jLabel5.setBounds(new Rectangle(175, 130, 110, 15));
        jLabel6.setText("Quantity in Stock");
        jLabel6.setBounds(new Rectangle(175, 160, 110, 20));
        jLabel7.setText("Quantity On Order");
        jLabel7.setBounds(new Rectangle(175, 200, 120, 15));
        jLabel8.setText("Re-Order Level");
        jLabel8.setBounds(new Rectangle(175, 235, 100, 15));
        jTextSupplierID.setText("2");
        jTextSupplierID.setBounds(new Rectangle(310, 30, 60, 20));
        jTextProductID.setText("9");
        jTextProductID.setBounds(new Rectangle(310, 5, 90, 20));
        jTextProductName.setText("3");
        jTextProductName.setBounds(new Rectangle(270, 60, 180, 20));
        jTextQuantityPerProduct.setText("4");
        jTextQuantityPerProduct.setBounds(new Rectangle(310, 90, 85, 20));
        jTextPrice.setText("5");
        jTextPrice.setBounds(new Rectangle(310, 125, 85, 20));
        jTextQuantityInStock.setText("6");
        jTextQuantityInStock.setBounds(new Rectangle(310, 160, 85, 20));
        jTextQuantityOnOrder.setText("7");
        jTextQuantityOnOrder.setBounds(new Rectangle(310, 195, 85, 20));
        jTextReOrderLevel.setText("8");
        jTextReOrderLevel.setBounds(new Rectangle(310, 230, 85, 20));
           
        this.getContentPane().add(jComboBox1, null);
        this.getContentPane().add(jLabel9, null);
           this.getContentPane().add(jTextReOrderLevel, null);
           this.getContentPane().add(jTextQuantityOnOrder, null);
           this.getContentPane().add(jTextQuantityInStock, null);
           this.getContentPane().add(jTextPrice, null);
           this.getContentPane().add(jTextQuantityPerProduct, null);
           this.getContentPane().add(jTextProductName, null);
           this.getContentPane().add(jTextSupplierID, null);
           this.getContentPane().add(jTextProductID, null);
           this.getContentPane().add(jLabel8, null);
           this.getContentPane().add(jLabel7, null);
           this.getContentPane().add(jLabel6, null);
           this.getContentPane().add(jLabel5, null);
           this.getContentPane().add(jLabel4, null);
           this.getContentPane().add(jLabel3, null);
           this.getContentPane().add(jLabel2, null);
           this.getContentPane().add(jButton1, null);
           this.getContentPane().add(jTextField1, null);
           jButton1.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          jButton1_actionPerformed(e);
        }
      });
 
       jList1.addListSelectionListener( new ListSelectionListener()
      {
        public void valueChanged(ListSelectionEvent listSelectionEvent)
        {
      }});}
 
 public void actionPerformed(ActionEvent e)
        {
          jButton1_actionPerformed(e);
        }
      public void get_quantity()
      {
           int list[] = jList1.getSelectedIndices();
           int i= list[0];
           jTextSupplierID.setText(toString(amount[i]));
      }
        public void jButton1_actionPerformed(ActionEvent e)
      {
           get_quantity();
      }
         public String toString(int x)
      {
         return "Quantity is " + x + " ";
      }
         public void initDb()
     {
           try
           {
            // Load the database drivers.
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
           System.out.println("drivers loaded");
               
           // Obtain connection to the database.
               
          connection = DriverManager.getConnection("jdbc:odbc:TechInventory");
          System.out.println("Connection established");
               
          }
          catch (ClassNotFoundException cnf)
          {
           System.out.println("Could not find class: " + cnf);
          }
          catch (SQLException sqle)
          {
            System.out.println("SQL problem: " + sqle);
          }
     }
     
      public void readDataFromDb()
      {
         Statement statement;
         ResultSet rs;
         {
          try
           {
           // Create statement to read and write to/from database.
           statement = connection.createStatement();
           System.out.println("Statement created");
                     
           rs = statement.executeQuery
  ("SELECT * FROM PRODUCTS WHERE productid=" + jComboBox1.getSelectedIndex());

           {            //read from database method called once user
                        //has selected a product id from the combo box.
           initDb();
           readDataFromDb();
           }                  
          // Get next row.
          while( rs.next() )
           {
            jTextProductID.setText(rs.getString("ProductID"));
            jTextSupplierID.setText(rs.getString("SupplierID"));
            jTextProductName.setText(rs.getString("ProductName"));
            jTextQuantityPerProduct.setText(rs.getString("QuantityPerProduct"));
            jTextPrice.setText(rs.getString("Price"));
            jTextQuantityInStock.setText(rs.getString("QuantityInStock"));
            jTextQuantityOnOrder.setText(rs.getString("QuantityOnOrder"));
            jTextReOrderLevel.setText(rs.getString("ReOrderLevel"));
           }
          // Close statement and connection.
      statement.close();
        }
      catch (SQLException sqle)
        {
           System.out.println("SQL problem: " + sqle);
        }
    }
}
     
     public void closeConnectionToDb()
     {
        try
        {
          connection.close();
        }
          catch (SQLException sqle)
          {
            System.out.println("SQL problem: " + sqle);
          }
     }
     
    public static void main(String args[])
    {
      Frame1 frame = new Frame1();
      frame.setVisible(true);
    }
 
  private void jTextField2_actionPerformed(ActionEvent e)
  {
  }
}
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 8067890
See how this works out for you! Search for "TLB:" (without the quotes) for my comments. Read them and reflect!

package mypackage1;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JList;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import java.util.List;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import java.awt.Color;
import java.sql.*;

public class Frame1 extends JFrame
implements ActionListener
{
      // TLB: Unless you want other classes to be ablt to directly access your instance
      // variables, always make them private
      private Connection connection;
      
      //setting up the list of product numbers, buttons
      //text fields and labels for text fields
      private String items[] =
      {
            "1","2", "3", "4", "5", "6", "7",
            "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
            "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28",
            "29", "30", "31", "32", "33", "34", "35"
      };
      
      
      private int amount[] =
      {};
      private JButton jButton1 = new JButton();
      private JLabel jLabel1 = new JLabel();
      private JLabel jLabel2 = new JLabel();
      private JLabel jLabel3 = new JLabel();
      private JLabel jLabel4 = new JLabel();
      private JLabel jLabel5 = new JLabel();
      private JLabel jLabel6 = new JLabel();
      private JLabel jLabel7 = new JLabel();
      private JLabel jLabel8 = new JLabel();
      private JTextField jTextProductID = new JTextField();
      private JTextField jTextField1 = new JTextField();
      private JTextField jTextSupplierID = new JTextField();
      private JTextField jTextProductName = new JTextField();
      private JTextField jTextQuantityPerProduct = new JTextField();
      private JTextField jTextPrice = new JTextField();
      private JTextField jTextQuantityInStock = new JTextField();
      private JTextField jTextQuantityOnOrder = new JTextField();
      private JTextField jTextReOrderLevel = new JTextField();
      private JLabel jLabel9 = new JLabel();
      private JComboBox jComboBox1 = new JComboBox(items);
      private JList jList1 = new JList();
      
      public Frame1()
      {
            try
            {
                  jbInit();
                  
                  // TLB: you only want to initialize the database connection once
                  initDb();
            }
            catch(Exception e)
            {
                  e.printStackTrace();
            }
      }
      
      public void jbInit()
      {
            //set the fields within GUI to display names of fields
            this.getContentPane().setLayout(null);
            this.setSize(new Dimension(461, 321));
            this.setBackground(new Color(33, 41, 227));
            this.setForeground(Color.blue);
            jButton1.setText("Display ProductID Details");
            jButton1.setBounds(new Rectangle(0, 5, 185, 25));
            
            // TLB: I don't see a use for this right now
            /*jTextSupplierID.addActionListener(new ActionListener()
            {
                  public void actionPerformed(ActionEvent e)
                  {
                        jTextField2_actionPerformed(e);
                  }
            });*/
            jLabel9.setText("Product ID");
            jLabel9.setBounds(new Rectangle(195, 5, 75, 20));
            jComboBox1.setBounds(new Rectangle(15, 45, 124, 20));
            jLabel2.setText("Supplier ID");
            jLabel2.setBounds(new Rectangle(195, 35, 70, 15));
            jLabel3.setText("Product Name");
            jLabel3.setBounds(new Rectangle(175, 65, 135, 15));
            jLabel4.setText("Quantity Per Product");
            jLabel4.setBounds(new Rectangle(175, 90, 130, 20));
            jLabel5.setText("Price");
            jLabel5.setBounds(new Rectangle(175, 130, 110, 15));
            jLabel6.setText("Quantity in Stock");
            jLabel6.setBounds(new Rectangle(175, 160, 110, 20));
            jLabel7.setText("Quantity On Order");
            jLabel7.setBounds(new Rectangle(175, 200, 120, 15));
            jLabel8.setText("Re-Order Level");
            jLabel8.setBounds(new Rectangle(175, 235, 100, 15));
            jTextSupplierID.setText("2");
            jTextSupplierID.setBounds(new Rectangle(310, 30, 60, 20));
            jTextProductID.setText("9");
            jTextProductID.setBounds(new Rectangle(310, 5, 90, 20));
            jTextProductName.setText("3");
            jTextProductName.setBounds(new Rectangle(270, 60, 180, 20));
            jTextQuantityPerProduct.setText("4");
            jTextQuantityPerProduct.setBounds(new Rectangle(310, 90, 85, 20));
            jTextPrice.setText("5");
            jTextPrice.setBounds(new Rectangle(310, 125, 85, 20));
            jTextQuantityInStock.setText("6");
            jTextQuantityInStock.setBounds(new Rectangle(310, 160, 85, 20));
            jTextQuantityOnOrder.setText("7");
            jTextQuantityOnOrder.setBounds(new Rectangle(310, 195, 85, 20));
            jTextReOrderLevel.setText("8");
            jTextReOrderLevel.setBounds(new Rectangle(310, 230, 85, 20));
            
            this.getContentPane().add(jComboBox1, null);
            this.getContentPane().add(jLabel9, null);
            this.getContentPane().add(jTextReOrderLevel, null);
            this.getContentPane().add(jTextQuantityOnOrder, null);
            this.getContentPane().add(jTextQuantityInStock, null);
            this.getContentPane().add(jTextPrice, null);
            this.getContentPane().add(jTextQuantityPerProduct, null);
            this.getContentPane().add(jTextProductName, null);
            this.getContentPane().add(jTextSupplierID, null);
            this.getContentPane().add(jTextProductID, null);
            this.getContentPane().add(jLabel8, null);
            this.getContentPane().add(jLabel7, null);
            this.getContentPane().add(jLabel6, null);
            this.getContentPane().add(jLabel5, null);
            this.getContentPane().add(jLabel4, null);
            this.getContentPane().add(jLabel3, null);
            this.getContentPane().add(jLabel2, null);
            this.getContentPane().add(jButton1, null);
            this.getContentPane().add(jTextField1, null);
            jButton1.addActionListener(new ActionListener()
            {
                  public void actionPerformed(ActionEvent e)
                  {
                        jButton1_actionPerformed(e);
                  }
            });
            
            jList1.addListSelectionListener( new ListSelectionListener()
            {
                  public void valueChanged(ListSelectionEvent listSelectionEvent)
                  {
                  }});}
      
      public void actionPerformed(ActionEvent e)
      {
            jButton1_actionPerformed(e);
      }
      public void get_quantity()
      {
            int list[] = jList1.getSelectedIndices();
            int i= list[0];
            jTextSupplierID.setText(toString(amount[i]));
      }
      public void jButton1_actionPerformed(ActionEvent e)
      {
            get_quantity();
      }
      public String toString(int x)
      {
            return "Quantity is " + x + " ";
      }
      public void initDb()
      {
            try
            {
                  // Load the database drivers.
                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                  System.out.println("drivers loaded");
                  
                  // Obtain connection to the database.
                  
                  connection = DriverManager.getConnection("jdbc:odbc:TechInventory");
                  System.out.println("Connection established");
                  
            }
            catch (ClassNotFoundException cnf)
            {
                  System.out.println("Could not find class: " + cnf);
            }
            catch (SQLException sqle)
            {
                  System.out.println("SQL problem: " + sqle);
            }
      }
      
      public void readDataFromDb()
      {
            Statement statement;
            ResultSet rs;
            {
                  try
                  {
                        // Create statement to read and write to/from database.
                        statement = connection.createStatement();
                        System.out.println("Statement created");
                        
                        // TLB: you probably want the ITEM and not the index
                        // TLB: names for things in databases might be case sensitive, don't make assumptions about that ALWAYS use proper case
                        // TLB: all upper characters makes text hard to read, and traditionally, SQL statements are entered in lower case
                        rs = statement.executeQuery("select * from Products where ProductID=" + jComboBox1.getSelectedItem());
                        
                        // REMOVE this section after reading my comments
                        {            //read from database method called once user
                              //has selected a product id from the combo box.
                              
                              // TLB: with the following line in place you would have had an infinite loop as there is no branching and
                              // TLB: you only want to initialize the database connection once
                              //initDb();
                              // TLB: no base case for the recursion
                              //readDataFromDb();
                        }
                        // Get next row.
                        // TLB: since your only reading ONE line from the database, let's use 'if'
                        if( rs.next() )
                        {
                              jTextProductID.setText(rs.getString("ProductID"));
                              jTextSupplierID.setText(rs.getString("SupplierID"));
                              jTextProductName.setText(rs.getString("ProductName"));
                              jTextQuantityPerProduct.setText(rs.getString("QuantityPerProduct"));
                              jTextPrice.setText(rs.getString("Price"));
                              jTextQuantityInStock.setText(rs.getString("QuantityInStock"));
                              jTextQuantityOnOrder.setText(rs.getString("QuantityOnOrder"));
                              jTextReOrderLevel.setText(rs.getString("ReOrderLevel"));
                        }
                        // TLB: Close statement and NOT the connection.
                        statement.close();
                  }
                  catch (SQLException sqle)
                  {
                        System.out.println("SQL problem: " + sqle);
                  }
            }
      }
      
      public void closeConnectionToDb()
      {
            try
            {
                  connection.close();
            }
            catch (SQLException sqle)
            {
                  System.out.println("SQL problem: " + sqle);
            }
      }
      
      public static void main(String args[])
      {
            Frame1 frame = new Frame1();
            frame.setVisible(true);
      }
      
      // TLB: I don't see a use for this right now
      /*private void jTextField2_actionPerformed(ActionEvent e)
      {
      }*/
}

0
 

Author Comment

by:SunKosi
ID: 8068021
Thanks so much for the quick turn around!  I have taken this and placed it into the program.  I still don't seem to get anything back from the database though.  The fields 2-9 in the textfields are not being populated with the database info no matter what product code I click on in the combobox dropdown list.  Not sure where to turn with this.  I see that the program is only seeing the portion of the code below.  Is there a method that I would need to use to clear the numbers that are in the textfields first so that the database info can be shown (kind of like a reset so the next product number can be retrieved?  

jTextSupplierID.setText("2");
          jTextSupplierID.setBounds(new Rectangle(310, 30, 60, 20));
          jTextProductID.setText("9");
          jTextProductID.setBounds(new Rectangle(310, 5, 90, 20));
          jTextProductName.setText("3");
          jTextProductName.setBounds(new Rectangle(270, 60, 180, 20));
          jTextQuantityPerProduct.setText("4");
          jTextQuantityPerProduct.setBounds(new Rectangle(310, 90, 85, 20));
          jTextPrice.setText("5");
          jTextPrice.setBounds(new Rectangle(310, 125, 85, 20));
          jTextQuantityInStock.setText("6");
          jTextQuantityInStock.setBounds(new Rectangle(310, 160, 85, 20));
          jTextQuantityOnOrder.setText("7");
          jTextQuantityOnOrder.setBounds(new Rectangle(310, 195, 85, 20));
          jTextReOrderLevel.setText("8");
          jTextReOrderLevel.setBounds(new Rectangle(310, 230, 85, 20));

also, just to clarify on the comment below:

                      // Get next row.
                    // TLB: since your only reading ONE line from the database, let's use 'if'
                    if( rs.next() )
                    {
                         jTextProductID.setText(rs.getString("ProductID"));

you are meaning one row at a time right? I wish I knew enough to be of some assistance.  
0
 

Author Comment

by:SunKosi
ID: 8068042
Thanks so much for the quick turn around!  I have taken this and placed it into the program.  I still don't seem to get anything back from the database though.  The fields 2-9 in the textfields are not being populated with the database info no matter what product code I click on in the combobox dropdown list.  Not sure where to turn with this.  I see that the program is only seeing the portion of the code below.  Is there a method that I would need to use to clear the numbers that are in the textfields first so that the database info can be shown (kind of like a reset so the next product number can be retrieved?  

jTextSupplierID.setText("2");
          jTextSupplierID.setBounds(new Rectangle(310, 30, 60, 20));
          jTextProductID.setText("9");
          jTextProductID.setBounds(new Rectangle(310, 5, 90, 20));
          jTextProductName.setText("3");
          jTextProductName.setBounds(new Rectangle(270, 60, 180, 20));
          jTextQuantityPerProduct.setText("4");
          jTextQuantityPerProduct.setBounds(new Rectangle(310, 90, 85, 20));
          jTextPrice.setText("5");
          jTextPrice.setBounds(new Rectangle(310, 125, 85, 20));
          jTextQuantityInStock.setText("6");
          jTextQuantityInStock.setBounds(new Rectangle(310, 160, 85, 20));
          jTextQuantityOnOrder.setText("7");
          jTextQuantityOnOrder.setBounds(new Rectangle(310, 195, 85, 20));
          jTextReOrderLevel.setText("8");
          jTextReOrderLevel.setBounds(new Rectangle(310, 230, 85, 20));

also, just to clarify on the comment below:

                      // Get next row.
                    // TLB: since your only reading ONE line from the database, let's use 'if'
                    if( rs.next() )
                    {
                         jTextProductID.setText(rs.getString("ProductID"));

you are meaning one row at a time right? I wish I knew enough to be of some assistance.  
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 8068119
Well, since the ProductID field is the primary key for the Products table, there will only ever be one row to retrieve from the database when you query in that field with only one value.

0
 

Author Comment

by:SunKosi
ID: 8068137
I understand that, but as I change product ID's, the information of that particular product ID should correspond to that product ID.  Any ideas about the actual information not appearing in the application?  It might be my set up of the driver.  I'll check that and let you know if that is the problem.
0
 

Author Comment

by:SunKosi
ID: 8068194
I have rechecked my drivers and they are all pointed to the TechInventory Database.  Not sure what the issue is.
0
 

Author Comment

by:SunKosi
ID: 8068217
I have rechecked my drivers and they are all pointed to the TechInventory Database.  Not sure what the issue is.
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 8068295
I should be up and running shortly
0
 

Author Comment

by:SunKosi
ID: 8068363
OK.  
0
 

Author Comment

by:SunKosi
ID: 8068426
Sorry, but I have to turn it in now.  I'll go ahead and give you the points just because you have so much understanding and patience.  Thanks again for all your help and advice.  And I hope you will be online for guidance for a few other assignments I have to turn in over the next 2 months.
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 8068452
can't you wait for 15 min?
0
 

Author Comment

by:SunKosi
ID: 8068474
I wish I could.  The assignment is due at 5:30 my time and that is 6 min from now.
0
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 1200 total points
ID: 8068497
dude, I am so sorry! I can't accept all the points for this question. Save them for future use, unless you still want my help in solving the problems with this assignment.

Let me know!
0
 

Author Comment

by:SunKosi
ID: 8068580
Don't even apologize!!  It was entirely up to me to figure this out and I didn't do it.  Don't work on it any longer.  I want to give you all those points just because you have the patience of a saint and you got me much further along than I would have gotten on my own!  I will keep reading and learning more over the next two weeks until the next one is due.  I am sure I will have questions and I would like to have your help and guidance if you are still willing.

Thanks again!  Go get some turns in on the mountain...I know I will!
0
 

Author Comment

by:SunKosi
ID: 8068586
Still a great teacher and helper.  I couldn't have done it without this person.
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 8068615
Ok, thanks! I'll be here for the next assignment! tommy@braas.ac

Yeah, you go get some turns on the mountain!
0
 

Author Comment

by:SunKosi
ID: 8068663
Thanks.  I'll post questions to you just like this one when I have an issue I can't resolve.
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 8068946
Chech this code out:


package mypackage1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import javax.swing.event.*;
import java.sql.*;

public class Frame1 extends JFrame
{
      // TLB: Unless you want other classes to be ablt to directly access your instance
      // variables, always make them private
      private Connection connection;
            
      private JButton jButtonQuit = new JButton();
      private JLabel jLabel1 = new JLabel();
      private JLabel jLabel2 = new JLabel();
      private JLabel jLabel3 = new JLabel();
      private JLabel jLabel4 = new JLabel();
      private JLabel jLabel5 = new JLabel();
      private JLabel jLabel6 = new JLabel();
      private JLabel jLabel7 = new JLabel();
      private JLabel jLabel8 = new JLabel();
      private JTextField jTextProductID = new JTextField();
      private JTextField jTextField1 = new JTextField();
      private JTextField jTextSupplierID = new JTextField();
      private JTextField jTextProductName = new JTextField();
      private JTextField jTextQuantityPerProduct = new JTextField();
      private JTextField jTextPrice = new JTextField();
      private JTextField jTextQuantityInStock = new JTextField();
      private JTextField jTextQuantityOnOrder = new JTextField();
      private JTextField jTextReOrderLevel = new JTextField();
      private JLabel jLabel9 = new JLabel();
      
      boolean updatingComboBox = false;
      
      // TLB: we can't initialize the combo box with items until we've read them from the database
      private JComboBox jComboProductIDs = new JComboBox();
      
      public Frame1()
      {
            try
            {
                  jbInit();
                  
                  // TLB: you only want to initialize the database connection once
                  initDb();
                  
                  //  TLB: reads the available products from the database
                  initComboBox();
                  
                  // update the UI with the first object in the combo box list
                  if( jComboProductIDs.getItemCount() > 0 )
                  {
                        readDataFromDb(jComboProductIDs.getSelectedItem().toString());
                  }
                  
            }
            catch(Exception e)
            {
                  e.printStackTrace();
            }
      }
      
      public void jbInit()
      {
            //set the fields within GUI to display names of fields
            this.getContentPane().setLayout(new BorderLayout());
            this.setSize(new Dimension(461, 321));
            this.setBackground(new Color(33, 41, 227));
            this.setForeground(Color.blue);
            JPanel textFieldPanel = new JPanel();
            textFieldPanel.setLayout(new GridLayout(8,2,20,5));
            
            jButtonQuit.setText("Quit");
            
            jLabel9.setText("Product ID");
            jLabel2.setText("Supplier ID");
            jLabel3.setText("Product Name");
            jLabel4.setText("Quantity Per Product");
            jLabel5.setText("Price");
            jLabel6.setText("Quantity in Stock");
            jLabel7.setText("Quantity On Order");
            jLabel8.setText("Re-Order Level");

            textFieldPanel.add(jLabel9);
            textFieldPanel.add(jTextProductID);
            
            textFieldPanel.add(jLabel2);
            textFieldPanel.add(jTextSupplierID);
            
            textFieldPanel.add(jLabel3);
            textFieldPanel.add(jTextProductName);
            
            textFieldPanel.add(jLabel4);
            textFieldPanel.add(jTextQuantityPerProduct);
            
            textFieldPanel.add(jLabel5);
            textFieldPanel.add(jTextPrice);
            
            textFieldPanel.add(jLabel6);
            textFieldPanel.add(jTextQuantityInStock);
            
            textFieldPanel.add(jLabel7);
            textFieldPanel.add(jTextQuantityOnOrder);
            
            textFieldPanel.add(jLabel8);
            textFieldPanel.add(jTextReOrderLevel);

            JPanel comboPanel = new JPanel();
            comboPanel.setLayout(new FlowLayout());
            comboPanel.add(new JLabel("Select Product ID:"));
            comboPanel.add(jComboProductIDs);
            
            getContentPane().add(textFieldPanel, "Center");
            getContentPane().add(comboPanel, "North");
            getContentPane().add(jButtonQuit, "South");

            jButtonQuit.addActionListener(new ActionListener()
            {
                  public void actionPerformed(ActionEvent e)
                  {
                        jButtonQuit_actionPerformed(e);
                  }
            });
            
            jComboProductIDs.addItemListener(new ItemListener()
            {
                  public void itemStateChanged(ItemEvent itemEvent)
                  {
                        readDataFromDb(jComboProductIDs.getSelectedItem().toString());
                  }
            });
            // TLB: I don't see a use for this right now
            jTextProductID.addActionListener(new ActionListener()
            {
                  public void actionPerformed(ActionEvent e)
                  {
                        jTextProductID_actionPerformed(e);
                  }
            });
      }
            
      public void jButtonQuit_actionPerformed(ActionEvent e)
      {
            try
            {
                  connection.close();
                  System.exit(0);
            }
            catch(SQLException sqle)
            {
                  sqle.printStackTrace();
            }
            System.exit(-13);
      }

      private void jTextProductID_actionPerformed(ActionEvent e)
      {
            readDataFromDb(jTextProductID.getText());
      }

      public String toString(int x)
      {
            return "Quantity is " + x + " ";
      }

      public void initDb()
      {
            try
            {
                  // Load the database drivers.
                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                  System.err.println("drivers loaded");
                  
                  // Obtain connection to the database.
                  
                  connection = DriverManager.getConnection("jdbc:odbc:TechInventory", "", "");
                  System.err.println("Connection established");
                  
            }
            catch (ClassNotFoundException cnf)
            {
                  System.err.println("Could not find class: " + cnf);
            }
            catch (SQLException sqle)
            {
                  System.err.println("SQL problem: " + sqle);
                  sqle.printStackTrace();
            }
      }
      
      public void readDataFromDb(String productID)
      {
            Statement statement;
            ResultSet rs;
            if( !updatingComboBox && productID != null && productID.length() > 0 )
            {
                  try
                  {
                        // Create statement to read and write to/from database.
                        statement = connection.createStatement();
                        System.err.println("Statement created");
                        
                        // TLB: you probably want the ITEM and not the index
                        // TLB: names for things in databases might be case sensitive, don't make assumptions about that ALWAYS use proper case
                        // TLB: all upper characters makes text hard to read, and traditionally, SQL statements are entered in lower case
                        String sqlString = "select * from Products where ProductID=" + productID;
                        System.err.println("Executing statement: \"" + sqlString + "\"");
                        rs = statement.executeQuery(sqlString);
                        System.err.println("Statement successfully executed");

                        // TLB: since your only reading ONE line from the database, let's use 'if'
                        if( rs.next() )
                        {
                              jTextProductID.setText(rs.getString("ProductID"));
                              jTextSupplierID.setText(rs.getString("SupplierID"));
                              jTextProductName.setText(rs.getString("ProductName"));
                              jTextQuantityPerProduct.setText(rs.getString("QuantityPerProduct"));
                              jTextPrice.setText(rs.getString("Price"));
                              jTextQuantityInStock.setText(rs.getString("QuantityInStock"));
                              jTextQuantityOnOrder.setText(rs.getString("QuantityOnOrder"));
                              jTextReOrderLevel.setText(rs.getString("ReOrderLevel"));
                        }
                        // TLB: Close statement and NOT the connection.
                        statement.close();
                  }
                  catch (SQLException sqle)
                  {
                        System.err.println("SQL problem: " + sqle);
                        sqle.printStackTrace();
                  }
            }
      }
      
      public void initComboBox()
      {
            Statement statement;
            ResultSet rs;
            updatingComboBox = true;
            try
            {
                  // Create statement to read and write to/from database.
                  statement = connection.createStatement();
                  System.err.println("Statement created");

                  // read ALL the product IDs from the table
                  rs = statement.executeQuery("select ProductID from Products");

                  // since the query was successful we can safely remove all the items from the combo box
                  jComboProductIDs.removeAllItems();

                  // Get next row.
                  while( rs.next() )
                  {
                        String temp = rs.getString("ProductID");
                        System.err.println("Adding product with ID = " + temp + " to combo box");
                        jComboProductIDs.addItem(temp);
                  }
                  // TLB: Close statement and NOT the connection.
                  statement.close();
            }
            catch (SQLException sqle)
            {
                  System.err.println("SQL problem: " + sqle);
                  sqle.printStackTrace();
            }
            System.err.println("There are " + jComboProductIDs.getItemCount() + " items in the combo box");
            updatingComboBox = false;
      }

      
      public void closeConnectionToDb()
      {
            try
            {
                  connection.close();
            }
            catch (SQLException sqle)
            {
                  System.err.println("SQL problem: " + sqle);
            }
      }
      
      public static void main(String args[])
      {
            Frame1 frame = new Frame1();
            frame.setVisible(true);
      }
      
}
0
 

Author Comment

by:SunKosi
ID: 8069306
thanks.  I did look at it, but it still didn't return anything.  Either I have my drivers set up incorrectly, the code isn't right, or I have a bug in the JDeveloper software.  In any case, I've given my(your) version over to the professor, so there isn't much I can do at this point.  At least I will get some feedback in a few days about the code and I will pass it along to you when I do.  Not sure where the problem is.  

The mountains got dumped on, so I'm taking a sick day tomorrow to do some telemarking!
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 8069571
I think that there is possibly a problem with your ODBC setup. I had some problems initially too, but I managed to fix them. You might want to use a 'System DSN' for your ODBC database setting. And verify that it actually 'remembers' which database file you selected. My email was in a previous posting.

Have fun in the snow! Have fun for me too! I'm envious.....
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question