-1

以下是 netbeans 在将 jtable 与 netbeans gui 编辑器绑定时生成的代码。

serverDetailsQuery = java.beans.Beans.isDesignTime() ? null : F1SoftSMSCPUEntityManager.createQuery("SELECT s FROM ServerDetails s ");    

我通过编辑更改了查询

SELECT s FROM ServerDetails s order by id desc

这工作正常。现在我想在查询中传递参数以通过 ShortCode 过滤记录,例如:

SELECT s FROM ServerDetails s where s.shortCode : filterShortCode order by id desc

短代码是文本字段,如果用户键入短代码并单击该按钮,则有一个按钮将过滤记录。

相关代码

@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {
    bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

    F1SoftSMSCPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("F1SoftSMSCPU").createEntityManager();
        serverDetailsQuery = java.beans.Beans.isDesignTime() ? null : F1SoftSMSCPUEntityManager.createQuery("SELECT s FROM ServerDetails s order by id desc");
    serverDetailsList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : serverDetailsQuery.getResultList();
    jPanel1 = new javax.swing.JPanel();
    serverDetailsPanel = new javax.swing.JPanel();
    bindButton = new javax.swing.JButton();
    jScrollPane1 = new javax.swing.JScrollPane();
    bindingDetailsTable = new javax.swing.JTable();
    blockButtonToBindingDetails = new javax.swing.JButton();
    smscBindingDetailsFilterPanel = new javax.swing.JPanel();
    filterByShortCodeTextField = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    smscBindingFilterButton = new javax.swing.JButton();


    }// Variables declaration - do not modify                     
private javax.persistence.EntityManager F1SoftSMSCPUEntityManager;
private javax.swing.JButton bindButton;
private javax.swing.JTable bindingDetailsTable;
private javax.swing.JButton blockButtonToBindingDetails;
private javax.swing.JTextField filterByShortCodeTextField;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private java.util.List<com.f1soft.SMSC.entities.ServerDetails> serverDetailsList;
private javax.swing.JPanel serverDetailsPanel;
private javax.persistence.Query serverDetailsQuery;
private javax.swing.JPanel smscBindingDetailsFilterPanel;
private javax.swing.JButton smscBindingFilterButton;
private org.jdesktop.beansbinding.BindingGroup bindingGroup;
// End of variables declaration     

感谢并愿意尽快得到专家的回复。

4

1 回答 1

2

以下是如何将参数传递给查询:

Query q = em.createQuery(jpql);
q.setParameter("filterShortCode", theParameterValue);

编辑:

上面的代码应该放在一个监听器方法中,当点击按钮时调用。它不应该被放置,initComponents()因为显然,当initComponents()被调用时,用户还没有填写文本并点击按钮:

smscBindingFilterButton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        String theParameterValue = filterByShortCodeTextField.getText();
        // TODO create and execute the query
    }
}

鉴于您的问题,我假设您对 Swing 和一般事件完全不熟悉。我建议您忘记 NetBeans 所见即所得编辑器,并使用不涉及绑定和 JPA 的简单示例进行练习。遵循Swing 教程,尤其是关于事件的部分

于 2013-08-08T06:50:45.760 回答