从Java GUI中删除时,记录不会从MySQL数据库的表中删除吗?

时间:2014-10-23 19:13:45

标签: java mysql database swing jtable

已编辑的问题

当我单击删除按钮时,表中的行将在GUI中删除,但不会从mysql服务器中的数据库中删除。

以下是代码:

// DatabaseStore。这部分运行正常。

public class DatabaseStore {

     private final String server = "jdbc:mysql://localhost/";
    private final String database = "music_magic";
    private final String user_name = "root";
    private final String pass_word = "";

    private final String driver = "com.mysql.jdbc.Driver";

    public Connection doConnection() {
        Connection c;
        try {
            //load the driver
            Class.forName(driver);
            c = DriverManager.getConnection(server + database, user_name, pass_word);
           // JOptionPane.showMessageDialog(null, "Database connected");
        } catch (Exception e) {
            c = null;
            JOptionPane.showMessageDialog(null, "Error : " + e.getMessage());
        }
        return c;
    }

//

// Imports
import Database_music.DatabaseStore; //main database page where i connect to database
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import magic_music.Items; //ignore this
//...

// Subject method
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  // TODO add your handling code here:
  if(jTable1.getSelectedRow() >=0){
    try{
      DatabaseStore dtbs = new DatabaseStore();
      Connection cn = dtbs.doConnection();
      Statement stat = cn.createStatement();
      String sql = "DELETE FROM products_info WHERE Product_id ='"+jTable1.getSelectedRow() +"'";
      stat.executeUpdate(sql);
      DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
      model.removeRow(jTable1.getSelectedRow());
    }
    catch (SQLException sqlException)
    {
      sqlException.printStackTrace();
      JOptionPane.showMessageDialog(null, "sql err");
    }
  } else {
    JOptionPane.showMessageDialog(null, "Please select an item to delete");
  }
}  

请告诉我,我做错了什么?

0 个答案:

没有答案