Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Inventory application with Read, Delete, Insert and Update Methods

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Inventory application with Read, Delete, Insert and Update Methods

    Can anyone help me with the read, insert, delete and update methods in my code? I believe the delete method is working OK but I get SQL errors when I use any of the other methods. I'm a beginner in programming and really can't figure this out. This is an applet that uses buttons named read, insert, delete and update. They are reading from a database that contains fields called partNumber(primary Key), name, description and price. I appreciate any help. Thanks!

    private void read_actionPerformed(ActionEvent e) {

    String partNumber = txtPartNumber.getText();

    try{
    String queryString = "select partNumber, name, description, price from Inventory" +
    " where Inventory.partNumber = " + partNumber ;

    ResultSet rset = stmt.executeQuery(queryString);

    if (rset.next()){
    String partNumber1 = rset.getString(1);
    String name = rset.getString(2);
    String description = rset.getString(3);
    String price = rset.getString(4);

    txtName.setText(name);
    txtDescription.setText(description);
    txtPrice.setText(price);


    } else { JOptionPane.showMessageDialog(null, "Inventory item not found");
    }
    }
    catch (SQLException ex) {
    ex.printStackTrace();
    }

    }

    private void update_actionPerformed(ActionEvent e) {

    String partNumber = txtPartNumber.getText();
    String name = txtName.getText();
    String description = txtDescription.getText();
    String price = txtPrice.getText();

    txtName.setText("");
    txtDescription.setText("");
    txtPrice.setText("");
    txtPartNumber.setText(partNumber);

    try{

    String updateString = "update Inventory "+

    " name= '"+ name +"',"+

    " description= '"+description+"',"+ "price= "+price+

    " where Inventory.partNumber = " + partNumber;

    ResultSet rset = stmt.executeQuery(toString());

    //Results

    if(rset.next()) {

    int resultInsert = stmt.executeUpdate(updateString);
    }
    else {
    JOptionPane.showMessageDialog(null,"Update NOT executed, inventory item not found");
    }
    }
    catch (SQLException ex) {
    ex.printStackTrace();
    }
    }

    private void delete_actionPerformed(ActionEvent e) {

    String partNumber = txtPartNumber.getText();

    try {
    String deleteString = "delete from Inventory where partNumber= "+partNumber;

    //Results
    int result = stmt.executeUpdate(deleteString);

    }
    catch (SQLException ex) {
    ex.printStackTrace();

    }
    }

    private void insert_actionPerformed(ActionEvent e) {

    try{
    String partNumber = txtPartNumber.getText();
    String name = txtName.getText();
    String description = txtDescription.getText();
    String price = txtPrice.getText();

    String insertString = "insert into Inventory "+

    "set partNumber = '"+ partNumber +"', name = '"+ name +"',"+

    " description= '"+description+"',"+ "price= "+price;


    System.out.println(insertString);
    txtName.setText(name);
    txtDescription.setText(description);
    txtPrice.setText(price);
    txtPartNumber.setText(partNumber);

    int result = stmt.executeUpdate(toString());


    ResultSet rset = stmt.executeQuery
    ("select * from inventory");

    if (rset.next()) {
    String partName = rset.getString(1);
    String partDescription = rset.getString(2);
    String partNumber1 = rset.getString(3);
    String partPrice = rset.getString(5);

    txtName.setText(name);
    txtDescription.setText(description);
    txtPrice.setText(price);
    txtPartNumber.setText(partNumber);

    // Display result in a dialog box
    JOptionPane.showMessageDialog(null, "Part#: " + partNumber + " will be inserted with == > " + partDescription +
    " " + partNumber + partPrice);
    } else {
    // Display result in a dialog box
    JOptionPane.showMessageDialog(null, "Update NOT executed, inventory item not found");
    }
    }
    catch (SQLException ex) {
    ex.printStackTrace();
    }
    }


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Inventory application with Read, Delete, Insert and Update Methods

    First repost your code in the [code=java][/code ] tags to maintain formatting and highlighting. Also, post the errors your are getting.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inventory application with Read, Delete, Insert and Update Methods

    How do I do that?

    private void read_actionPerformed(ActionEvent e) {

    String partNumber = txtPartNumber.getText();

    try{
    String queryString = "select partNumber, name, description, price from Inventory" +
    " where Inventory.partNumber = " + partNumber ;

    ResultSet rset = stmt.executeQuery(queryString);

    if (rset.next()){
    String partNumber1 = rset.getString(1);
    String name = rset.getString(2);
    String description = rset.getString(3);
    String price = rset.getString(4);

    txtName.setText(name);
    txtDescription.setText(description);
    txtPrice.setText(price);


    } else { JOptionPane.showMessageDialog(null, "Inventory item not found");
    }
    }
    catch (SQLException ex) {
    ex.printStackTrace();
    }

    }

    private void update_actionPerformed(ActionEvent e) {

    String partNumber = txtPartNumber.getText();
    String name = txtName.getText();
    String description = txtDescription.getText();
    String price = txtPrice.getText();

    txtName.setText("");
    txtDescription.setText("");
    txtPrice.setText("");
    txtPartNumber.setText(partNumber);

    try{

    String updateString = "update Inventory "+

    " name= '"+ name +"',"+

    " description= '"+description+"',"+ "price= "+price+

    " where Inventory.partNumber = " + partNumber;

    ResultSet rset = stmt.executeQuery(toString());

    //Results

    if(rset.next()) {

    int resultInsert = stmt.executeUpdate(updateString);
    }
    else {
    JOptionPane.showMessageDialog(null,"Update NOT executed, inventory item not found");
    }
    }
    catch (SQLException ex) {
    ex.printStackTrace();
    }
    }

    private void delete_actionPerformed(ActionEvent e) {

    String partNumber = txtPartNumber.getText();

    try {
    String deleteString = "delete from Inventory where partNumber= "+partNumber;

    //Results
    int result = stmt.executeUpdate(deleteString);

    }
    catch (SQLException ex) {
    ex.printStackTrace();

    }
    }

    private void insert_actionPerformed(ActionEvent e) {

    try{
    String partNumber = txtPartNumber.getText();
    String name = txtName.getText();
    String description = txtDescription.getText();
    String price = txtPrice.getText();

    String insertString = "insert into Inventory "+

    "set partNumber = '"+ partNumber +"', name = '"+ name +"',"+

    " description= '"+description+"',"+ "price= "+price;


    System.out.println(insertString);
    txtName.setText(name);
    txtDescription.setText(description);
    txtPrice.setText(price);
    txtPartNumber.setText(partNumber);

    int result = stmt.executeUpdate(toString());


    ResultSet rset = stmt.executeQuery
    ("select * from inventory");

    if (rset.next()) {
    String partName = rset.getString(1);
    String partDescription = rset.getString(2);
    String partNumber1 = rset.getString(3);
    String partPrice = rset.getString(5);

    txtName.setText(name);
    txtDescription.setText(description);
    txtPrice.setText(price);
    txtPartNumber.setText(partNumber);

    // Display result in a dialog box
    JOptionPane.showMessageDialog(null, "Part#: " + partNumber + " will be inserted with == > " + partDescription +
    " " + partNumber + partPrice);
    } else {
    // Display result in a dialog box
    JOptionPane.showMessageDialog(null, "Update NOT executed, inventory item not found");
    }
    }
    catch (SQLException ex) {
    ex.printStackTrace();
    }
    }


    --- Update ---

    This is one of the errors I get when I try to update the price from item partNumber 22222. Thats the only item I can read to try to update and it. When I click update it does not update and when I read it after clicking the update button I get this error:

    com.mysql.jdbc.exceptions.MySQLSyntaxErrorExceptio n: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Inventory[panel0,0,0,1024x663,invalid,layout=java.awt.Border Layout,rootPane=java' at line 1

    --- Update ---

    I also get this error when I try to insert a new entry with part number 66666, name:test, description:testagain, price:201

    com.mysql.jdbc.exceptions.MySQLSyntaxErrorExceptio n: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Inventory[panel0,0,0,1024x663,invalid,layout=java.awt.Border Layout,rootPane=java' at line 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:936)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:2870)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:15 73)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java :1665)
    at com.mysql.jdbc.Connection.execSQL(Connection.java: 3170)
    at com.mysql.jdbc.Statement.executeUpdate(Statement.j ava:1316)
    at com.mysql.jdbc.Statement.executeUpdate(Statement.j ava:1235)
    at Inventory.insert_actionPerformed(Inventory.java:20 7)
    at Inventory.access$3(Inventory.java:186)
    at Inventory$4.actionPerformed(Inventory.java:54)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Inventory application with Read, Delete, Insert and Update Methods

    Your code is still near impossible to read since you've used quote tags, not code tags. Please re-read the forum FAQ and try to get your posted code formatted well. It's extremely hard to help if we can't read the code.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inventory application with Read, Delete, Insert and Update Methods

    Is this the correct way?I am sorry. I never used this website before...I attached the .java code in a zip file if that helps...




    private void read_actionPerformed(ActionEvent e) {
     
    	  String partNumber = txtPartNumber.getText();
     
          try{
          String queryString = "select partNumber, name, description, price from Inventory" +
          " where Inventory.partNumber = " + partNumber ;
     
    	  ResultSet rset = stmt.executeQuery(queryString);
     
    	  if (rset.next()){
    		  String partNumber1 = rset.getString(1);
    		  String name = rset.getString(2);
    		  String description = rset.getString(3);
    		  String price = rset.getString(4);
     
    		  txtName.setText(name);
    		  txtDescription.setText(description);
    		  txtPrice.setText(price);
     
     
    	  } else { JOptionPane.showMessageDialog(null, "Inventory item not found");
    	  }
    	}
      catch (SQLException ex) {
    	  ex.printStackTrace();
      }
     
      }
     
      private void update_actionPerformed(ActionEvent e) {
     
    	  String partNumber = txtPartNumber.getText();
    	  String name = txtName.getText();
    	  String description = txtDescription.getText();
    	  String price = txtPrice.getText();
     
    	  txtName.setText("");
    	  txtDescription.setText("");
    	  txtPrice.setText("");
    	  txtPartNumber.setText(partNumber);
     
    	  try{
     
    	 String updateString = "update Inventory "+
     
    	  " name= '"+ name +"',"+
     
    	        " description= '"+description+"',"+ "price= "+price+
     
    	        " where Inventory.partNumber = " + partNumber;
     
    	 ResultSet rset = stmt.executeQuery(toString());
     
    	 //Results
     
    	 if(rset.next()) {
     
    	  int resultInsert = stmt.executeUpdate(updateString);
    	 } 
    	   else {
    	      JOptionPane.showMessageDialog(null,"Update NOT executed, inventory item not found");
    	  }
    	     }
    	   catch (SQLException ex) {
    	     ex.printStackTrace();
    	   }
    	 }
     
      private void delete_actionPerformed(ActionEvent e) {
     
    	    String partNumber = txtPartNumber.getText();
     
    	    try {
    	        String deleteString = "delete from Inventory where partNumber= "+partNumber;
     
        		//Results
        		int result = stmt.executeUpdate(deleteString);  
     
    	    }  
    	    catch (SQLException ex) {
                ex.printStackTrace();
     
              }
      }
     
      private void insert_actionPerformed(ActionEvent e) {
     
    	  try{
    		  String partNumber = txtPartNumber.getText();
    		  String name = txtName.getText();
    		  String description = txtDescription.getText();
    		  String price = txtPrice.getText();
     
    		  String insertString = "insert into Inventory "+
     
    		  "set partNumber = '"+ partNumber +"', name = '"+ name +"',"+
     
    		        " description= '"+description+"',"+ "price= "+price;
     
     
    		 System.out.println(insertString);
    		 txtName.setText(name);
    		 txtDescription.setText(description);
    		 txtPrice.setText(price);
    		 txtPartNumber.setText(partNumber);
     
    	     int result = stmt.executeUpdate(toString());
     
     
        ResultSet rset = stmt.executeQuery
        ("select * from inventory"); 
     
          if (rset.next()) {
            String partName = rset.getString(1);
            String partDescription = rset.getString(2);
            String partNumber1 = rset.getString(3);
            String partPrice = rset.getString(5);
     
            txtName.setText(name);
    		  txtDescription.setText(description);
    		  txtPrice.setText(price);
    		  txtPartNumber.setText(partNumber);
     
            // Display result in a dialog box
            JOptionPane.showMessageDialog(null, "Part#: " + partNumber + " will be inserted with == > " + partDescription +
              " " + partNumber + partPrice);
          } else {
            // Display result in a dialog box
            JOptionPane.showMessageDialog(null, "Update NOT executed, inventory item not found");
          }
        }
        catch (SQLException ex) {
          ex.printStackTrace();
        }
      }
    Attached Files Attached Files

  6. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inventory application with Read, Delete, Insert and Update Methods

    Can anyone help me with this code?

  7. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inventory application with Read, Delete, Insert and Update Methods

    Hello. Can anyone help me with this code? Thanks!



    private void read_actionPerformed(ActionEvent e) {
     
    	  String partNumber = txtPartNumber.getText();
     
          try{
          String queryString = "select partNumber, name, description, price from Inventory" +
          " where Inventory.partNumber = " + partNumber ;
     
    	  ResultSet rset = stmt.executeQuery(queryString);
     
    	  if (rset.next()){
    		  String partNumber1 = rset.getString(1);
    		  String name = rset.getString(2);
    		  String description = rset.getString(3);
    		  String price = rset.getString(4);
     
    		  txtName.setText(name);
    		  txtDescription.setText(description);
    		  txtPrice.setText(price);
     
     
    	  } else { JOptionPane.showMessageDialog(null, "Inventory item not found");
    	  }
    	}
      catch (SQLException ex) {
    	  ex.printStackTrace();
      }
     
      }
     
      private void update_actionPerformed(ActionEvent e) {
     
    	  String partNumber = txtPartNumber.getText();
    	  String name = txtName.getText();
    	  String description = txtDescription.getText();
    	  String price = txtPrice.getText();
     
    	  txtName.setText("");
    	  txtDescription.setText("");
    	  txtPrice.setText("");
    	  txtPartNumber.setText(partNumber);
     
    	  try{
     
    	 String updateString = "update Inventory "+
     
    	  " name= '"+ name +"',"+
     
    	        " description= '"+description+"',"+ "price= "+price+
     
    	        " where Inventory.partNumber = " + partNumber;
     
    	 ResultSet rset = stmt.executeQuery(toString());
     
    	 //Results
     
    	 if(rset.next()) {
     
    	  int resultInsert = stmt.executeUpdate(updateString);
    	 } 
    	   else {
    	      JOptionPane.showMessageDialog(null,"Update NOT executed, inventory item not found");
    	  }
    	     }
    	   catch (SQLException ex) {
    	     ex.printStackTrace();
    	   }
    	 }
     
      private void delete_actionPerformed(ActionEvent e) {
     
    	    String partNumber = txtPartNumber.getText();
     
    	    try {
    	        String deleteString = "delete from Inventory where partNumber= "+partNumber;
     
        		//Results
        		int result = stmt.executeUpdate(deleteString);  
     
    	    }  
    	    catch (SQLException ex) {
                ex.printStackTrace();
     
              }
      }
     
      private void insert_actionPerformed(ActionEvent e) {
     
    	  try{
    		  String partNumber = txtPartNumber.getText();
    		  String name = txtName.getText();
    		  String description = txtDescription.getText();
    		  String price = txtPrice.getText();
     
    		  String insertString = "insert into Inventory "+
     
    		  "set partNumber = '"+ partNumber +"', name = '"+ name +"',"+
     
    		        " description= '"+description+"',"+ "price= "+price;
     
     
    		 System.out.println(insertString);
    		 txtName.setText(name);
    		 txtDescription.setText(description);
    		 txtPrice.setText(price);
    		 txtPartNumber.setText(partNumber);
     
    	     int result = stmt.executeUpdate(toString());
     
     
        ResultSet rset = stmt.executeQuery
        ("select * from inventory"); 
     
          if (rset.next()) {
            String partName = rset.getString(1);
            String partDescription = rset.getString(2);
            String partNumber1 = rset.getString(3);
            String partPrice = rset.getString(5);
     
            txtName.setText(name);
    		  txtDescription.setText(description);
    		  txtPrice.setText(price);
    		  txtPartNumber.setText(partNumber);
     
            // Display result in a dialog box
            JOptionPane.showMessageDialog(null, "Part#: " + partNumber + " will be inserted with == > " + partDescription +
              " " + partNumber + partPrice);
          } else {
            // Display result in a dialog box
            JOptionPane.showMessageDialog(null, "Update NOT executed, inventory item not found");
          }
        }
        catch (SQLException ex) {
          ex.printStackTrace();
        }
      }
    [/QUOTE]

Similar Threads

  1. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM
  2. Replies: 2
    Last Post: August 31st, 2012, 12:36 PM
  3. Replies: 1
    Last Post: June 29th, 2012, 01:29 PM
  4. Insert+Update+Delete in one connections?
    By Hurricane in forum JDBC & Databases
    Replies: 5
    Last Post: February 27th, 2012, 10:13 AM
  5. Simple inventory System. Sometimes it doesn't update my added book.
    By JustinK in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 1st, 2011, 10:50 AM