Hello everybody, I am new to Java, I am creating a simple application that will insert into my database, but what I really want is, I have a class called product which contains the jTextfields of the product description and product qty and a button called test, and I also have another class called insert class which contains the code to insert into the database.

My main problem here is, I created a method called insertBotton in the insert class and in the product class i called the insertBotton method and it is suppose to insert any data entered in that product description and product qty Jtextfields into the database, the problem here is it will pass an empty field to the insert class making it to have an error saying

java.lang.NumberFormatException: For input string: ""

Just want my insert code to be in a different class so i can call it anytime i want


Please you guys should help me solve this problem


You can download the files from http://www.maxinville.com/bucky.zip

Files Inside:
*the database
*the project files (Created with Netbeans)

Database username: java

Password: 1234

product.java and insert.java are the 2 main files to work with and in the Product Class you use the test button to work all other buttons are working well except the test button where i want the action to be.

Thanks

Attachments
-- phpMyAdmin SQL Dump
-- version 3.3.9
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Mar 09, 2012 at 11:31 PM
-- Server version: 5.1.41
-- PHP Version: 5.2.9-2

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `myapp`
--

-- --------------------------------------------------------

--
-- Table structure for table `login`
--

CREATE TABLE IF NOT EXISTS `login` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `fname` varchar(50) NOT NULL,
  `uname` varchar(50) NOT NULL,
  `pass` varchar(50) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uname` (`uname`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;

--
-- Dumping data for table `login`
--

INSERT INTO `login` (`id`, `fname`, `uname`, `pass`) VALUES
(1, 'Kelvin Rolex Services Limited', 'kelvinrolex', '1234'),
(2, 'Maxinville Technologies Limited', 'maxinville', '1234'),
(6, 'KelvinROLEX', 'rolex', 'inform'),
(7, 'Kelvin Forteta', 'forteta', 'inform');

-- --------------------------------------------------------

--
-- Table structure for table `product`
--

CREATE TABLE IF NOT EXISTS `product` (
  `pid` int(11) NOT NULL AUTO_INCREMENT,
  `pdesc` varchar(55) NOT NULL,
  `qty` int(11) NOT NULL,
  `datez` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`pid`),
  UNIQUE KEY `pdesc` (`pdesc`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=31 ;

--
-- Dumping data for table `product`
--

INSERT INTO `product` (`pid`, `pdesc`, `qty`, `datez`) VALUES
(30, 'Blckberry Bold 5', 45, '2012-03-09 23:30:57'),
(29, 'LG Phone', 23, '2012-03-09 23:30:42');
import java.sql.*;
import javax.swing.JOptionPane;

public class insert {
    String driver ="com.mysql.jdbc.Driver";
    String url ="jdbc:mysql://localhost:3306/myapp";
    String user = "java";
    String pwd = "1234";
    
    product pr = new product();
    
    public void insertBotton()
            
         {
        
       try
      {
         String sql = "INSERT INTO product (pid, pdesc, qty)" + "VALUES (NULL, ?,?)";
         Class.forName(driver);
         Connection con = DriverManager.getConnection(url, user, pwd);
         //Statement st = con.createStatement();
         PreparedStatement ps = con.prepareStatement(sql);
         ps.setString(1, pr.desc.getText());
         ps.setInt(2, Integer.parseInt(pr.qty.getText()));
         int a = ps.executeUpdate();
         if(a == 1)
         {
            JOptionPane.showMessageDialog(null, "Insert was successful");
         }
 else {
       JOptionPane.showMessageDialog(null, "Insert failed");
 }
      }
      catch(Exception e) {
         System.out.println(e);

      }
    }                              
    }
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * product.java
 *
 * Created on Mar 2, 2012, 11:37:10 AM
 */

/**
 *
 * @author Administrator
 */
public class product extends javax.swing.JFrame
{     
        String driver ="com.mysql.jdbc.Driver";
        String url ="jdbc:mysql://localhost:3306/myapp";
        String user = "java";
        String pwd = "1234";
        Statement st = null;
        Vector header;
        Vector content;
        String dx ="";
        String qx ="";
        
    /** Creates new form product */
    public product() {
        header = new Vector();
        header.add("DATE");
        header.add("PRODUCT NAME");
        header.add("QUANTIY");
        try
        {
        table t = new table();
        content = t.list();
        }
        catch(Exception e)
        {System.out.println(e);}
        
        initComponents();
        
        combo();
    }
public void connect()
    {
      try
      {
          Class.forName(driver);
          Connection con = DriverManager.getConnection(url, user, pwd);
          st = con.createStatement();
      }
      catch (Exception e)
     {
          System.out.println(e);
     }
    }
public void combo()
    {
    try
    {
      String sql = "SELECT * FROM product ORDER BY pdesc ASC";
      connect();
      ResultSet rs = st.executeQuery(sql);
      com.addItem("Select Product");
      while (rs.next())
      {
          String des = rs.getString("pdesc").toUpperCase();
          com.addItem(des);
      }
    }
    catch(Exception e)
    {
      System.out.println(e);
    }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lab = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        com = new javax.swing.JComboBox();
        id = new javax.swing.JTextField();
        desc = new javax.swing.JTextField();
        qty = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton7 = new javax.swing.JButton();
        jLayeredPane3 = new javax.swing.JLayeredPane();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        fn = new javax.swing.JTextField();
        us = new javax.swing.JTextField();
        pass = new javax.swing.JPasswordField();
        jLabel7 = new javax.swing.JLabel();
        rpass = new javax.swing.JPasswordField();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Product App");
        setBounds(new java.awt.Rectangle(425, 150, 0, 0));

        jLayeredPane1.setBackground(new java.awt.Color(255, 255, 255));
        jLayeredPane1.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        jLayeredPane1.setForeground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Product ID:");
        jLabel1.setBounds(150, 70, 70, 14);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setText("Description:");
        jLabel2.setBounds(150, 110, 70, 20);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setText("Quantity:");
        jLabel3.setBounds(160, 150, 60, 14);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        com.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comActionPerformed(evt);
            }
        });
        com.setBounds(390, 10, 140, 20);
        jLayeredPane1.add(com, javax.swing.JLayeredPane.DEFAULT_LAYER);

        id.setEditable(false);
        id.setBounds(260, 70, 70, 20);
        jLayeredPane1.add(id, javax.swing.JLayeredPane.DEFAULT_LAYER);

        desc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                descActionPerformed(evt);
            }
        });
        desc.setBounds(260, 110, 160, 20);
        jLayeredPane1.add(desc, javax.swing.JLayeredPane.DEFAULT_LAYER);

        qty.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                qtyActionPerformed(evt);
            }
        });
        qty.setBounds(260, 150, 40, 20);
        jLayeredPane1.add(qty, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton1.setText("Insert");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.setBounds(120, 210, 90, 23);
        jLayeredPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton2.setText("Update");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jButton2.setBounds(220, 210, 90, 23);
        jLayeredPane1.add(jButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton3.setText("Delete");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jButton3.setBounds(320, 210, 90, 23);
        jLayeredPane1.add(jButton3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton4.setText("Reset");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jButton4.setBounds(420, 210, 70, 23);
        jLayeredPane1.add(jButton4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton8.setText("test");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });
        jButton8.setBounds(400, 160, 100, 23);
        jLayeredPane1.add(jButton8, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTabbedPane1.addTab("Home", jLayeredPane1);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            content, header
        ));
        jScrollPane1.setViewportView(jTable1);

        jScrollPane1.setBounds(0, 0, 610, 230);
        jLayeredPane2.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton7.setText("Refresh Records");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jButton7.setBounds(363, 240, 130, 23);
        jLayeredPane2.add(jButton7, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTabbedPane1.addTab("Product List", jLayeredPane2);

        jLabel4.setText("Username:");
        jLabel4.setBounds(130, 70, 80, 20);
        jLayeredPane3.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setText("Full name:");
        jLabel5.setBounds(130, 40, 90, 14);
        jLayeredPane3.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel6.setText("Password:");
        jLabel6.setBounds(130, 100, 100, 20);
        jLayeredPane3.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);
        fn.setBounds(260, 40, 150, 20);
        jLayeredPane3.add(fn, javax.swing.JLayeredPane.DEFAULT_LAYER);
        us.setBounds(260, 70, 100, 20);
        jLayeredPane3.add(us, javax.swing.JLayeredPane.DEFAULT_LAYER);
        pass.setBounds(260, 100, 110, 20);
        jLayeredPane3.add(pass, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel7.setText("Retype Password:");
        jLabel7.setBounds(90, 140, 140, 14);
        jLayeredPane3.add(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);
        rpass.setBounds(260, 130, 110, 20);
        jLayeredPane3.add(rpass, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton5.setText("Create User");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jButton5.setBounds(210, 190, 120, 23);
        jLayeredPane3.add(jButton5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTabbedPane1.addTab("New Registration", jLayeredPane3);

        jButton6.setText("Exit");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(

problem here is it will pass an empty field

If the value is a String the String class has methods you can use to detect an empty String.

Please post any code you have questions about here on the forum. Be sure to wrap the code in code tags. Use the (code) icon above the input box.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

product.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * product.java
 *
 * Created on Mar 2, 2012, 11:37:10 AM
 */

/**
 *
 * @author Administrator
 */
public class product extends javax.swing.JFrame
{     
        String driver ="com.mysql.jdbc.Driver";
        String url ="jdbc:mysql://localhost:3306/myapp";
        String user = "java";
        String pwd = "1234";
        Statement st = null;
        Vector header;
        Vector content;
        String dx ="";
        String qx ="";
        
    /** Creates new form product */
    public product() {
        header = new Vector();
        header.add("DATE");
        header.add("PRODUCT NAME");
        header.add("QUANTIY");
        try
        {
        table t = new table();
        content = t.list();
        }
        catch(Exception e)
        {System.out.println(e);}
        
        initComponents();
        
        combo();
    }
public void connect()
    {
      try
      {
          Class.forName(driver);
          Connection con = DriverManager.getConnection(url, user, pwd);
          st = con.createStatement();
      }
      catch (Exception e)
     {
          System.out.println(e);
     }
    }
public void combo()
    {
    try
    {
      String sql = "SELECT * FROM product ORDER BY pdesc ASC";
      connect();
      ResultSet rs = st.executeQuery(sql);
      com.addItem("Select Product");
      while (rs.next())
      {
          String des = rs.getString("pdesc").toUpperCase();
          com.addItem(des);
      }
    }
    catch(Exception e)
    {
      System.out.println(e);
    }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lab = new javax.swing.JLabel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        com = new javax.swing.JComboBox();
        id = new javax.swing.JTextField();
        desc = new javax.swing.JTextField();
        qty = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton7 = new javax.swing.JButton();
        jLayeredPane3 = new javax.swing.JLayeredPane();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        fn = new javax.swing.JTextField();
        us = new javax.swing.JTextField();
        pass = new javax.swing.JPasswordField();
        jLabel7 = new javax.swing.JLabel();
        rpass = new javax.swing.JPasswordField();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Product App");
        setBounds(new java.awt.Rectangle(425, 150, 0, 0));

        jLayeredPane1.setBackground(new java.awt.Color(255, 255, 255));
        jLayeredPane1.setBorder(javax.swing.BorderFactory.createCompoundBorder());
        jLayeredPane1.setForeground(new java.awt.Color(255, 255, 255));

        jLabel1.setText("Product ID:");
        jLabel1.setBounds(150, 70, 70, 14);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setText("Description:");
        jLabel2.setBounds(150, 110, 70, 20);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setText("Quantity:");
        jLabel3.setBounds(160, 150, 60, 14);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        com.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comActionPerformed(evt);
            }
        });
        com.setBounds(390, 10, 140, 20);
        jLayeredPane1.add(com, javax.swing.JLayeredPane.DEFAULT_LAYER);

        id.setEditable(false);
        id.setBounds(260, 70, 70, 20);
        jLayeredPane1.add(id, javax.swing.JLayeredPane.DEFAULT_LAYER);

        desc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                descActionPerformed(evt);
            }
        });
        desc.setBounds(260, 110, 160, 20);
        jLayeredPane1.add(desc, javax.swing.JLayeredPane.DEFAULT_LAYER);

        qty.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                qtyActionPerformed(evt);
            }
        });
        qty.setBounds(260, 150, 40, 20);
        jLayeredPane1.add(qty, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton1.setText("Insert");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jButton1.setBounds(120, 210, 90, 23);
        jLayeredPane1.add(jButton1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton2.setText("Update");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jButton2.setBounds(220, 210, 90, 23);
        jLayeredPane1.add(jButton2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton3.setText("Delete");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jButton3.setBounds(320, 210, 90, 23);
        jLayeredPane1.add(jButton3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton4.setText("Reset");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jButton4.setBounds(420, 210, 70, 23);
        jLayeredPane1.add(jButton4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton8.setText("test");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });
        jButton8.setBounds(400, 160, 100, 23);
        jLayeredPane1.add(jButton8, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTabbedPane1.addTab("Home", jLayeredPane1);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            content, header
        ));
        jScrollPane1.setViewportView(jTable1);

        jScrollPane1.setBounds(0, 0, 610, 230);
        jLayeredPane2.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton7.setText("Refresh Records");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jButton7.setBounds(363, 240, 130, 23);
        jLayeredPane2.add(jButton7, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTabbedPane1.addTab("Product List", jLayeredPane2);

        jLabel4.setText("Username:");
        jLabel4.setBounds(130, 70, 80, 20);
        jLayeredPane3.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setText("Full name:");
        jLabel5.setBounds(130, 40, 90, 14);
        jLayeredPane3.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel6.setText("Password:");
        jLabel6.setBounds(130, 100, 100, 20);
        jLayeredPane3.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);
        fn.setBounds(260, 40, 150, 20);
        jLayeredPane3.add(fn, javax.swing.JLayeredPane.DEFAULT_LAYER);
        us.setBounds(260, 70, 100, 20);
        jLayeredPane3.add(us, javax.swing.JLayeredPane.DEFAULT_LAYER);
        pass.setBounds(260, 100, 110, 20);
        jLayeredPane3.add(pass, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel7.setText("Retype Password:");
        jLabel7.setBounds(90, 140, 140, 14);
        jLayeredPane3.add(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);
        rpass.setBounds(260, 130, 110, 20);
        jLayeredPane3.add(rpass, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jButton5.setText("Create User");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jButton5.setBounds(210, 190, 120, 23);
        jLayeredPane3.add(jButton5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jTabbedPane1.addTab("New Registration", jLayeredPane3);

        jButton6.setText("Exit");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 614, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addComponent(lab, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 287, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 280, Short.MAX_VALUE)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(275, 275, 275))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(lab, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton6)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void qtyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_qtyActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_qtyActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
     try
   {
      String d = com.getSelectedItem().toString();
      connect();
      String sql = "DELETE FROM product WHERE pdesc = '"+d+"'";
      connect();
           int a = st.executeUpdate(sql);

           if (a == 1)
           {
               JOptionPane.showMessageDialog(null, "The Product was successfully Deleted");
               desc.setText("");
               qty.setText("");
               id.setText("");
               // com.addItem(de);
               com.removeAllItems();
               combo();
               table t = new table();
            content = t.list();
            jTable1 = new javax.swing.JTable();

            jTable1.setModel(new javax.swing.table.DefaultTableModel(
            content , header
            ));
            jScrollPane1.setViewportView(jTable1);
           }
 else {
         JOptionPane.showMessageDialog(null, "Please select a product to delete");
 }
      
   }
   catch (Exception e)
   {
       System.out.println(e);
   }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void comActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comActionPerformed
   try
   {
      String d = com.getSelectedItem().toString();
      connect();
      String sql = "SELECT * FROM product WHERE pdesc = '"+d+"'";
      ResultSet rs = st.executeQuery(sql);
      while(rs.next())
      {
          id.setText(rs.getInt("pid")+"");
          desc.setText(rs.getString("pdesc"));
          qty.setText(rs.getInt("qty")+"");
      }
      dx = desc.getText();
      qx = qty.getText();
   }
   catch (Exception e)
   {
       System.out.println(e);
   }
    }//GEN-LAST:event_comActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        try
        {
           String ss;
           ss = desc.getText();
           int bh;
           bh = ss.length();
           if (bh >= 3 )
           {
           String de = desc.getText();
           int qt = Integer.parseInt(qty.getText());
           String sql = "INSERT INTO product (pid, pdesc, qty)"+ "VALUES (NULL, '"+de+"', "+qt+")";
           connect();
           int a = st.executeUpdate(sql);
          
           
           
           
           if (a == 1)
           {
               JOptionPane.showMessageDialog(null, "The Product was successfully Created");
               desc.setText("");
               qty.setText("");
               id.setText("");
               // com.addItem(de);
               com.removeAllItems();
               combo();
               table t = new table();
            content = t.list();
            jTable1 = new javax.swing.JTable();

            jTable1.setModel(new javax.swing.table.DefaultTableModel(
            content , header
            ));
            jScrollPane1.setViewportView(jTable1);
           }
 else {
           JOptionPane.showMessageDialog(null, "Insert Failed");
 }}
 else {
JOptionPane.showMessageDialog(null, "Product must be equal or more than 3 characters");
      
 }
        }
        catch(java.lang.NumberFormatException x)
        {
            JOptionPane.showMessageDialog(null, "Invalid Qty");
        }
        catch(com.mysql.jdbc.MysqlDataTruncation xx)
        {
            JOptionPane.showMessageDialog(null, "Product Description is too long");
        }
        catch (com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException z)
        {
          JOptionPane.showMessageDialog(null, "Product already exist");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }


    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed

        id.setText("");
        desc.setText("");
        qty.setText("");
        com.removeAllItems();
        combo();
        
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

        try
        {
           String ss;
           ss = desc.getText();
           int bh;
           bh = ss.length();
           if (bh >= 3 )
           {
           String d = com.getSelectedItem().toString();
           connect();
           String de = desc.getText();
           int qt = Integer.parseInt(qty.getText());
           String sql = "UPDATE product SET pdesc = '"+de+"', qty = '"+qt+"' WHERE pdesc ='"+d+"'";
           connect();
           int a = st.executeUpdate(sql);
           if (a == 1)
           {
               JOptionPane.showMessageDialog(null, "The Product was successfully Updated from "+dx +" to "+ de +" from " +qx +" to " + qt + " quantity(s)");
              com.removeAllItems();
              combo();
              table t = new table();
            content = t.list();
            jTable1 = new javax.swing.JTable();

            jTable1.setModel(new javax.swing.table.DefaultTableModel(
            content , header
            ));
            jScrollPane1.setViewportView(jTable1);
           }
 else {
           JOptionPane.showMessageDialog(null, "Update Failed");
 }
        }
           
 else {
JOptionPane.showMessageDialog(null, "Product must be equal or more than 3 characters");
      
 }
        }
catch(java.lang.NumberFormatException x)
        {
            JOptionPane.showMessageDialog(null, "Invalid Qty");
        }
        catch (com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException z)
        {
          JOptionPane.showMessageDialog(null, "Product already exist");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed

        try
           {
           String x, y, z, yy;
           x = fn.getText();
           y = us.getText();
           z = pass.getText();
           yy = rpass.getText();
           int u, v, w;
           u = x.length();
           v = y.length();
           w = z.length();
           if (u >= 3 && v >= 3 && w >= 3)
           {
           if (z .equals(yy))
           {
           String fnx = fn.getText();
           String unx = us.getText();
           String pasx = pass.getText();
           String sql = "INSERT INTO login (id, fname, uname, pass)"+ "VALUES (NULL, '"+fnx+"', '"+unx+"','"+pasx+"')";
           connect();
           int a = st.executeUpdate(sql);

           if (a == 1)
           {
               JOptionPane.showMessageDialog(null, "The User was successfully Created");
               fn.setText("");
               us.setText("");
               pass.setText("");
               rpass.setText("");
               

        }

 else{
           JOptionPane.showMessageDialog(null, "Creation failed");
 }}else{
           JOptionPane.showMessageDialog(null, "Passwords do not match");
 }
        }else {
               JOptionPane.showMessageDialog(null, "All fields are required");
        }}

        catch (com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException sd)
        {
             JOptionPane.showMessageDialog(null, "Sorry the Username already exist");
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
       System.exit(0);
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
      try
        {
            table t = new table();
            content = t.list();
            jTable1 = new javax.swing.JTable();

            jTable1.setModel(new javax.swing.table.DefaultTableModel(
            content , header
            ));
            jScrollPane1.setViewportView(jTable1);
           // jTable1.addRowSelectionInterval(0, 6);

        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }//GEN-LAST:event_jButton7ActionPerformed

   [B] private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed
       insert tuna = new insert();
       tuna.insertBotton();[/B]
       
    }//GEN-LAST:event_jButton8ActionPerformed

    private void descActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_descActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_descActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new product().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox com;
    public javax.swing.JTextField desc;
    private javax.swing.JTextField fn;
    private javax.swing.JTextField id;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    public javax.swing.JButton jButton8;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JLayeredPane jLayeredPane3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable1;
    public javax.swing.JLabel lab;
    private javax.swing.JPasswordField pass;
    public javax.swing.JTextField qty;
    private javax.swing.JPasswordField rpass;
    private javax.swing.JTextField us;
    // End of variables declaration//GEN-END:variables

}

insert.java

import java.sql.*;
import javax.swing.JOptionPane;

public class insert {
    String driver ="com.mysql.jdbc.Driver";
    String url ="jdbc:mysql://localhost:3306/myapp";
    String user = "java";
    String pwd = "1234";
    
    product pr = new product();
    
    public void insertBotton()
            
         {
        
       try
      {
         String sql = "INSERT INTO product (pid, pdesc, qty)" + "VALUES (NULL, ?,?)";
         Class.forName(driver);
         Connection con = DriverManager.getConnection(url, user, pwd);
         //Statement st = con.createStatement();
         PreparedStatement ps = con.prepareStatement(sql);
         ps.setString(1, pr.desc.getText());
         ps.setInt(2, Integer.parseInt(pr.qty.getText()));
         int a = ps.executeUpdate();
         if(a == 1)
         {
            JOptionPane.showMessageDialog(null, "Insert was successful");
         }
 else {
       JOptionPane.showMessageDialog(null, "Insert failed");
 }
      }
      catch(Exception e) {
         System.out.println(e);

      }
    }                              
    }

Where are the questions about the code?
Where do you get the error?

Did you look at the String class's methods for detecting an empty String?

Edited 4 Years Ago by NormR1: n/a

I haven't time to read all that code, but normally in the actionPerformed you would get the text from the local data entry fields and pass those values as parameters to the insertBotton method. That way the insert class doesn't need to know anything about the data entry fields or any of the variables within the product class.

don't really know how to do that, like i said i am new to Java...

I get error when I fill in detail to the description and qty jtextfield and click on the test button to submit it to the database.

the text button that do call the insert class is

private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      
        insert in = new insert();
        in.insertBotton();

then this is the insert class that is suppose to insert the input to the database

import java.sql.*;
import javax.swing.JOptionPane;

public class insert{
    String driver ="com.mysql.jdbc.Driver";
    String url ="jdbc:mysql://localhost:3306/myapp";
    String user = "java";
    String pwd = "1234";
    
    
    product pr = new product();
     
    
    public void insertBotton()
            
         {
        
       try
      {
         String sql = "INSERT INTO product (pid, pdesc, qty)" + "VALUES (NULL, ?,?)";
         Class.forName(driver);
         Connection con = DriverManager.getConnection(url, user, pwd);
         //Statement st = con.createStatement();
         PreparedStatement ps = con.prepareStatement(sql);
         ps.setString(1, pr.desc.getText());
         ps.setInt(2, Integer.parseInt(pr.qty.getText()));
         int a = ps.executeUpdate();
         if(a == 1)
         {
            JOptionPane.showMessageDialog(null, "Insert was successful");
         }
 else {
       JOptionPane.showMessageDialog(null, "Insert failed");
 }
      }
      catch(Exception e) {
         System.out.println(e);

      }
    }                              
    }

I haven't time to read all that code, but normally in the actionPerformed you would get the text from the local data entry fields and pass those values as parameters to the insertBotton method. That way the insert class doesn't need to know anything about the data entry fields or any of the variables within the product class.

Please can you help me write some code to do that cos i'm new to Java...

Do you expect that the new instance of the Product class you create in the Insert class will have any data in it? Is the data you want in another instance of the Product class?

You could pass arguments to the insertBotton() method when you call it from the listener method. What values does the insertBotton() method need?

i'm new to Java...

Where did you get these hundreds of lines of code?

Edited 4 Years Ago by NormR1: n/a

Typically the OK button's action handler looks something like this

actionPerformed...
   String name = nameTextField.getText();
   String dateOfBirth = dateOfBirthTextField.getText();
   insertInstance.insertPerson(name, dateOfBirth);

and the method to do the insert in the Insert class looks like

public void insertPerson(String name, String dob) {
   ... insert name amd dob into database

Edited 4 Years Ago by JamesCherrill: n/a

You could test the contents of the data before passing it and issue a message if it is missing or bad.

Typically the OK button's action handler looks something like this

actionPerformed...
   String name = nameTextField.getText();
   String dateOfBirth = dateOfBirthTextField.getText();
   insertInstance.insertPerson(name, dateOfBirth);

and the method to do the insert in the Insert class looks like

public void insertPerson(String name, String dob) {
   ... insert name amd dob into database

Thank you so much it is finally working with the steps you gave me...

Thank you also @NormR1 for your contributions, i really appreciate...

This question has already been answered. Start a new discussion instead.