Results 1 to 6 of 6
  1. #1
    Madhatter40 is offline Member
    Join Date
    Jan 2015
    Posts
    11
    Rep Power
    0

    Default Retrieving data from database based on user input

    I need to take the users input from 2 boxes and reference that to a sqlite database and populate the fields with the data in the database. I can't figure out how. Any suggestions?
    I know the database code work.
    Here is the code for the "user" form;
    Java Code:
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JTextField;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import javax.swing.SwingConstants;
    import javax.swing.JButton;
    
    
    public class Input_Items implements ActionListener {
    
    	private static final Object Execute_Button = null;
    	private JFrame frame;
    	private JTextField F_Item;
    	private JTextField F_Desc;
    	private JTextField F_FFM;
    	private JTextField F_CupCount;
    	private JTextField F_Tooling;
    	private JTextField S_Item;
    	private JTextField S_Desc;
    	private JTextField S_FFM;
    	private JTextField S_CupCount;
    	private JTextField S_Tooling;
    	private JTextField Item_Number;
    	private JTextField Second_Number;
    
    			/**
    			 * Launch the application.
    			 */
    			public static void main(String[] args) {
    				EventQueue.invokeLater(new Runnable() {
    					public void run() {
    						try {
    							Input_Items window = new Input_Items();
    							window.frame.setVisible(true);
    						} catch (Exception e) {
    							e.printStackTrace();
    						}
    					}
    				});
    			}//End of main method
    
    				//Create the application.				 
    				public Input_Items() {
    					initialize();
    				}//End of Output Window Constructor
    
    		// Initialize the contents of the frame.	
    	private void initialize() {
    		frame = new JFrame();
    		frame.setBounds(100, 100, 800, 300);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.getContentPane().setLayout(null);
    		//----------------------------
    			JMenuBar bar = new JMenuBar();		    
    		    JMenu menu = new JMenu("File");	    
    		    menu.setMnemonic(KeyEvent.VK_O);	
    		    //ButtonGroup group = new ButtonGroup();	    
    	     //create file menu	      
    	      JMenuItem myItem = new JMenuItem("Save");
    	      menu.add(myItem);
    	      myItem.addActionListener(this);
    		  menu.add(myItem);
    	      menu.addSeparator();// This puts a line in the file menu
    	      JMenuItem quit = new JMenuItem("Quit");
    		  quit.addActionListener(this);
    		  menu.add(quit);
    	     
           
    	    bar.add(menu);
    	    frame.setJMenuBar(bar);
    	    frame.setSize(800, 400);
    	    frame.setVisible(true);
    	    
    		//------------This is the Current Item number Info----------------
    	    
    	    JLabel lblFirstItem = new JLabel("First Item");
    		lblFirstItem.setHorizontalAlignment(SwingConstants.CENTER);
    		lblFirstItem.setFont(new Font("Tahoma", Font.PLAIN, 12));
    		lblFirstItem.setBounds(178, 201, 74, 14);
    		frame.getContentPane().add(lblFirstItem);
    		
    		JLabel lblItem = new JLabel("Item#");
    		lblItem.setBounds(22, 229, 74, 14);
    		frame.getContentPane().add(lblItem);
    		
    		JLabel lblDescription = new JLabel("Description");
    		lblDescription.setBounds(22, 254, 74, 14);
    		frame.getContentPane().add(lblDescription);
    		
    		JLabel lblCurrentFfmSetup = new JLabel("FFM Setup");
    		lblCurrentFfmSetup.setBounds(22, 279, 96, 14);
    		frame.getContentPane().add(lblCurrentFfmSetup);
    		
    		JLabel lblCupCount = new JLabel("Cup Count");
    		lblCupCount.setBounds(22, 304, 74, 14);
    		frame.getContentPane().add(lblCupCount);
    		
    		JLabel lblCurrenttooling = new JLabel("CurrentTooling");
    		lblCurrenttooling.setBounds(22, 329, 74, 14);
    		frame.getContentPane().add(lblCurrenttooling);
    		
    		F_Item = new JTextField();
    		F_Item.setBounds(125, 226, 200, 20);
    		frame.getContentPane().add(F_Item);
    		F_Item.setColumns(10);
    		F_Item.setText("Try");//Sets the text for the first Item (Item)
    		
    		F_Desc = new JTextField();
    		F_Desc.setBounds(125, 251, 200, 20);
    		frame.getContentPane().add(F_Desc);
    		F_Desc.setColumns(10);
    		F_Desc.setText("Try");//Sets the text for the first Item (Description)
    				
    		F_FFM = new JTextField();
    		F_FFM.setBounds(125, 276, 200, 20);
    		frame.getContentPane().add(F_FFM);
    		F_FFM.setColumns(10);
    		F_FFM.setText("Try");//Sets the text for the first Item (FFM)
    		
    		F_CupCount = new JTextField();
    		F_CupCount.setBounds(125, 301, 200, 20);
    		frame.getContentPane().add(F_CupCount);
    		F_CupCount.setColumns(10);
    		F_CupCount.setText("Try");//Sets the text for the first Item (Cup Count)
    		
    		F_Tooling = new JTextField();
    		F_Tooling.setBounds(125, 326, 200, 20);
    		frame.getContentPane().add(F_Tooling);
    		F_Tooling.setColumns(10);
    		F_Tooling.setText("1234");//Sets the text for the first Item (Tooling)
    		
    		//------------This is the Next Item number Info----------------
    		
    		// First number refers to the Distance from Left
    		// Second number refers to the Distance from Top
    		// Third number refers to the Width of text box
    		// First number refers to the Height of text box
    		
    		JLabel lblNextItem_1 = new JLabel("Next Item");
    		lblNextItem_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
    		lblNextItem_1.setBounds(537, 201, 57, 14);
    		frame.getContentPane().add(lblNextItem_1);				
    		
    		JLabel lblNextItem = new JLabel("Next Item#");
    		lblNextItem.setBounds(350, 229, 74, 14);
    		frame.getContentPane().add(lblNextItem);
    		
    		JLabel lblNewLabel = new JLabel("Description");
    		lblNewLabel.setBounds(350, 254, 74, 14);
    		frame.getContentPane().add(lblNewLabel);
    		
    		JLabel lblFfmChangeOver = new JLabel("FFM Setup");
    		lblFfmChangeOver.setBounds(350, 279, 143, 14);
    		frame.getContentPane().add(lblFfmChangeOver);
    		
    		JLabel lblCupCount_1 = new JLabel("Cup Count");
    		lblCupCount_1.setBounds(350, 304, 74, 14);
    		frame.getContentPane().add(lblCupCount_1);
    		
    		JLabel lblToolingChangeOver = new JLabel("Tooling ");
    		lblToolingChangeOver.setBounds(350, 329, 143, 14);
    		frame.getContentPane().add(lblToolingChangeOver);
    		
    		S_Item = new JTextField();
    		S_Item.setBounds(469, 226, 200, 20);
    		frame.getContentPane().add(S_Item);
    		S_Item.setColumns(10);
    		S_Item.setText("Try");//Sets the text for the second Item (Item)
    		
    		S_Desc = new JTextField();
    		S_Desc.setBounds(469, 251, 200, 20);
    		frame.getContentPane().add(S_Desc);
    		S_Desc.setColumns(10);
    		S_Desc.setText("Try");//Sets the text for the Second Item (Description)
    		
    		S_FFM = new JTextField();
    		S_FFM.setBounds(469, 276, 200, 20);
    		frame.getContentPane().add(S_FFM);
    		S_FFM.setColumns(10);
    		S_FFM.setText("Try");//Sets the text for the second Item (FFM)
    		
    		S_CupCount = new JTextField();
    		S_CupCount.setBounds(469, 301, 200, 20);
    		frame.getContentPane().add(S_CupCount);
    		S_CupCount.setColumns(10);
    		S_CupCount.setText("Try");//Sets the text for the second Item (Cup Count)
    				
    		S_Tooling = new JTextField();
    		S_Tooling.setBounds(469, 326, 200, 20);
    		frame.getContentPane().add(S_Tooling);
    		S_Tooling.setColumns(10);
    		S_Tooling.setText("1234");//Sets the text for the second Item (Tooling)	
    		
    		//------------This is the setup for user input of item numbers----------------
    		
    		JLabel lblInputCurrentItem = new JLabel("Input Current Item Running");
    		lblInputCurrentItem.setBounds(143, 24, 147, 20);
    		frame.getContentPane().add(lblInputCurrentItem);
    		
    		Item_Number = new JTextField();
    		Item_Number.setBounds(176, 67, 86, 20);
    		frame.getContentPane().add(Item_Number);
    		Item_Number.setColumns(10);
    		
    		JLabel lblInputNextItem = new JLabel("Input Next Item Running");
    		lblInputNextItem.setBounds(503, 24, 147, 20);
    		frame.getContentPane().add(lblInputNextItem);
    		
    		Second_Number = new JTextField();
    		Second_Number.setBounds(518, 67, 86, 20);
    		frame.getContentPane().add(Second_Number);
    		Second_Number.setColumns(10);
    		
    		JButton Execute_Button = new JButton("Calculate");
    		Execute_Button.setBounds(350, 111, 89, 23);
    		frame.getContentPane().add(Execute_Button);
    		Execute_Button.addActionListener(this);
    		
    		
    	}//End of initialize Constructor
    	
    	public void actionPerformed(ActionEvent ae) {
    		
    		Database_Connection dbc = new Database_Connection();
    		
    		try {
    			dbc.dataBase_Connection();
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		}		
    
           
        }//End of actionPerformed
    	
    }//End of Output Window class
    Here is my database connection code:
    Java Code:
     import java.sql.Connection;
     import java.sql.DriverManager;
     import java.sql.SQLException;
     import java.sql.Statement;
    
    public class Database_Connection {	
    	
        public void dataBase_Connection() throws ClassNotFoundException {
    	  // load the SQLite-JDBC driver using the current class loader
    	  Class.forName("org.sqlite.JDBC");
    	  Connection connection = null;	  
          try {
    		  // create a database connection
    		  connection = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Matthew\\Documents\\Eclipse\\WorkSpace\\Database\\ProductInfo.sqlite");
    		  Statement statement = connection.createStatement();
    		  statement.setQueryTimeout(30);  // set timeout to 30 sec.		  
          }//End Try loop      
    		catch(SQLException e) {			
    			  // if the error message is "out of memory", 
    			  // it probably means no database file is found
    			  System.err.println(e.getMessage());			  
    		}//End Catch block       
    		finally {			
    			  try {			  
    			    if(connection != null)
    			    connection.close();
    			  }//End Try block
    		  
    			  catch(SQLException e) {
    			    // connection close failed.
    			    System.err.println(e);
    			  }//End catch		  
    		}//End finally block      
        }//End main method    
       
     }//End Database_Connection class

  2. #2
    Madhatter40 is offline Member
    Join Date
    Jan 2015
    Posts
    11
    Rep Power
    0

    Default Re: Retrieving data from database based on user input

    So I was able to capture the Item#'s in the following code:
    I passed in the variable "arow" which gave me the info to get the right row in the database.
    How can I pass this info from ("Db_Connect") class back into the other class("Input_Items")

    Java Code:
     ResultSet rs = stmt.executeQuery( "SELECT * FROM Product Where Item = " + arow);
    	     
    	      // This will select the first item and description from the table 
    	        String[] Item1 = {rs.getString("Item"),rs.getString("Description"),rs.getString("CoffeeType"),rs.getString("CupCount"),rs.getString("PushCleanAllowed")};
    I am not sure how.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Retrieving data from database based on user input

    That's where the return statement comes in handy. You can return values to the method that calls it. See example:

    Java Code:
    public void doSomething() {
       int a1 = add(1, 2); //a1 is now 3
       int a2 = add(2,10); //a2 is now 12
    }
    
    public int add(int a, int b) {
       int c= a + b;
       return c;
    }
    Note that you can only return one type, so only a String, or an array of Strings, or anything else, but not TWO values. (Unless you put those in an array or a List, ofcourse...)

    @see: Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    Madhatter40 is offline Member
    Join Date
    Jan 2015
    Posts
    11
    Rep Power
    0

    Default Re: Retrieving data from database based on user input

    So I do understand the concept of the return statement some. My question is where do I put the return method in order to get the data back to the other class. I have tried many places with no success.

  5. #5
    Madhatter40 is offline Member
    Join Date
    Jan 2015
    Posts
    11
    Rep Power
    0

    Default Re: Retrieving data from database based on user input

    So the following code works great when i output it to the console window. I need it to be passed back to "Input_Items" class Please help!
    I tried to make a object of "Input_Items" class and that seems to work but it makes another
    window over the first with the new info. I would like to keep the first window and just populate that.
    Java Code:
        Class.forName("org.sqlite.JDBC");
          c = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Matthew\\Documents\\Eclipse\\WorkSpace\\Database\\ProductInfo.sqlite");
          c.setAutoCommit(false);
          System.out.println("Opened database successfully");
    
          
          	  stmt = c.createStatement(); 
          	  
         
    	      ResultSet rs = stmt.executeQuery( "SELECT * FROM Product Where Item = " + arow);
    	     
    	      // This will select the info from the table 
    	        String[] Item1 = {rs.getString("Item"),rs.getString("Description"),rs.getString("CoffeeType"),rs.getString("CupCount"),rs.getString("PushCleanAllowed")};
    	        
    	        
    	        	       
    	        // This outputs it to the screen   
    	         System.out.println( "Item = " + Item1[0] );
    	         System.out.println( "Description = " + Item1[1] );         
    	         System.out.println( "Coffee Type = " + Item1[2] );
    	         System.out.println( "Cup Count = " + Item1[3] );	         
    	         System.out.println();  
         
    	      ResultSet rs1 = stmt.executeQuery( "SELECT * FROM Product Where Item = " + arow1);
    	        
    	      // This will select the info from the table 
    	         String[] Item2 = {rs1.getString("Item"),rs1.getString("Description"),rs1.getString("CoffeeType"),rs1.getString("CupCount"),rs1.getString("PushCleanAllowed")};
    		  // This outputs it to the screen   
    	         System.out.println( "Item = " + Item2[0] );
    	         System.out.println( "Description = " + Item2[1] );         
    	         System.out.println( "Coffee Type = " + Item2[2] );
    	         System.out.println( "Cup Count = " + Item2[3] );	         
    	         System.out.println();     
     
    	         
    	         
          rs.close();
          stmt.close();
          c.close();

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Retrieving data from database based on user input

    You can also create a class to contain a variety of data types (primitive and objects). You can fill the class and return it and pass it as an argument to other class instances.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Displaying a square based on user's input
    By luke in forum New To Java
    Replies: 15
    Last Post: 07-14-2012, 09:43 AM
  2. Replies: 1
    Last Post: 01-25-2012, 05:10 PM
  3. Replies: 1
    Last Post: 04-05-2011, 08:28 AM
  4. How do I retrieve data based on user input?
    By tunali2 in forum New To Java
    Replies: 1
    Last Post: 04-03-2011, 02:40 PM
  5. Retrieving data from database every 10 secs.
    By vnsuresh.abi in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 03-31-2011, 11:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •