Java 在数据库中搜索名称并删除条目

Java 在数据库中搜索名称并删除条目,java,sql,database,search,Java,Sql,Database,Search,我正在制作一个Java程序,在这个程序中,我可以使用Java和SQL命令来访问数据库,制作一本本地烹饪书。我试图通过在文本字段中输入名称来删除数据库条目,然后删除数据库中具有相同名称的匹配条目。我有一个奇怪的错误,它说“executeQuery()”不能用作更新,但我仍然有更新方法。如果有人能知道我需要做些什么来解决这个问题,我将不胜感激 谢谢 /** Creates new form deleteRecipe */ public deleteRecipe() {

我正在制作一个Java程序,在这个程序中,我可以使用Java和SQL命令来访问数据库,制作一本本地烹饪书。我试图通过在文本字段中输入名称来删除数据库条目,然后删除数据库中具有相同名称的匹配条目。我有一个奇怪的错误,它说“executeQuery()”不能用作更新,但我仍然有更新方法。如果有人能知道我需要做些什么来解决这个问题,我将不胜感激

谢谢

    /** Creates new form deleteRecipe */
    public deleteRecipe() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        txtName = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnDelete = new javax.swing.JButton();
        btnBack = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtName.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        txtName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNameActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 18)); // NOI18N
        jLabel1.setText("What Recipe do you want to delete?");

        btnDelete.setText("Delete!");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnBack.setText("Back");
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(87, 87, 87)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtName)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(199, 199, 199)
                        .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnBack)))
                .addContainerGap(80, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(btnBack)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(30, 30, 30)
                .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(34, 34, 34))
        );

        pack();
    }// </editor-fold>                        

    private void txtNameActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                       

    private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {                                        
        this.dispose(); 
        CookbookApp ca = new CookbookApp();
        ca.setVisible(true);
    }                                       

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
        ArrayList<Cookbook  > recipes = new ArrayList();
        String name = txtName.getText();
        try {
            String url = "jdbc:derby://localhost:1527/Cookbook";
            Connection conn = DriverManager.getConnection(url);
            String query = "INSERT into RECIPES(NAME, SUBCATEGORY, CATEGORY, INGREDIENTS, INSTRUCTIONS, MODIFICATIONS) values(?,?,?,?,?,?)";
            PreparedStatement pst = conn.prepareStatement(query);
            pst.setString(1, name);
            ResultSet rs = pst.executeQuery();
            Cookbook recipe;

            while (rs.next()) {
                recipe = new Cookbook(rs.getString("NAME"), rs.getString("SUBCATEGORY"), rs.getString("CATEGORY"), rs.getString("INGREDIENTS"), rs.getString("INSTRUCTIONS"), rs.getString("MODIFICATIONS"));
                if (rs.getString(1).equalsIgnoreCase(name))
                recipes.remove(recipe);
            }
            pst.executeUpdate();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }

//    }
//     public ArrayList<Cookbook> recipes() {
//        ArrayList<Cookbook  > recipes = new ArrayList();
//        
//        try {
//            String url = "jdbc:derby://localhost:1527/Cookbook";
//            Connection conn = DriverManager.getConnection(url);
//            String query = "SELECT * FROM RECIPES";
//            PreparedStatement pst = conn.prepareStatement(query);
//            ResultSet rs = pst.executeQuery();
//
//            Cookbook recipe;
//            
//            pst.setString(1, name);
//            
//            
//            while (rs.next()) {
//                recipe = new Cookbook(rs.getString("NAME"), rs.getString("SUBCATEGORY"), rs.getString("CATEGORY"), rs.getString("INGREDIENTS"), rs.getString("INSTRUCTIONS"), rs.getString("MODIFICATIONS"));
//                if (rs.getString(1).equalsIgnoreCase(name))
//                recipes.remove(recipe);
//            }
//        } catch (Exception e) {
//            JOptionPane.showMessageDialog(null, e);
//        }
//        return recipes;
//    
    }                                         

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(deleteRecipe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(deleteRecipe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(deleteRecipe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(deleteRecipe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new deleteRecipe().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnBack;
    private javax.swing.JButton btnDelete;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField txtName;
    // End of variables declaration                   

}
/**创建新表单deleteRecipe*/
公共删除配方(){
初始化组件();
}
/**此方法从构造函数中调用,以
*初始化表单。
*警告:请勿修改此代码。这种方法的内容是
*始终由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//                           
私有组件(){
txtName=newjavax.swing.JTextField();
jLabel1=newjavax.swing.JLabel();
btnDelete=newjavax.swing.JButton();
btnBack=newjavax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setFont(新java.awt.Font(“Lucida Grande”,0,18));//NOI18N
addActionListener(新的java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
txtNameActionPerformed(evt);
}
});
jLabel1.setFont(新的java.awt.Font(“Lucida Grande”,1,18));//NOI18N
jLabel1.setText(“您想删除什么配方?”);
setText(“删除!”);
btnDelete.addActionListener(新的java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
执行的BTN删除操作(evt);
}
});
btnBack.setText(“返回”);
addActionListener(新的java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent evt){
btnBackActionPerformed(evt);
}
});
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(87,87,87)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,false)
.addComponent(jLabel1,javax.swing.GroupLayout.DEFAULT\u SIZE,javax.swing.GroupLayout.DEFAULT\u SIZE,Short.MAX\u值)
.addComponent(txtName)))
.addGroup(layout.createSequentialGroup()
.addGap(199199199199199)
.addComponent(btnDelete,javax.swing.GroupLayout.PREFERRED\u SIZE,101,javax.swing.GroupLayout.PREFERRED\u SIZE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(btnBack)))
.addContainerGap(80,简称最大值))
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup()
.addComponent(btnBack)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,69,Short.MAX_值)
.addComponent(jLabel1)
.addGap(30,30,30)
.addComponent(txtName,javax.swing.GroupLayout.PREFERRED\u SIZE,47,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addGap(38,38,38)
.addComponent(btnDelete,javax.swing.GroupLayout.PREFERRED\u SIZE,45,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addGap(34,34,34))
);
包装();
}//                         
私有void txtNameActionPerformed(java.awt.event.ActionEvent evt){
//TODO在此处添加您的处理代码:
}                                       
私有void btnBackActionPerformed(java.awt.event.ActionEvent evt){
这个。dispose();
CookbookApp ca=新建CookbookApp();
ca.setVisible(真);
}                                       
私有void btnDeleteActionPerformed(java.awt.event.ActionEvent evt){
ArrayList recipes=新的ArrayList();
String name=txtName.getText();
试一试{
String url=“jdbc:derby://localhost:1527/Cookbook";
连接conn=DriverManager.getConnection(url);
String query=“插入配方(名称、子类别、类别、成分、说明、修改)值(?,,,,,,,?)”;
PreparedStatement pst=conn.prepareStatement(查询);
pst.setString(1,名称);
ResultSet rs=pst.executeQuery();
食谱;
while(rs.next()){
配方=新食谱(rs.getString(“名称”)、rs.getString(“子类别”)、rs.getString(“类别”)、rs.getString(“配料”)、rs.getString(“说明”)、rs.getString(“修改”);
if(rs.getString(1.equalsIgnoreCase(名称))
配方。移除(配方);
}
pst.executeUpdate();
}捕获(例外e){
showMessageDialog(null,e);
}
//    }
//公共ArrayList配方(){
//ArrayList recipes=新的ArrayList();
//        
//试一试{
//