n
当我db.collection.find({'_id': {'$gt': record_id } }).sort({'_id': 1}).limit(n)
发生错误时。
//RESEARCH jframe code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package InterfaceDico;
import GestionDictionnaire.GestionDico;
import javax.swing.JOptionPane;
/**
*
* @author Franck
*/
public class RechercherMot extends javax.swing.JFrame {
/**
* Creates new form RechercherMot
*/
public RechercherMot() {
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() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
valider = new javax.swing.JButton();
entrer_mot = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
entrer_definition = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
jLabel1.setText(" RECHERCHER MOT ");
jLabel2.setText("MOT A RECHERCHER :");
valider.setText("RECHERCHER");
valider.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
validerActionPerformed(evt);
}
});
entrer_mot.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
entrer_motActionPerformed(evt);
}
});
entrer_definition.setColumns(20);
entrer_definition.setRows(5);
jScrollPane1.setViewportView(entrer_definition);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(valider))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(entrer_mot, javax.swing.GroupLayout.PREFERRED_SIZE, 277, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(entrer_mot, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(valider)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void validerActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
String mot= entrer_mot.getText();
entrer_mot.setText("");
String definition = entrer_definition.getText();
entrer_definition.setText("");
if(mot.equalsIgnoreCase("")){
JOptionPane.showMessageDialog(null, "données incorrectes", "Erreur", JOptionPane.ERROR_MESSAGE);
}
else{
GestionDico dico =new GestionDico();
dico.RechercherMot(mot);
答案 0 :(得分:0)
方法RechercherMot(String mot)
的返回类型无效。这意味着它没有返回任何东西。现在您已使用enter_definition.setText(dico.RechercherMot(mot))
其中enter_definition
是JTextArea
的实例,函数setText
需要String
作为参数。但是你的dico.RechercherMot(mot)
返回无效或无效。所以你得到编译错误, void无法转换为String;
现在,为了消除错误,您必须将RechercherMot(String mot)
的返回类型更改为String
,并使用方法中的String
语句返回有效的return
。
这里我从你所声明的内容中理解它应该返回正在执行的查询的文本格式化结果集。所以我的建议是你可以声明String
变量说result
并在创建预准备语句之前初始化为空字符串,如:
String result = "";
然后在循环内while(resultat.next())
替换
System.out.println("mot = " +resultat.getString("mot")+" "+"definition = "+resultat.getString("definition")+" ");
使用
result += "mot = " +resultat.getString("mot")+" "+"definition = "+resultat.getString("definition")+" \n";
最后在方法的最后一行,在catch
块插入之后:
return result;
当然,将方法的返回类型从void
更改为String
。