我想将eclipse中的数据输入到Mysql的表(项目)中,该表存储项目代码,项目名称,项目描述,项目价格,项目成本和项目数量的值。我发现以下代码,这些是否有效,如果是,我应该在哪里放置这些代码?
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();
这是我的功能:
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();
}
}
}
}
答案 0 :(得分:0)
您的插入查询应该像
PreparedStatement smt = con.prepareStatement("insert into item ( ItemCode , ItemName ,
ItemDescription , ItemQuantity, ItemPrice , ItemCost) values(?,?,?,?,?,?) ");