英文:
Return duplicates
问题
ApproveButton = new JButton("Approve");
ApproveButton.setFont(new Font("Tahoma", Font.PLAIN, 13));
ApproveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int rows = table.getRowCount();
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/swing_demo", "root123", "root12345");
PreparedStatement upd = connection.prepareStatement("INSERT INTO approvedsales(productname, quantity, price, total, date) VALUES (?, ?,?,?,? )");
for (int row = 0; row < rows; row++) {
String productname = (String) table.getValueAt(row, 0);
String quantity = (String) table.getValueAt(row, 1);
String price = (String) table.getValueAt(row, 2);
String total = (String) table.getValueAt(row, 3);
String date = (String) table.getValueAt(row, 4);
upd.setString(1, productname);
upd.setString(2, quantity);
upd.setString(3, price);
upd.setString(4, total);
upd.setString(5, date);
upd.addBatch();
}
upd.executeBatch();
int x = upd.executeUpdate();
if (x == 0) {
JOptionPane.showMessageDialog(ApproveButton, "This is already exist");
} else {
JOptionPane.showMessageDialog(ApproveButton,
"Data are successfully stored");
}
} catch (Exception exception) {
exception.printStackTrace();
}
}
});
ApproveButton.setBounds(129, 204, 89, 23);
contentPane.add(ApproveButton);
数据库中的表,所有列都是varchar(255),没有唯一和主键
productname quantity price total date
biscuits 1 1 1 7th Sept 2020
英文:
I tried to insert my data into my database using this code but I kept on getting duplicates and I can't seem to figure out how to fix it. I am very new at Java and MySQL so any help or guidance are very much appreciated.
Below are my codes
ApproveButton = new JButton("Approve");
ApproveButton.setFont(new Font("Tahoma", Font.PLAIN, 13));
ApproveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
int rows=table.getRowCount();
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/swing_demo", "root123", "root12345");
PreparedStatement upd = connection.prepareStatement("INSERT INTO approvedsales(productname, quantity, price, total, date) VALUES (?, ?,?,?,? )");
for(int row = 0; row<rows; row++)
{
String productname = (String)table.getValueAt(row, 0);
String quantity = (String)table.getValueAt(row, 1);
String price = (String)table.getValueAt(row, 2);
String total = (String)table.getValueAt(row, 3);
String date = (String)table.getValueAt(row, 4);
upd.setString(1, productname);
upd.setString(2, quantity);
upd.setString(3, price);
upd.setString(4, total);
upd.setString(5, date);
upd.addBatch();
}
upd.executeBatch();
int x = upd.executeUpdate();
if (x == 0) {
JOptionPane.showMessageDialog(ApproveButton, "This is alredy exist");
} else {
JOptionPane.showMessageDialog(ApproveButton,
"Data are successfully stored");
}}
catch(Exception exception){
exception.printStackTrace();
}
}});
ApproveButton.setBounds(129, 204, 89, 23);
contentPane.add(ApproveButton);
My table in database, all of the columns are varchar(255), no unique and primary keys
productname quantity price total date
biscuits 1 1 1 7th Sept 2020
答案1
得分: 1
仅使用Statement#executeBatch,而不是使用executeUpdate。
int[] xs = upd.executeBatch();
if (IntStream.of(xs).min().orElse(1) == 0) { ...
同时执行executeUpdate会导致SQL语句执行两次。
当然,在表的列定义中使用主键(PRIMARY KEY)会更好地处理产品(product)。
此外,MySQL可以将INSERT与UPDATE结合起来,替代方案是REPLACE - 我相信是这个名字。
英文:
Only use Statement#executeBatch, not executeUpdate.
int[] xs = upd.executeBatch();
if (IntStream.of(xs).min().orElse(1) == 0) { ...
By also doing an executeUpdate the SQL is executed twice.
Of course a PRIMARY KEY in the table's column definition for product would have been nice.
Furthermore MySQL can combine INSERT with an UPDATE alternative, REPLACE is - I believe - the name.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论