返回重复项

huangapple go评论63阅读模式
英文:

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(&quot;Approve&quot;);
ApproveButton.setFont(new Font(&quot;Tahoma&quot;, Font.PLAIN, 13));
ApproveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try{
int rows=table.getRowCount();
Class.forName(&quot;com.mysql.jdbc.Driver&quot;).newInstance();
Connection connection = DriverManager.getConnection(&quot;jdbc:mysql://localhost:3306/swing_demo&quot;, &quot;root123&quot;, &quot;root12345&quot;);
PreparedStatement upd = connection.prepareStatement(&quot;INSERT INTO approvedsales(productname, quantity, price, total, date) VALUES (?, ?,?,?,? )&quot;);
for(int row = 0; row&lt;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, &quot;This is alredy exist&quot;);
} else {
JOptionPane.showMessageDialog(ApproveButton,
&quot;Data are successfully stored&quot;);
}}
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.

huangapple
  • 本文由 发表于 2020年9月7日 21:00:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63778126.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定