简体   繁体   中英

Inserting data from TextField into Mysql database

I want to input the data from eclipse into the table (item) of Mysql , which stores the values of item code, item name, item description, item price, item cost and item quantity. I found the following codes, are these valid and if it is, where should I place these codes?

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory","root", "1234");    // connect to database server based on localhost
PreparedStatement smt = con.prepareStatement("insert into item ( ItemCode = ?, ItemName = ?,ItemDescription = ?, ItemQuantity = ?, ItemPrice = ?, ItemCost = ? ");
smt.setString(1, code);
smt.setString(2, name);
smt.setString(3, description);
smt.setString(4, quantity);
smt.setString(5, price);
smt.setString(6, cost);
ResultSet rs = smt.executeQuery();

Here is my function:

package test;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class Create_Item extends JPanel implements ActionListener {
    JTextField ItemCode, ItemName, ItemDescription, ItemQuantity, ItemPrice, ItemCost;
    JButton jbtSubmit;

    public Create_Item() {

        // java.awt.Container container = getContentPane();
        setLayout(new BorderLayout(5, 5));
        JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayout(6, 2, 5, 5));

        JLabel label1 = new JLabel("Item Code:");
        panel1.add(label1);
        ItemCode = new JTextField();
        panel1.add(ItemCode);

        JLabel label2 = new JLabel("Item Name:");
        panel1.add(label2);
        ItemName = new JTextField();
        panel1.add(ItemName);

        JLabel label3 = new JLabel("Item Description:");
        panel1.add(label3);
        ItemDescription = new JTextField();
        panel1.add(ItemDescription);

        JLabel label4 = new JLabel("Item Quantity:");
        panel1.add(label4);
        ItemQuantity = new JTextField();
        panel1.add(ItemQuantity);

        JLabel label5 = new JLabel("Item Price:");
        panel1.add(label5);
        ItemPrice = new JTextField();
        panel1.add(ItemPrice);

        JLabel label6 = new JLabel("Item Cost:");
        panel1.add(label6);
        ItemCost = new JTextField();
        panel1.add(ItemCost);

        add(panel1, BorderLayout.CENTER);
        JPanel panel2 = new JPanel();
        jbtSubmit = new JButton("Create Item");
        panel2.add(jbtSubmit);
        add(panel2, BorderLayout.SOUTH);
        jbtSubmit.addActionListener(this);


    }

    public void actionPerformed(ActionEvent e) {
        {
        String code = ItemCode.getText();
        String name = ItemName.getText();
        String description = ItemDescription.getText();
        String quantity = ItemQuantity.getText();
        String price = ItemPrice.getText();
        String cost = ItemCost.getText();

        if (code.equals("")) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JOptionPane.showMessageDialog(frame, "Item Code is missing!");
            ItemCode.requestFocusInWindow();
        }

        else if (name.equals("")) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JOptionPane.showMessageDialog(frame, "Item Name is missing!");
            ItemName.requestFocusInWindow();
        }

        else if (description.equals("")) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JOptionPane.showMessageDialog(frame, "Item Description is missing!");
            ItemQuantity.requestFocusInWindow();
        }

        else if (quantity.equals("")) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JOptionPane.showMessageDialog(frame, "Item Quantity is missing!");
            ItemPrice.requestFocusInWindow();
        }

        else if (price.equals("")) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JOptionPane.showMessageDialog(frame, "Item Price is missing!");
            ItemCost.requestFocusInWindow();
        }

        else if (cost.equals("")) {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JOptionPane.showMessageDialog(frame, "Item Cost is missing!");
            ItemCost.requestFocusInWindow();
        }
        }
    }
}

Your insert query should be like

PreparedStatement smt = con.prepareStatement("insert into item ( ItemCode , ItemName ,
ItemDescription , ItemQuantity, ItemPrice , ItemCost) values(?,?,?,?,?,?) ");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM