【问题标题】:Eclipse Rich Client Application Connect to databaseEclipse 富客户端应用程序连接到数据库
【发布时间】:2019-06-22 10:06:23
【问题描述】:

我正在启动一个新的 Eclipse RCP 应用程序,这是我第一次遇到问题,我想显示可用数据库的列表(顺便说一下,我使用的是 nosql 数据库(MongoDB))但我的代码似乎不工作,谁能帮忙,谁能给我一个好的教程 感谢您的时间和帮助。

package test2.parts;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.eclipse.e4.ui.di.Focus;
import org.eclipse.e4.ui.di.Persist;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCursor;
import org.eclipse.swt.widgets.Label;

public class SamplePart  {


    org.eclipse.swt.widgets.List list ;
    private TableViewer tableViewer;

    @Inject
    private MPart part;

    @PostConstruct
    public void createComposite(Composite parent) {
        parent.setLayout(new GridLayout(1, false));

        Text txtInput = new Text(parent, SWT.BORDER);
        txtInput.setMessage("Enter text to mark part as dirty");
        txtInput.addModifyListener(e -> part.setDirty(true));
        txtInput.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        list = new org.eclipse.swt.widgets.List(parent, SWT.BORDER);



        tableViewer = new TableViewer(parent);
        tableViewer.setContentProvider(ArrayContentProvider.getInstance());
        tableViewer.setInput(createInitialDataModel());
        tableViewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH));


    }

    @Focus
    public void setFocus() {
        tableViewer.getTable().setFocus();
    }

    @Persist
    public void save() {
        part.setDirty(false);
    }


    private List<String> createInitialDataModel() {

        MongoClient mongoClient = new MongoClient("localhost", 27017);
        ArrayList<String> dbs = new ArrayList<String>();
        MongoCursor<String> dbsCursor = mongoClient.listDatabaseNames().iterator();
        while (dbsCursor.hasNext()) {
            list.add(dbsCursor.next());
        }
        return (List<String>) list;

    }
}

【问题讨论】:

  • 请将您的代码发布为文本而不是图像。无法复制图像来测试代码。什么不起作用?发生什么了?你预计会发生什么?
  • 我希望从我的代码中列出数据库名称,

标签: java eclipse mongodb eclipse-rcp e4


【解决方案1】:

堆栈跟踪显示插件找不到 MongoClient 类。

Eclipse 插件只能访问其他插件或插件中包含的 jar 中的代码。他们不能使用普通 Java 类路径上的 jars。

因此,您需要将包含 MongoClient 类的 jar 添加到插件中,并将其添加到 MANIFEST.MF 中的 Bundle-Classpath。您可以在“运行时”选项卡的“类路径”部分的 MANIFEST.MF 编辑器中执行此操作。

您还需要在 build.properties 文件中包含该 jar。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多